0

I have created a web crawler and then a GUI for users to search through the database etc… What I would like is now for the JXTable to clickable to go to the URL. Here is my code for the JXTable:

outPut = new JXTable(tableModel);
add(new JScrollPane(outPut), BorderLayout.CENTER);
outPut.setEditable(false);
AbstractHyperlinkAction<Object> simpleAction = new AbstractHyperlinkAction<Object>(null) {
    @Override
        public void actionPerformed(ActionEvent e) {
        //No idea what goes here
    }
};

I have got it to display from the database as shown below but have no clue how to make the cells clickable.

Just shows the output of the search result

This is supposed to be the mouse listener for the table. Doesn't work at all sadly. I have set the table edit to false as well. I'm not sure where to go after this cause I'm not sure if the research I'm reading is correct or not.

private static boolean isURLColumn(JTable outPut, int column) {
    return column>=0 && outPut.getColumnClass(column).equals(URL.class);
}
public void mouseClicked(MouseEvent e) {
    outPut = (JTable)e.getSource();
    Point pt = e.getPoint();
    int ccol = outPut.columnAtPoint(pt);
    if(isURLColumn(outPut, ccol)) {
        int crow = outPut.rowAtPoint(pt);
        URL url = (URL)outPut.getValueAt(crow, ccol);
        try {
            if(Desktop.isDesktopSupported()) {
                Desktop.getDesktop().browse(url.toURI());
            }
        }
        catch(Exception ex) {
            ex.printStackTrace();
        }
    }
}

This is the code that populates the JTable using a default table model:

ResultSetMetaData metaData = rS.getMetaData();
// Names of columns
Vector<String> columnNames = new Vector<>();
int columnCount = metaData.getColumnCount();
for (int i = 1; i <= columnCount; i++) {
    columnNames.add(metaData.getColumnName(i));
}

// Data of the table
Vector<Vector<Object>> data = new Vector<>();
while (rS.next()) {
    Vector<Object> vector = new Vector<>();
    for (int i = 1; i <= columnCount; i++) {
        vector.add(rS.getObject(i));
    }
    data.add(vector);
}

tableModel.setDataVector(data, columnNames);
tableModel.setRowCount(maxRow);
grg
  • 5,023
  • 3
  • 34
  • 50
Relentless
  • 81
  • 1
  • 11
  • That is not a `SSCCE`. I can't compile the code since it is not complete. I don't have access to your database. Create a simple JFrame with a simple JTable. Hardcode the URL into one or two cells of the table. Add the MouseListener to the table and click, on the cell containing the URL. The point of a SSCCE is to simplify the problem to its basics. – camickr Mar 26 '15 at 16:51

1 Answers1

1

You could use a custom editor. Take a look at the section from the Swing tutorial on Using Other Editor. The example displays a JColorChooser dialog when you double click on the cell. You could customize the code to display your URL is a webpage.

Take a look at the table of contents. The tutorial also has a section on How to Integrate With the Desktop Class which makes it easy to display your system browser.

Or the other option is to add a MouseListener to the table and then display the browser, again using the desktop class. The tutorial also has a section on How to Write a MouseListener.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • To be honest I have no clue what im doing now as I have never used any of this stuff before. Does below look remotely right in a mouse listener? – Relentless Mar 26 '15 at 16:17
  • outPut = (JXTable)e.getSource(); Point pt = e.getPoint(); int ccol = outPut.columnAtPoint(pt); if(isURLColumn(outPut, ccol)) { int crow = outPut.rowAtPoint(pt); URL url = (URL)outPut.getValueAt(crow, ccol); try{ if(Desktop.isDesktopSupported()) { Desktop.getDesktop().browse(url.toURI()); } – Relentless Mar 26 '15 at 16:17
  • @Relentless Don't post code is a comment. As you can see it is not formatted and difficult to read. Code looks reasonable. If it doesn't work then post a proper [SSCCE](http://sscce.org/) that demonstrates the problem in your question. And use a JTable in the `SSCCE` so we don't have to download JXTable just to test your code. – camickr Mar 26 '15 at 16:22