JXTable列中的HyperLinks,从数据库填充 [英] HyperLinks in JXTable column, populated from database

查看:134
本文介绍了JXTable列中的HyperLinks,从数据库填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是如何将JXTable列(仅适用于一个/特定列)的超链接转换为我的默认桌面Web浏览器的_blank链接。

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.

我使用JXTable和DefaultTableModel,我也从sqlite数据库调用数据。我在互联网上进行了研究,google,[...]我找到了很多信息,如果我没有犯错:

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:


  • 将一个MouseListener注册到JXTable;

  • 从MouseEvent生成点对象;

  • 通过getValueAt获取文本

***注意:该列每个单元格只有1个链接,没有任何文本,只有链接。

***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.
         }
   }
});

编辑1
来自@Kleopatra的编辑2的代码我的申请有一些问题。此外,我做了另一个尝试,如下面的代码,瞧 - 当涉及第一次点击时,链接就在那里,但没有反应(没有浏览器打开)。
@Kleopatra,您能否向我提供有关您的建议的更多信息,因为当我尝试放置该代码时,IDE无法识别 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);
      }
   }
});


推荐答案

没有涉及听众,SwingX有一个HyperlinkProvider,你只需要根据需要配置动作:

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.

编辑2

如果要打开浏览器/邮件客户端,请使用配置的HyerlinkProvider使用SwingX HyperlinkAction。这是由核心DesktopAction支持的。它的目标必须是URI类型,然后它会自动检测是否打开一个或另一个。

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.

您可以按列手动安装:

TableCellRenderer renderer = new DefaultTableRenderer(
    new HyperlinkProvider(new HyperlinkAction()));
table.getColumnExt(0).setEditable(false);
table.getColumnExt(0).setCellRenderer(renderer);

事实上,它甚至更简单:JXTable已经将它作为URI类的默认值。您所要做的就是实现您的模型,使其包含uris并将其报告为其列类:

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

为了看到支持在行动 - 它是相同的JXList / JXTree - 运行 SwingLabs网络演示:示例在几个页面上,fi JXTable,超链接/扩展,荧光笔和浏览它附带的代码

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

这篇关于JXTable列中的HyperLinks,从数据库填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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