我发誓......我希望这是我要问的最后一个问题,但是我要发疯了。
我有一个使用自定义TableCellRenderer的JTable,它使用JEditorPane在JTable的各个单元格中显示html。如何处理点击JEditorPane中显示的链接?
我知道HyperlinkListener但是没有鼠标事件可以通过JTable传递到EditorPane,以便处理任何HyperlinkEvent。
如何在JTable中的JEditorPane中处理超链接?
答案 0 :(得分:6)
EditorPane没有收到任何事件,因为从TableCellRenderer返回的组件只允许显示,而不是截取事件,使其与图像几乎相同,不允许任何行为。因此,即使在监听者注册时,返回的组件也不会“意识到”任何事件。解决方法是在JTable上注册MouseListener,并从那里拦截所有相关事件。
这里是我过去创建的一些类,它们允许JButton翻转在JTable中工作,但你应该能够重复使用大部分这些类来解决你的问题。我为每个需要它的单元都有一个单独的JButton。这样,此ActiveJComponentTableMouseListener可以在鼠标事件发生的单元格中运行,并将事件调度到相应的组件。 ActiveJComponentTableCellRenderer的工作是通过Map跟踪组件。
知道它什么时候已经触发了事件,这很聪明,所以你不会得到积压的冗余事件。为超文本实现这一点不应该那么不同,你可能仍然想要翻转。这是课程
public class ActiveJComponentTableMouseListener extends MouseAdapter implements MouseMotionListener {
private JTable table;
private JComponent oldComponent = null;
private TableCell oldTableCell = new TableCell();
public ActiveJComponentTableMouseListener(JTable table) {
this.table = table;
}
@Override
public void mouseMoved(MouseEvent e) {
TableCell cell = new TableCell(getRow(e), getColumn(e));
if (alreadyVisited(cell)) {
return;
}
save(cell);
if (oldComponent != null) {
dispatchEvent(createMouseEvent(e, MouseEvent.MOUSE_EXITED), oldComponent);
oldComponent = null;
}
JComponent component = getComponent(cell);
if (component == null) {
return;
}
dispatchEvent(createMouseEvent(e, MouseEvent.MOUSE_ENTERED), component);
saveComponent(component);
save(cell);
}
@Override
public void mouseExited(MouseEvent e) {
TableCell cell = new TableCell(getRow(e), getColumn(e));
if (alreadyVisited(cell)) {
return;
}
if (oldComponent != null) {
dispatchEvent(createMouseEvent(e, MouseEvent.MOUSE_EXITED), oldComponent);
oldComponent = null;
}
}
@Override
public void mouseEntered(MouseEvent e) {
forwardEventToComponent(e);
}
private void forwardEventToComponent(MouseEvent e) {
TableCell cell = new TableCell(getRow(e), getColumn(e));
save(cell);
JComponent component = getComponent(cell);
if (component == null) {
return;
}
dispatchEvent(e, component);
saveComponent(component);
}
private void dispatchEvent(MouseEvent componentEvent, JComponent component) {
MouseEvent convertedEvent = (MouseEvent) SwingUtilities.convertMouseEvent(table, componentEvent, component);
component.dispatchEvent(convertedEvent);
// This is necessary so that when a button is pressed and released
// it gets rendered properly. Otherwise, the button may still appear
// pressed down when it has been released.
table.repaint();
}
private JComponent getComponent(TableCell cell) {
if (rowOrColumnInvalid(cell)) {
return null;
}
TableCellRenderer renderer = table.getCellRenderer(cell.row, cell.column);
if (!(renderer instanceof ActiveJComponentTableCellRenderer)) {
return null;
}
ActiveJComponentTableCellRenderer activeComponentRenderer = (ActiveJComponentTableCellRenderer) renderer;
return activeComponentRenderer.getComponent(cell);
}
private int getColumn(MouseEvent e) {
TableColumnModel columnModel = table.getColumnModel();
int column = columnModel.getColumnIndexAtX(e.getX());
return column;
}
private int getRow(MouseEvent e) {
int row = e.getY() / table.getRowHeight();
return row;
}
private boolean rowInvalid(int row) {
return row >= table.getRowCount() || row < 0;
}
private boolean rowOrColumnInvalid(TableCell cell) {
return rowInvalid(cell.row) || columnInvalid(cell.column);
}
private boolean alreadyVisited(TableCell cell) {
return oldTableCell.equals(cell);
}
private boolean columnInvalid(int column) {
return column >= table.getColumnCount() || column < 0;
}
private MouseEvent createMouseEvent(MouseEvent e, int eventID) {
return new MouseEvent((Component) e.getSource(), eventID, e.getWhen(), e.getModifiers(), e.getX(), e.getY(), e.getClickCount(), e.isPopupTrigger(), e.getButton());
}
private void save(TableCell cell) {
oldTableCell = cell;
}
private void saveComponent(JComponent component) {
oldComponent = component;
}}
public class TableCell {
public int row;
public int column;
public TableCell() {
}
public TableCell(int row, int column) {
this.row = row;
this.column = column;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final TableCell other = (TableCell) obj;
if (this.row != other.row) {
return false;
}
if (this.column != other.column) {
return false;
}
return true;
}
@Override
public int hashCode() {
int hash = 7;
hash = 67 * hash + this.row;
hash = 67 * hash + this.column;
return hash;
}}
public class ActiveJComponentTableCellRenderer<T extends JComponent> extends AbstractCellEditor implements TableCellEditor, TableCellRenderer {
private Map<TableCell, T> components;
private JComponentFactory<T> factory;
public ActiveJComponentTableCellRenderer() {
this.components = new HashMap<TableCell, T>();
}
public ActiveJComponentTableCellRenderer(JComponentFactory<T> factory) {
this();
this.factory = factory;
}
public T getComponent(TableCell key) {
T component = components.get(key);
if (component == null && factory != null) {
// lazy-load component
component = factory.build();
initialiseComponent(component);
components.put(key, component);
}
return component;
}
/**
* Override this method to provide custom component initialisation code
* @param component passed in component from getComponent(cell)
*/
protected void initialiseComponent(T component) {
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
return getComponent(new TableCell(row, column));
}
@Override
public Object getCellEditorValue() {
return null;
}
@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
return getComponent(new TableCell(row, column));
}
public void setComponentFactory(JComponentFactory factory) {
this.factory = factory;
}}
public interface JComponentFactory<T extends JComponent> {
T build();
}
要使用它,您需要将侦听器注册为表上的鼠标和动作侦听器,并在适当的单元格上注册渲染器。如果要拦截actionPerformed类型事件,请覆盖ActiveJComponentTableCellRenderer.initialiseComponent(),如下所示:
protected void initialiseComponent(T component) {
component.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
stopCellEditing();
}
});
}
答案 1 :(得分:1)
如果在JTable上注册MouseListener
,则可以轻松地在鼠标单击点处获取文本。这可以通过使用Point
和MouseEvent
从event.getX()
生成event.getY()
对象来完成。然后,您将Point
传递给JTable
的{{1}}和rowAtPoint(pt)
。从那里,您可以通过columnAtPoint(pt)
获取文字。现在您拥有了单元格的值,因此您可以确定它是否是链接,并根据结果执行您想要的操作。
答案 2 :(得分:0)
为了解决同样的问题,我没有尝试让JEditorPane生成事件,而是处理了JTable生成的MouseEvent,让我们在点击某个链接时弄清楚了。
这是代码。它保留了JEditorPanes的映射,因此请确保没有内存泄漏,并且如果表中的数据可以更改,则应适当地清除此映射。它从我实际使用的代码中略微修改 - 在我实际使用的版本中,它只在实际链接在html中时才生成JEditorPane,并且在没有这样的链接时没有使用JEditorPanes ......
Service