HyperLinks in JXTable column, populated from database
Asked Answered
B

1

3

My question 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.
         }
   }
});

EDIT 1 The code from EDIT 2 of @Kleopatra have some issues for my application. Also, I made another try like the code bellow, and voila - the links are there when first click is involved, but don't react (no browser open). @Kleopatra, can you provide me more information about your suggestion, because when I'm trying to put that code, the IDE don't recognize hyperlinkColumn.

Table_Employee.addMouseListener(new MouseAdapter() {
   public void mouseClicked(MouseEvent e) {
      if (e.getClickCount() == 1) {
         JXTable target = (JXTable)e.getSource();
         int row = target.getSelectedRow();
         int column = target.getSelectedColumn();
         AbstractHyperlinkAction<Object> simpleAction = new AbstractHyperlinkAction<Object>(null) {

                public void actionPerformed(ActionEvent e) {
                //open the browser event?
                }
         };

    TableCellRenderer renderer = new DefaultTableRenderer(
    new HyperlinkProvider(simpleAction));
        Table_Employee.getColumnExt(2).setEditable(false);
        Table_Employee.getColumnExt(2).setCellRenderer(renderer);
      }
   }
});
Burnout answered 2/8, 2012 at 15:26 Comment(0)
D
7

no listeners involved, SwingX has a HyperlinkProvider that you simply configure with an action as needed:

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);

Note that the column needs to be not-editable to auto-support hyperlinks.

Edit 2

If you want to open the browser/mail client, then use a HyerlinkProvider configured with a SwingX HyperlinkAction. This is backed by core DesktopAction. Its target must be of type URI, then it auto-detects whether to open the one or other.

You can manually install it per-column:

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

In fact, it's even simpler: JXTable already has it as default for URI class. All you have to do is to implement your model such that it contains uris and reports that as its column class:

DefaultTableModel model = new DefaultTableModel(...) {

     @Override
     Class<?> getColumnClass(int column) {
         if (column == hyperlinkColumn) {
             return URI.class;
         } 
         ... // handle other columns
         return super.getColumnClass(column);
     }

     @Override
     boolean isCellEditable(int row, int column) {
         if (column == hyperlinkColumn) {
            return false; 
         }
         ... // handle other columns
         return super.isCellEditable(row, column); 
     }
}

For seeing that support in action - it's the same for JXList/JXTree - run the SwingLabs web demo: examples are on several pages, f.i. JXTable, Hyperlink/extended, Highlighter and skim through the code that comes with it

Dartmouth answered 2/8, 2012 at 15:46 Comment(6)
AncientSwingTeam is a class of what? and also the LOG. Can you provide me more information. I do this in IDE NB. Thanks in advance. Thanks for your timeBurnout
// here goes what you want to do on activating the hyperlink And how can I make the column cells to be clickable to that link which is in the cell?Burnout
I got this exception java.lang.ArrayIndexOutOfBoundsException:0 >= 0 and if judge right, the problem is from table model because I do not populate model just the visual table? Again, I want to specify that my jxtable is populated from database by a sql query. Thanks for your time. How can I make it work?Burnout
Something wrong with your code, of course :-) It's impossible to populate just the visual table, not the model, the data is in the model always. No offense meant - but to me it looks like you need to learn some basics, not only of Swing but basic java language as well.Dartmouth
No problem, I realize that. The best learning is practice. What I try to say is that the exception error comes from not assigned columns to the table model. Or I'm wrong. The defaultmodel is called.Burnout
Many thanks for your time @kleopatra. You really motivated me to solve this homework. I solved with Runtime.getRuntime() which execute the iexplorer.Burnout

© 2022 - 2024 — McMap. All rights reserved.