JAVA GUI学习 - JTable表格组件学习_C ***

  1 /**

  2  * JTable高级应用

  3  * @author Wfei

  4  *

  5  */

  6 public class JTableKnow_C extends JFrame

  7 {

  8     JTable jTable;

  9     MyJMenuItem jMenuItem;

 10     MyJMenuItem jMenuItem2;

 11     JPopupMenu jPopupMenu;

 12     JMenu jMenu;

 13     public JTableKnow_C()

 14     {

 15         this.setBounds(300, 100, 400, 300);

 16         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 17         

 18         Object[][] tableData = new Object[3][4];

 19         tableData[0] = new Object[]{"1-1","1-2","左",true};

 20         tableData[1] = new Object[]{"2-1","2-2","右",false};

 21         tableData[2] = new Object[]{"3-1","3-2","左",true};

 22         Object[] tableColName = new Object[]{"第一列名","第二列名","第三列名","第四列名"};

 23         //创建表格模型

 24         DefaultTableModel defaultTableModel = new DefaultTableModel(tableData,tableColName);

 25         //实例化表格

 26         jTable = new JTable(defaultTableModel)

 27         {

 28             /**

 29              * 判断某一列是否为boolean类型,如果是:以复选框显示

 30              */

 31             @Override

 32             public Class<?> getColumnClass(int column)

 33             {

 34                 // TODO Auto-generated method stub

 35                 return jTable.getValueAt(0, column).getClass();

 36             }

 37             /**

 38              * 设置某一行、某一列、或某个单元格是否可编辑

 39              */

 40             @Override

 41             public boolean isCellEditable(int row, int column)

 42             {

 43                 // TODO Auto-generated method stub

 44                 if (row == 1 && column == 1)

 45                 {

 46                     return false;

 47                 }

 48                 else {

 49                     return true;

 50                 }

 51             }

 52             //表格的标题,必须配合滚动面板一起使用才能显示出来

 53         };

 54         //实例化滚动面板

 55         JScrollPane jScrollPane = new JScrollPane(jTable);

 56         this.add(jScrollPane);

 57         

 58         jPopupMenu = new JPopupMenu();

 59         jMenu = new JMenu("更改为:");

 60         jMenuItem = new MyJMenuItem("左",1);

 61         jMenuItem2 = new MyJMenuItem("右",1);

 62         jPopupMenu.add(jMenu);

 63         jMenu.add(jMenuItem);jMenu.add(jMenuItem2);

 64         

 65         jTable.setComponentPopupMenu(jPopupMenu);

 66         jPopupMenu.addPopupMenuListener(new PopupMenuListener()

 67         {

 68             /**

 69              * 设置个性化显示菜单的监听事件

 70              */

 71             @Override

 72             public void popupMenuWillBecomeVisible(PopupMenuEvent e)

 73             {

 74                 int index = jTable.getSelectedRow();

 75                 if (index != -1)

 76                 {

 77                     String str = jTable.getValueAt(index, 2).toString();

 78                     if (str.equals("左"))

 79                     {

 80                         jMenuItem.setVisible(false);

 81                         jMenuItem2.setVisible(true);

 82                     }

 83                     else {

 84                         jMenuItem.setVisible(true);

 85                         jMenuItem2.setVisible(false);

 86                     }

 87                 }

 88             }

 89             @Override

 90             public void popupMenuWillBecomeInvisible(PopupMenuEvent e)

 91             {

 92             }

 93             @Override

 94             public void popupMenuCanceled(PopupMenuEvent e)

 95             {

 96             }

 97         });

 98         //为菜单的每个jMenuItem加上监听器

 99 //        addListenerToJMenuItem(jMenu);

100     }

101 //    public void addListenerToJMenuItem(Container container)

102 //    {

103 //        for (Component component : container.getComponents())

104 //        {

105 //            if (component == JMenuItem.class)

106 //            {

107 //                System.out.println("1111111");

108 //                JMenuItem jMenuItem = (JMenuItem) component;

109 //                jMenuItem.addActionListener(new MenuItemListener());

110 //            }

111 //            else {

112 //                System.out.println("2222222");

113 //                JMenu jMenu = (JMenu) component;

114 //                addListenerToJMenuItem(jMenu);

115 //            }

116 //        }

117 //    }

118     /**

119      * 菜单项监听器

120      * @author Administrator

121      *

122      */

123     private class MenuItemListener implements ActionListener

124     {

125 

126         @Override

127         public void actionPerformed(ActionEvent e)

128         {

129             JMenuItem item = (JMenuItem)e.getSource();

130             // TODO Auto-generated method stub

131             //JOptionPane.showMessageDialog(null, item.getText());

132             //判断是否有选中项

133             if(jTable.getSelectedRow() != -1)

134             {

135                 //判断按钮

136                 if (item == jMenuItem)

137                 {

138                     JOptionPane.showMessageDialog(null, "已更改为:右");

139                 }

140                 else {

141                     JOptionPane.showMessageDialog(null, "已更改为:左");

142                 }

143             }

144             else

145             {

146                 JOptionPane.showMessageDialog(null, "请选择修改的用户");

147             }

148         }

149         

150     }

151     public static void main(String[] args)

152     {

153         JTableKnow_C jTableKnowC = new JTableKnow_C();

154         jTableKnowC.setVisible(true);

155     }

156 }

 

你可能感兴趣的:(jtable)