选择JTable行时,该行中应显示按钮

时间:2013-07-10 14:22:58

标签: java swing jtable awt

实际上我在使用JTable时遇到了问题。

我的桌子上有三列。

表结构如下:

Parameter Name | Parameter Type | Operation

上面是它的三个栏目.. 考虑表包含一行如下:

Parameter Name | Parameter Type | Operation
------------------------------------------------------------------------------
Request            |         String          |     [Delete]
------------------------------------------------------------------------------
Response          |         String          |     [Delete]
------------------------------------------------------------------------------

此处[删除]是按钮。 这就是所有情况..

现在我想要的是每当我选择行时,只有删除按钮应该是可见的。

你能帮帮我吗?

1 个答案:

答案 0 :(得分:1)

您应该创建一个Custom DefaultTableRenderer和一个Custom TableCellEditor

也许这就是你要找的东西:

public class ButtonTable extends JDialog {

    private final JPanel contentPanel = new JPanel();
    private JTable table;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        try {
            ButtonTable dialog = new ButtonTable();
            dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            dialog.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Create the dialog.
     */
    public ButtonTable() {
        setBounds(100, 100, 450, 300);
        getContentPane().setLayout(new BorderLayout());
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        getContentPane().add(contentPanel, BorderLayout.CENTER);
        GridBagLayout gbl_contentPanel = new GridBagLayout();
        gbl_contentPanel.columnWidths = new int[]{414, 0};
        gbl_contentPanel.rowHeights = new int[]{240, 0};
        gbl_contentPanel.columnWeights = new double[]{1.0, Double.MIN_VALUE};
        gbl_contentPanel.rowWeights = new double[]{1.0, Double.MIN_VALUE};
        contentPanel.setLayout(gbl_contentPanel);

        JScrollPane scrollPane = new JScrollPane();
        GridBagConstraints gbc_scrollPane = new GridBagConstraints();
        gbc_scrollPane.fill = GridBagConstraints.BOTH;
        gbc_scrollPane.gridx = 0;
        gbc_scrollPane.gridy = 0;
        contentPanel.add(scrollPane, gbc_scrollPane);

        table = new JTable() { 
            public boolean isCellEditable(int row, int column) {
                return column == 2;
            };
        };
        table.setFillsViewportHeight(true);
        table.setModel(new DefaultTableModel(
            new Object[][] {
                {"ONE", null, null},
                {"TWO", null, null},
                {"THREE", null, null},
                {"FOUR", null, null},
                {"FIVE", null, null},
            },
            new String[] {
                "First Column", "Second Column", "Button"
            }
        ));
        table.getColumnModel().getColumn(0).setPreferredWidth(196);
        table.getColumnModel().getColumn(1).setPreferredWidth(180);
        table.setDefaultEditor(Object.class, new TableEditor(new JTextField()));
        table.setDefaultRenderer(Object.class, new TableRenderer());
        scrollPane.setViewportView(table);
    }
}

class TableEditor extends DefaultCellEditor implements TableCellEditor {

    //This is just because the Constructor of superclass needs a JTextField, or JCheckBox or whatever...
    public TableEditor(JTextField field) {
        super(field);
    }

    @Override
    public Component getTableCellEditorComponent(JTable table, Object value,
            boolean isSelected, int row, int column) {

        if (isSelected && column == 2)
            return new JButton("Delete");
        return super.getTableCellEditorComponent(table, value, isSelected, row, column);
    }
}

class TableRenderer extends DefaultTableCellRenderer {
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {

        if (isSelected && column == 2)
            return new JButton("Delete");
        return super.getTableCellRendererComponent(table, value, isSelected, hasFocus,
                row, column);
    }
}