【问题标题】:Is it possible jtable row in optionDialog box选项对话框中的jtable行是否可能
【发布时间】:2016-06-12 06:13:25
【问题描述】:

我有一个 jtable。

一些单元格包含很长的字符串,尝试左右滚动很困难。我的问题是是否可以在弹出窗口中显示 JTable 中的一行,例如 showDialog 类型框(即,所选行被组织为一列)。

即使是指向教程的链接也可以。

我搜索了互联网,但我认为我没有真正使用正确的关键字,因为我得到了很多右键单击选项。

如果无法做到这一点,是否有任何其他建议可以帮助您做到这一点?

【问题讨论】:

  • where the selected row is organised as a column - 不知道这意味着什么,但有可能。但是,当我回答您之前的一个问题时,您仍然没有回答我的问题:stackoverflow.com/a/37606317/131872
  • 哇。这是令人印象深刻的坚持。我现在为另一个不相关的问题添加了一个答案,但解决方法很懒惰(我知道像我这样的人应该......)我没有追求它(最后我知道只有我会受苦...... .)

标签: java swing jtable


【解决方案1】:

如图hereJOptionPane工厂方法会显示Object传入message参数。如果 message 是一列 JTable,您可以回收应用到原始表的任何自定义 renderers and editors

概括地说,

  • ListSelectionListener 添加到您的表并获取selectedRow

  • 遍历表的模型并构造一个newModel,其行是selectedRow的列。

  • 创建一个JTable newTable = new JTable(newModel)

  • 应用任何非默认渲染器和编辑器。

  • new JScrollPane(newTable) 作为message 参数传递给您选择的JOptionPane 方法。

从这个example开始,下面的监听器显示如图所示的对话框。

table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
    @Override
    public void valueChanged(ListSelectionEvent e) {
        int selectedRow = table.convertRowIndexToModel(table.getSelectedRow());
        if (selectedRow > -1) {
            DefaultTableModel newModel = new DefaultTableModel();
            String rowName = "Row: " + selectedRow;
            newModel.setColumnIdentifiers(new Object[]{rowName});
            for (int i = 0; i < model.getColumnCount(); i++) {
                newModel.addRow(new Object[]{model.getValueAt(selectedRow, i)});
            }
            JTable newTable = new JTable(newModel) {
                @Override
                public Dimension getPreferredScrollableViewportSize() {
                    return new Dimension(140, 240);
                }
            };
            // Apply any custom renderers and editors
            JOptionPane.showMessageDialog(f, new JScrollPane(newTable),
                rowName, JOptionPane.PLAIN_MESSAGE);
        }
    }
});

【讨论】:

【解决方案2】:

我想显示行中的所有值,每个值都在它们的 cel 中,垂直组织 - 这就是我所说的“在一列中”。

这应该在问题中,而不是在评论中。

没有默认功能,但你可以自己做。

您可以创建一个 JPanel(可能使用 GridBagLayout),在一行中有两个标签来表示表格中所选行的列中的数据。

对于第一个标签中的数据,您可以使用 TableModel 的 getColumnName(...) 方法。

对于第二个标签中的数据,您可以使用 TableModel 的 getValueAt(...) 方法。

另一种选择是简单地显示单元格的工具提示。有关更多信息,请参阅 Specifying ToolTips For Cells 上的 Swing 教程部分。

【讨论】:

  • 好的。谢谢。我已经接受了@trashgod 的回答,但这也很有帮助
【解决方案3】:

您可以使用以下ListSelectionListener

final JTable dialogTable =new JTable();     
table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
        @Override
        public void valueChanged(ListSelectionEvent event) {
            int selectedRow = table.getSelectedRow();
            if (selectedRow > -1) {
                int columnCount = table.getModel().getColumnCount();
                Object[] column = new Object[]{"Row "+(selectedRow+1)};
                Object[][] data = new Object[columnCount][1];
                for (int i = 0; i < columnCount; i++) {
                    Object obj = table.getModel().getValueAt(selectedRow, i);
                    data[i][0] = obj;
                }
                dialogTable.setModel(new DefaultTableModel(data, column));                  
                JOptionPane.showMessageDialog(null, new JScrollPane(dialogTable));
            }
        }
});

这将显示一个消息对话框,其中包含一个JTable,其中包含从选定行派生的数据。希望这对您有所帮助。

【讨论】:

  • 我想显示行中的所有值,每个值都在它们的 cel 中,垂直组织 - 这就是我所说的“在一列中”的意思。我认为这段代码只会显示一个单独的单元格值
  • 为什么不用ListSelectionListener 而不是MouseListener
  • @SebastianZeki 请你看看我的回答。我已根据您的需要对其进行了编辑。
  • @SanjeevSaha:很好地抓住空选择,但不要忘记translate cell coordinates
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-14
  • 2013-08-19
  • 1970-01-01
  • 1970-01-01
  • 2012-03-09
相关资源
最近更新 更多