单击jtable中的超链接? [英] Click hyperlink in jtable?

查看:71
本文介绍了单击jtable中的超链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为JTable中的每个记录启用超链接?

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?

另一个问题是我目前正在使用以下方式显示不同的屏幕.但这不是我理解我们应该使用cardlayout的一种优雅方式,但是如何精确地做到这一点呢?

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

推荐答案

要解决JTable消耗事件的问题,可以在JTable上添加自己的MouseListener(或MouseAdapter),然后在此侦听器中进行操作.这是您可以实现的示例:

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

}

这篇关于单击jtable中的超链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆