Skip to main content

HyperLinks in JXTable column, populated from database

2 replies [Last post]
Tzontonel
Offline
Joined: 2012-08-02
Points: 0

From this thread:

My questions is how can I make the hyperlinks from a jxtable column (just for one/specific column) to action like "_blank" links of my default desktop web browser.

I use jxtable and a DefaultTableModel, also I call the data from a sqlite database. I made the research on the internet, google, [...] and I found a lot of information which says, If I don't make a mistake:

registering a MouseListener to jxtable;
generate point object from MouseEvent;
get the text via getValueAt

***Note: The column have just 1 link per cell, without any text, just the link.

For now I have implemented this code to make an action where a cell is double clicked. Please someone can help me to implement a column hyperlinks which opening in default browser like in this example (but I don't know how to adapt because the data are not called from a database).

Table_Employee.addMouseListener(new MouseAdapter() {
   public void mouseClicked(MouseEvent e) {
      if (e.getClickCount() == 2) {
         JXTable target = (JXTable)e.getSource();
         int row = target.getSelectedRow();
         int column = target.getSelectedColumn();
           JFrame newFrame = new JFrame();               //I want to open an distinc link
               newFrame.setTitle("Detail Screen");       //for every cell along one column
               newFrame.setVisible(true);                //in the web browser, not a frame.
         }
   }
});

When I try to put the solution provided by @Kleopatra:

JXTable mytable = new JXTable(new AncientSwingTeam());
AbstractHyperlinkAction<Object> simpleAction = new AbstractHyperlinkAction<Object>(null) {

    public void actionPerformed(ActionEvent e) {
        LOG.info("hit: " + getTarget());
    }

};
TableCellRenderer renderer = new DefaultTableRenderer(
    new HyperlinkProvider(simpleAction));
mytable.getColumnExt(0).setEditable(false);
mytable.getColumnExt(0).setCellRenderer(renderer);

An error occur, AncientSwingTeam and LOG need to be a class. Please, someone help me. I use IDE NB.

Andrew

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
kschaefe
Offline
Joined: 2006-06-08
Points: 0

Copying an example without understanding it is never a good idea. Take your time to decipher what is going on there. If you don't know where the class AncientSwingTeam is you can still determine what it is and replace it correctly for your own code.

Karl

Tzontonel
Offline
Joined: 2012-08-02
Points: 0

Someone can help me please:

JXTable table = new JXTable(myModel);
AbstractHyperlinkAction<Object> simpleAction = new AbstractHyperlinkAction<Object>(null) {

    public void actionPerformed(ActionEvent e) {
        // here goes what you want to do on activating the hyperlink
        //LOG.info("hit: " + getTarget());
    }

};
TableCellRenderer renderer = new DefaultTableRenderer(
    new HyperlinkProvider(simpleAction));
table.getColumnExt(0).setEditable(false);
table.getColumnExt(0).setCellRenderer(renderer);

I tried this, but don't work:

AbstractHyperlinkAction<Object> simpleAction = new AbstractHyperlinkAction<Object>(null) {

            public void actionPerformed(ActionEvent e) {
                open(uri);
            }

        };
        TableCellRenderer renderer = new DefaultTableRenderer(
            new HyperlinkProvider(simpleAction));
        Table_Employee.getColumnExt(3).setEditable(false);                 //(3) is the third column where
        Table_Employee.getColumnExt(3).setCellRenderer(renderer);          //I have links?

private static void open(URI uri) {
    if (Desktop.isDesktopSupported()) {
      try {
        Desktop.getDesktop().browse(uri);
      } catch (IOException e) { //error handling}
    } else { //error handling }
  }