2

我在 JTable 中有按钮。我已将 ActionListener 添加到这些按钮中,但是当我单击编辑时,什么也没有发生。

在此处输入图像描述

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.nio.ByteOrder;

import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTree;
import javax.swing.border.EmptyBorder;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;

public class eSCCMyView extends JFrame {

/**
 * 
 */
private static final long serialVersionUID = 1L;

final int WIDTH_FRAME = 800;
final int HEIGHT_FRAME = 600;
final int x_frame = 0;
final int y_frame = 0;

private JPanel motherPanel = null;
private JButton bt_edit = new JButton("Edit");

private JTable tableA = null;
private JTable tableB = null;

private Object[] colNames = {"Col-1", "Col-2", "Col-3", "Button1", "Col-4", "Col-5", "Col-6", "Button2"};
private Object[][] data = {
                   {"One", "Two", "Three", bt_edit, "Four", "Five", "Six", bt_edit},
                   {"Four", "Five", "Six", bt_edit, "Four", "Five", "Six", bt_edit},
                   {"Four", "Five", "Six", bt_edit, "Four", "Five", "Six", bt_edit},
                   {"Four", "Five", "Six", bt_edit, "Four", "Five", "Six", bt_edit}
                   };

public static void main(String[] args) {

    EventQueue.invokeLater(new Runnable() {

        public void run() {
            try {
                JFrame eSCCFrame = new eSCCMyView();
                eSCCFrame.setVisible(true);
            }catch(Exception e) {
                e.printStackTrace();
            }
        }
    });
}

public eSCCMyView() {
    setTitle("eSCC My View");
    setBounds(x_frame, y_frame, WIDTH_FRAME, HEIGHT_FRAME);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    /*
     *  motherPanel is the main panel to which we add all the panels.
     */
    motherPanel = new JPanel();
    motherPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
    motherPanel.setLayout(new BorderLayout(0, 0));
    setContentPane(motherPanel);

    /*
     * A panel is added to mother panel to add the splitPanel;
     */
    JPanel panel2AddSplit = new JPanel();
    motherPanel.add(panel2AddSplit, BorderLayout.CENTER);
    panel2AddSplit.setLayout(new BorderLayout(0, 0));

    /*
     * Add a splitPanel on top of panel2AddSplit;
     */
    JSplitPane splitPane = new JSplitPane();
    panel2AddSplit.add(splitPane, BorderLayout.CENTER);

    /*
     * Add a panel to the left of the left of the splitPanel;
     */
    JPanel pl_leftTree = new JPanel();
    splitPane.setLeftComponent(pl_leftTree);
    pl_leftTree.setBackground(Color.WHITE);
    splitPane.setResizeWeight(0.1);

    /*
     * Add tree to the left panel;      
     */
    JTree tree = new JTree();
    pl_leftTree.add(tree, BorderLayout.CENTER);

    /*
     * Add a right panel to the splitPane;
     */
    JPanel pl_rightPanel = new JPanel();
    pl_rightPanel.setLayout(new BorderLayout(0, 0));
    splitPane.setRightComponent(pl_rightPanel);

    /*
     * This panel is used to add buttons, comboBoxes, checkBoxes
     */
    JPanel pl_toAddButtons = new JPanel();
    pl_rightPanel.add(pl_toAddButtons, BorderLayout.NORTH);

    JLabel lb_plant = new JLabel("Plant");
    pl_toAddButtons.add(lb_plant);

    ImageIcon image = new ImageIcon("SampleView/images/Arrow.png");
    JButton bt_imageButton = new JButton(image);
    pl_toAddButtons.add(bt_imageButton);

    JLabel lb_subPlant = new JLabel("Sub-Plant");
    pl_toAddButtons.add(lb_subPlant);

    JButton bt_arrowButton = new JButton(" -> ");
    pl_toAddButtons.add(bt_arrowButton);

    JCheckBox ck_boxActive = new JCheckBox("Lab Active");
    pl_toAddButtons.add(ck_boxActive);

    JCheckBox ck_boxLimit = new JCheckBox("LT");
    pl_toAddButtons.add(ck_boxLimit);

    JComboBox cb_box = new JComboBox();
    cb_box.setModel(new DefaultComboBoxModel(new String []{"0 - 100", "101 - 1000", "1001 - 1500"}));
    pl_toAddButtons.add(cb_box);

    /*
     * To add a tab to the rightPanel;
     */
    JTabbedPane tabPane = new JTabbedPane(JTabbedPane.TOP);
    pl_rightPanel.add(tabPane, BorderLayout.CENTER);

    JPanel pl_firsttabPanel = new JPanel();
    tabPane.addTab("First", null, pl_firsttabPanel, null);
    pl_firsttabPanel.setBackground(Color.WHITE);
    pl_firsttabPanel.setLayout(new BorderLayout(0, 0));

    JPanel pl_secondtabPanel = new JPanel();
    tabPane.addTab("Second", null, pl_secondtabPanel, null);
    pl_secondtabPanel.setLayout(new BorderLayout(0, 0));
    /*
     * In this second tab you need to create a splitpane as a component;
     */
    //JSplitPane sp_secondTab = new JSplitPane();
    //sp_secondTab.setT

    JPanel pl_inSecondTabPanel = new JPanel();
    pl_secondtabPanel.add(pl_inSecondTabPanel, BorderLayout.CENTER);
    pl_inSecondTabPanel.setLayout(new BorderLayout(0, 0));

    JPanel pl_inSecondPanelTwo = new JPanel();
    pl_secondtabPanel.add(pl_inSecondPanelTwo, BorderLayout.SOUTH);
    pl_inSecondPanelTwo.setLayout(new BorderLayout(0, 0));

    JPanel pl_bottomPanelInSecondTab = new JPanel();
    pl_inSecondPanelTwo.add(pl_bottomPanelInSecondTab, BorderLayout.SOUTH);

    /*
     *  The following 3-buttons Save, Save & Send, Supply-Chain are present in the secondTab's Panel;
     */
    JButton bt_save = new JButton("Save");
    pl_bottomPanelInSecondTab.add(bt_save);

    JButton bt_savenSend = new JButton("Save & Send");
    pl_bottomPanelInSecondTab.add(bt_savenSend);

    JButton bt_supplyChain = new JButton("Supply Chain");
    pl_bottomPanelInSecondTab.add(bt_supplyChain);

    /*
     * We need a panel to add SplitPane in the existing two panels in the secondTab;
     */
    JPanel pl_forSplitPanel = new JPanel();
    pl_forSplitPanel.setLayout(new BorderLayout(0, 0));
    pl_inSecondTabPanel.add(pl_forSplitPanel, BorderLayout.CENTER);

    JSplitPane sp_inSecondTab = new JSplitPane();
    sp_inSecondTab.setOrientation(JSplitPane.VERTICAL_SPLIT);
    pl_forSplitPanel.add(sp_inSecondTab, BorderLayout.CENTER);

    /*
     * Assign two tables say tableA, tableB to the splitPane sp_inSecondTab;
     */
    //JTable tableA = new JTable(data, colNames);
    tableA = new JTable(new CustomModelForTable(colNames, data));
    JTableHeader tableAHeader = tableA.getTableHeader();
    tableAHeader.setBackground(Color.GRAY);

    TableCellRenderer defaultRenderer = tableA.getDefaultRenderer(JButton.class);
    tableA.setDefaultRenderer(JButton.class, new JButtonRendererClass(defaultRenderer));

    //tableB = new JTable(data, colNames);
    tableB = new JTable(new CustomModelForTable(colNames, data));
    tableAHeader = tableB.getTableHeader();
    tableAHeader.setBackground(Color.GRAY);
    TableCellRenderer tableBRenderer = tableB.getDefaultRenderer(JButton.class);
    tableB.setDefaultRenderer(JButton.class, new JButtonRendererClass(tableBRenderer));


    sp_inSecondTab.setLeftComponent(new JScrollPane(tableA));
    sp_inSecondTab.setRightComponent(new JScrollPane(tableB));
    sp_inSecondTab.setOneTouchExpandable(true);
    sp_inSecondTab.setResizeWeight(0.5);

    // Add ActionListener to bt_edit button;
    bt_edit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            int row = tableA.getSelectedRow();
            System.out.println("Row Selected is: " + row);
            CustomModelForTable cus = (CustomModelForTable) tableA.getModel();
            cus.removeRow(row);
        }
    });
}
}

PS:我在代码末尾为按钮添加了动作监听器。

4

2 回答 2

4
于 2012-09-03T10:28:39.320 回答
4

我认为虽然按钮显示在表格上,但它并不是真正的按钮,而是像按钮一样呈现的单元格。

您可以收听表格的模型,以查看用户何时单击具有按钮的单元格并在发生这种情况时采取行动。

于 2012-09-03T10:29:21.343 回答