表格是程式中一種很常見的元件,Swing中的表格是:JTable,表格的功能是把資料以二維表格的形式顯示出來,並且允許使用者對表格中的資料進行編輯。今天我們來介紹一下JTable的簡單使用
1
構造方法1:
JTable(int numRows,int numColumns):使用 DefaultTableModel 構造具有 numRows 行和 numColumns 列個空單元格的 JTable。
2
構造方法2:
JTable(Object[][] rowData,Object[] columnNames):構造一個 JTable 來顯示二 維陣列 rowData 中的值,其列名稱為 columnNames。
1
建立一個範例,設定表格只能選擇單行,點選刪除可以刪除選中的表格行。
2
Demo32_JTable2?類程式碼如下:public class Demo32_JTable2 extends JFrame{private JPanel contentPane;private JTable table;public static void main(String[] args){Demo32_JTable2 frame = new Demo32_JTable2();}public Demo32_JTable2() {addWindowListener(new WindowAdapter(){@Overridepublic void windowActivated(WindowEvent e){do_this_windowActivated(e);}});setTitle("圖書資訊表");setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);SwingUtils.setCenter(this);//設定表單大小600*800並居中contentPane=new JPanel();contentPane.setBorder(new EmptyBorder(5,5,5,5));contentPane.setLayout(new BorderLayout(0,0));setContentPane(contentPane);JPanel panel=new JPanel();contentPane.add(panel,BorderLayout.SOUTH);JButton button=new JButton("刪除");button.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){do_button_actionPerformed(e);}});panel.add(button);JScrollPane scrollPane=new JScrollPane();contentPane.add(scrollPane,BorderLayout.CENTER);table=new JTable();table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);scrollPane.setViewportView(table);setVisible(true);}protected void do_this_windowActivated(WindowEvent e){DefaultTableModel tableModel=(DefaultTableModel) table.getModel();? ? //獲得表格模型tableModel.setRowCount(0);? ? //清空表格中的資料tableModel.setColumnIdentifiers(new Object[]{"書名","出版社","出版時間","叢書類別","定價"});? ? //設定表頭tableModel.addRow(new Object[]{"Java從入門到精通(第2版)","清華大學出版社","2010-07-01","軟體工程師入門叢書","59.8元"});? ? //增加列tableModel.addRow(new Object[]{"PHP從入門到精通(第2版)","清華大學出版社","2010-07-01","軟體工程師入門叢書","69.8元"});tableModel.addRow(new Object[]{"Visual Basic從入門到精通(第2版)","清華大學出版社","2010-07-01","軟體工程師入門叢書","69.8元"});tableModel.addRow(new Object[]{"Visual C++從入門到精通(第2版)","清華大學出版社","2010-07-01","軟體工程師入門叢書","69.8元" });table.setRowHeight(30);table.setModel(tableModel);? ? //應用表格模型}protected void do_button_actionPerformed(ActionEvent e){DefaultTableModel model=(DefaultTableModel) table.getModel();? ? //獲得表格模型int[] selectedRows=table.getSelectedRows();for(int i=0;i<selectedRows.length;i++){model.removeRow(selectedRows[0]);System.out.println();}table.setModel(model);}}ame.EXIT_ON_CLOSE);SwingUtils.setCenter(this);//設定表單大小600*800並居中contentPane=new JPanel();contentPane.setBorder(new EmptyBorder(5,5,5,5));contentPane.setLayout(new BorderLayout(0,0));setContentPane(contentPane);JPanel panel=new JPanel();contentPane.add(panel,BorderLayout.SOUTH);JButton button=new JButton("刪除");button.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){do_button_actionPerformed(e);}});panel.add(button);JScrollPane scrollPane=new JScrollPane();contentPane.add(scrollPane,BorderLayout.CENTER);table=new JTable();table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);scrollPane.setViewportView(table);setVisible(true);}protected void do_this_windowActivated(WindowEvent e){DefaultTableModel tableModel=(DefaultTableModel) table.getModel();? ? //獲得表格模型tableModel.setRowCount(0);? ? //清空表格中的資料tableModel.setColumnIdentifiers(new Object[]{"書名","出版社","出版時間","叢書類別","定價"});? ? //設定表頭tableModel.addRow(new Object[]{"Java從入門到精通(第2版)","清華大學出版社","2010-07-01","軟體工程師入門叢書","59.8元"});? ? //增加列tableModel.addRow(new Object[]{"PHP從入門到精通(第2版)","清華大學出版社","2010-07-01","軟體工程師入門叢書","69.8元"});tableModel.addRow(new Object[]{"Visual Basic從入門到精通(第2版)","清華大學出版社","2010-07-01","軟體工程師入門叢書","69.8元"});tableModel.addRow(new Object[]{"Visual C++從入門到精通(第2版)","清華大學出版社","2010-07-01","軟體工程師入門叢書","69.8元" });table.setRowHeight(30);table.setModel(tableModel);? ? //應用表格模型}protected void do_button_actionPerformed(ActionEvent e){DefaultTableModel model=(DefaultTableModel) table.getModel();? ? //獲得表格模型int[] selectedRows=table.getSelectedRows();for(int i=0;i<selectedRows.length;i++){model.removeRow(selectedRows[0]);System.out.println();}table.setModel(model);}}3
SwingUtils?類程式碼如下:public class SwingUtils {public static void setCenter(JFrame jf) {int screenWidth=Toolkit.getDefaultToolkit().getScreenSize().width;int screenHeight=Toolkit.getDefaultToolkit().getScreenSize().height;?int jframeWidth = 800;int jframeHeight = 600;jf.setBounds((screenWidth/2)-(jframeWidth/2), (screenHeight/2)-(jframeHeight/2),?? ? ? ? jframeWidth, jframeHeight);}}