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

1 回答 1

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.

于 2015-03-26T15:48:33.143 回答