2012-09-03 6 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 : 나는 코드의 끝 부분에있는 버튼에 대한 액션 청취자 추가 한. (제외 내장 JCheckBox as Boolean value)을 JTable에서 JButtons JComponent에 대한

+0

무엇이 당신의 질문입니까? 나는 7을 생각할 수 있지만, 하나를 선택하여 그것을 포스트로 편집하십시오. –

+0

@AndrewThompson 작업 수신기를 추가 한 후에도 테이블의 단추가 아무런 작업도 수행하지 않습니다. 적어도 actionPerformed 메서드에 도달하지 못했습니다. – Amarnath

답변

4

버튼이 테이블에 표시되는 동안 실제로 버튼이 아니라 버튼처럼 렌더링되는 셀이라고 생각합니다.

테이블의 모델을 청취하여 사용자가 언제 버튼이있는 셀을 클릭했는지 알 수 있습니다.

+0

정답과 적절한 방법 – mKorbel

+0

예. 심지어 나는 지금도 같은 생각을하고 있습니다. 감사. – Amarnath

관련 문제