问题
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);
}
}
});
回答1:
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
来源:https://stackoverflow.com/questions/11780845/hyperlinks-in-jxtable-column-populated-from-database