Click hyperlink in jtable?

2020-07-14 12:49发布

How can I enable hyperlink for every record in the JTable?

What I want to do is such that a user can click on the hyperlink which will then display the information they can edit/update.

Alternatively how can I enable in place editing of the table data?

Another question is i am currently using the following way to display different screen. But this is not an elegant way i understand we should use cardlayout but how exactly to go about it?

mainPanel.setVisible(false);
createBlogEntryPanel.setVisible(true);
setComponent(createBlogEntryPanel);

2条回答
贪生不怕死
2楼-- · 2020-07-14 12:53

To address the problem with JTable consuming the events, you can add your own MouseListener (or a MouseAdapter) to the JTable and do your manipulation inside this listener. Here is an example of what you can achieve:

public class Main extends JFrame {

public Main() {
    super();

    DefaultTableModel dt = new DefaultTableModel(
            new String[][] { { "http://google.com" }, { "http://gmail.com" } }, new String[] { "Url" });
    final JTable t = new JTable(dt);

    t.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            int row = t.rowAtPoint(new Point(e.getX(), e.getY()));
            int col = t.columnAtPoint(new Point(e.getX(), e.getY()));
            System.out.println(row + " " + col);

            String url = (String) t.getModel().getValueAt(row, col);
            System.out.println(url + " was clicked");
            // DO here what you want to do with your url
        }

        @Override
        public void mouseEntered(MouseEvent e) {
            int col = t.columnAtPoint(new Point(e.getX(), e.getY()));
            if (col == 0) {
                t.setCursor(new Cursor(Cursor.HAND_CURSOR));
            }
        }

        @Override
        public void mouseExited(MouseEvent e) {
            int col = t.columnAtPoint(new Point(e.getX(), e.getY()));
            if (col != 0) {
                t.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
            }
        }
    });

    add(new JScrollPane(t));

    t.getColumnModel().getColumn(0).setCellRenderer(new TableCellRenderer() {

        @Override
        public Component getTableCellRendererComponent(JTable table, final Object value, boolean arg2,
                boolean arg3, int arg4, int arg5) {
            final JLabel lab = new JLabel("<html><a href=\"" + value + "\">" + value + "</a>");
            return lab;
        }
    });

    setSize(700, 500);
    setLocationRelativeTo(null);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public static void main(String[] args) {
    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception e) {
        e.printStackTrace();
    }

    new Main();
}

}
查看更多
家丑人穷心不美
3楼-- · 2020-07-14 12:56

(The question is way too vague to answer it concisely/completely - this answer assume the main part to be the hyperlink in JTable)

Consider using a framework that fully supports hyperlinks in collection components (table, list, tree ..) - like f.i. SwingX

For a code example in SwingX, see a recent answer

Edit

But what if only part of the cell is a hyperlink, or worse, if there are 2 different links in a cell?

As mentioned in my comment: that's not supported in SwingX (it's cell-based).

So it's back to square one with several options

The ol' style: (mis-)use a cellEditor

  • have a editingComponent which is/contains a hyperlink
  • have a mouseListener which starts editing on rollover the cell
  • from now on the live-component takes care of mouseEvents and can trigger the appropriate actions on presses/clicks at the hyperlink/s

Extending SwingX style: enrich the rollover interface (the incubator may have examples, but that wasn't added to core SwingX for the reason of not yet being ready :-) The basic idea is to

  • add "hot-spots" the rollover interface
  • enhance (or replace) the rolloverProducer to produce finer-grained notifications once rollover-aware cell is detected
  • translate the mousePosition to the cell (rendering component) coordinates and query the cell if or not that corresponds to a hot-spot, if (and only if) so, arm and on click trigger the action

The second can be implemented independently of SwingX, of course: a plain ol' custom mouseListener and the mapping logic should do the trick (sorry, no code handy)

查看更多
登录 后发表回答