2012-09-03 66 views
2

我在JTable中有按鈕。我已經將ActionListener添加到這些按鈕,但是當我單擊編輯時,什麼也沒有發生。JButton的動作監聽器不能在JTable中工作

enter image description here

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

P.S:我已在碼的末尾添加動作偵聽器按鈕。

+0

你的問題是什麼?我能想到7,但你選擇一個並將其編輯到帖子中。 –

+0

@AndrewThompson即使在添加動作偵聽器之後,表格中的按鈕也不會執行任何操作。至少我沒有達到actionPerformed方法。 – Amarnath

回答

4

我認爲,雖然按鈕顯示在桌子上,但它不是一個真正的按鈕,而是一個像按鈕一樣呈現的單元格。

您可以偵聽表格的模型,看看使用過的點擊單元格上有一個按鈕,並在發生這種情況時採取行動。

+0

正確答案和正確方法 – mKorbel

+0

是的。即使我現在也這樣想。謝謝。 – Amarnath