HyperLinks in a JXTable column populated from a database

My question is how can I make hyperlinks from a JXTable column (for only one / specific column) for action, for example, the default "_blank" links of my web browser.

I use JXTable and DefaultTableModel, also I am calling data from sqlite database. I did a study on the Internet, google, [...] and I found a lot of information that says: "If I'm not mistaken:

  • register MouseListener in JXTable;
  • generate a point feature from MouseEvent;
  • get text through getValueAt

*** Note: the column has only 1 cell link, no text, only a link.

At the moment, I have implemented this code to take action when the cell is double clicked. Please someone can help me implement hyperlinks of columns that open in the browser by default, for example, in this example (but I don’t know how to adapt because the data does not call from the 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 Code from EDIT 2 of @Kleopatra has some problems for my application. In addition, I made another attempt, like the code below, and voila - there are links when the first click is activated, but they do not respond (the browser does not open). @Kleopatra, can you provide me more information about your proposal, because when I try to put this code, the IDE does not recognize the Column hyperlink.

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);
      }
   }
});
+3
1

, SwingX HyperlinkProvider, :

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

, .

2

/ , HyerlinkProvider, SwingX HyperlinkAction. DesktopAction. URI , , .

:

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

, : JXTable URI-. , , , uris , :

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

, - JXList/JXTree - - SwingLabs: , f.i. JXTable, Hyperlink/extended, Highlighter ,

+7

All Articles