1

我在最后一列的 JTable 中有一个 JButton,当客户端单击它时,它将显示一个 JFrame。但是我不知道如何获取该行,以便可以获取该行中的对象并将其发送到 JFrame 构造函数?

这是我的桌子:

table = new JTable(model);
    JTableHeader tableHeader = table.getTableHeader();
    tableHeaderRenderer = table.getTableHeader().getDefaultRenderer();
    tableHeader.setDefaultRenderer(new TableCellRenderer() {
        private JLabel label;
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value,
                boolean isSelected, boolean hasFocus, int row, int column) {
            if (selectedColumn == value) {
                label = (JLabel) tableHeaderRenderer.getTableCellRendererComponent(table,
                        value, true, true, row, column);
                label.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
                label.setBorder(BorderFactory.createCompoundBorder(label.getBorder(), 
                          BorderFactory.createEmptyBorder(0, 5, 0, 0)));
                label.setHorizontalAlignment(SwingConstants.LEFT);
            } else {
                label = (JLabel) tableHeaderRenderer.getTableCellRendererComponent(table,
                        value, false, false, row, column);
                label.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
                label.setBorder(BorderFactory.createCompoundBorder(label.getBorder(), 
                          BorderFactory.createEmptyBorder(0, 5, 0, 0)));
                label.setHorizontalAlignment(SwingConstants.CENTER);
            }
            return label;
        }
    });
    table.getColumnModel().getColumn(3).setCellRenderer(new TableCellRenderer() {
        public Component getTableCellRendererComponent(JTable table, Object value,
                boolean isSelected, boolean hasFocus, int row, int column) {
            JPanel buttonPanel = new JPanel(new BorderLayout());
            buttonPanel.setBackground(Color.white);
            JButton button = Shorter.button("Details", true);
            buttonPanel.add(button);
            return buttonPanel;
        }
    });
    table.getColumnModel().getColumn(3).setCellEditor(new TableCellEditor() {
        public boolean stopCellEditing() {return false;}
        public boolean shouldSelectCell(EventObject arg0) {return false;}
        public void removeCellEditorListener(CellEditorListener arg0) {}
        public boolean isCellEditable(EventObject arg0) {return true;}
        public Object getCellEditorValue() {return null;}
        public void cancelCellEditing() {}
        public void addCellEditorListener(CellEditorListener arg0) {}
        public Component getTableCellEditorComponent(JTable arg0, Object arg1,
                boolean arg2, int arg3, int arg4) {
            JFrame frame = new JFrame();
            frame.setVisible(true);
            return null;}
    });

加载框架时,我想知道选择哪一行添加到框架构造函数中...

4

3 回答 3

2

但是我不知道如何获取该行,以便可以获取该行中的对象并将其发送到 JFrame 构造函数?

有三个选择

  1. JButton inside a JTable in the last column必须返回正确坐标的事件JTables view,在这种情况下,您必须将视图转换为JTables view模型

    • 排序的

    • 过滤

    • 列重新排序

    • 列被隐藏(从 JTables 视图中删除)

  2. (和/或与)添加ListSelectionListenerJTable

  3. Mouse Events也可以返回,阅读官方Oracle tutorial How to use Tables - Specifying Tool Tips for Cells的工作代码示例

于 2013-11-08T11:00:25.810 回答
1

ButtonColumn我前段时间写了一个简单的,我认为它可以帮助你一点,但正如@mKorbel 提到的,你必须解决排序,过滤等问题。

.

public class ButtonColumn extends AbstractCellEditor 
    implements TableCellRenderer, TableCellEditor{

    private JButton button;
    private String value;

    public ButtonColumn(){
        button = new JButton();
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                System.out.println("pressed"+value);
            }
        });
    }

    @Override
    public Component getTableCellRendererComponent(JTable arg0, Object arg1,
            boolean arg2, boolean arg3, int arg4, int arg5) {
        button.setText(arg4+"");
        value = " "+arg4;
        return button;
    }


    @Override
    public Object getCellEditorValue() {
        return null;
    }

    @Override
    public Component getTableCellEditorComponent(JTable arg0, Object arg1,
            boolean arg2, int arg3, int arg4) {
        button.setText(arg3+"");
        value = " "+arg3;
        return button;
    }
}

通过以下方式将其设置为您的列:

table.getColumnModel().getColumn(YOUR_COLUMN_NUMBER).setCellEditor(new ButtonColumn());
table.getColumnModel().getColumn(YOUR_COLUMN_NUMBER).setCellRenderer(new ButtonColumn());
于 2013-11-08T11:13:02.243 回答
0

有关为您提供行的解决方案,请参阅表格按钮列。您需要做的就是为其提供一个Action.

于 2013-11-08T16:39:08.760 回答