单击jtable中的超链接?

发布于 2024-10-04 12:02:57 字数 316 浏览 4 评论 0原文

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

我想要做的是,用户可以单击超链接,然后该超链接将显示他们可以编辑/更新的信息。

或者,如何启用表数据的就地编辑?

另一个问题是我目前正在使用以下方式来显示不同的屏幕。但这不是一种优雅的方式,我知道我们应该使用卡片布局,但究竟如何去做呢?

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怀里藏娇 2024-10-11 12:02:57

要解决 JTable 消耗事件的问题,您可以将自己的 MouseListener(或 MouseAdapter)添加到 JTable 并在此侦听器中进行操作。以下是您可以实现的目标的示例:

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

}

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

}
冷默言语 2024-10-11 12:02:57

(这个问题太模糊,无法简洁/完整地回答它 - 这个答案假设主要部分是 JTable 中的超链接)

考虑使用完全支持集合组件(表、列表、树 ..) - 就像 fi SwingX

有关 SwingX 中的代码示例,请参阅 最近的答案

编辑

但是,如果单元格中只有一部分是超链接,或更糟糕的是,单元格中有 2 个不同的链接,该怎么办?

正如我的评论中提到的:SwingX 不支持这一点(它是基于单元格的)。

所以它回到了有几个选项的原点

ol' 风格:(误)使用 cellEditor

  • 有一个编辑组件,它是/包含一个超链接
  • 有一个 mouseListener,从现在开始在单元格翻转时开始编辑
  • ,实时组件负责处理mouseEvents 并可以在超链接上按下/单击时触发适当的操作

扩展 SwingX 样式:丰富翻转界面(孵化器可能有示例,但由于尚未准备好,因此未添加到核心 SwingX 中:- )基本思想是

  • 添加“热点”翻转接口,
  • 增强(或替换)rolloverProducer,以便在检测到翻转感知单元后生成更细粒度的通知
  • ,将 mousePosition 转换为单元(渲染组件)坐标并查询单元是否对应于热点,如果(且仅当)如此,则手臂和单击触发操作

第二个可以独立于 SwingX 实现,当然:一个简单的自定义 mouseListener 和映射逻辑应该做技巧(抱歉,手边没有代码)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文