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