query

1095Currently online
188Today's Reading
45Share Today
Multilingual display

Use of Swing tables

2018-05-05 06:24:33

A table is a common component in a program. A table in Swing is JTable. The function of a table is to display data in the form of a two-dimensional table and allow users to edit the data in the table. Today we will introduce the simple use of JTable

Tools/Materials

eclipse

1. Construction method
1

Constructor 1: JTable(int numRows,int numColumns) : Use DefaultTableModel to construct a JTable with numRows and empty cells in numColumns.

2

This article is based on experience

Second, common methods

Common methods are as follows:

Iii. Examples
1

Create an instance, set the table to select only one row, click Delete to delete the selected table row.

2

The Demo32_JTable2 class code is as follows: 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(){ @Override public void windowActivated(WindowEvent e){ do_this_windowActivated(e); }}); setTitle(' Book information Table '); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); SwingUtils.setCenter(this); // Set form size 600*800 and center contentPane=new JPanel(); contentPane.setBorder(new EmptyBorder(5,5,5)); contentPane.setLayout(new BorderLayout(0,0)); setContentPane(contentPane); JPanel panel=new JPanel(); contentPane.add(panel,BorderLayout.SOUTH); JButton button=new JButton(' Delete '); 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();    // Get the table model tableModel.setRowCount(0); / / clear the table data in tableModel. SetColumnIdentifiers (new Object [] {' title ', 'press', 'publication date', 'series category,' pricing '}); tableModel.addRow(new Object[]{'Java from Beginner to Master (2nd edition) ',' Tsinghua University Press ','2010-07-01',' Introduction Series for Software Engineers ','59.8 Yuan '}); . / / add columns tableModel addRow (new Object [] {' PHP from entry to the master (version 2) ', 'tsinghua university press', '2010-07-01', 'software engineers introductory books',' $69.8 '}); tableModel.addRow(new Object[]{'Visual Basic from Beginner to Master (2nd edition) ',' Tsinghua University Press ','2010-07-01',' Introduction Books for Software Engineers ','69.8 Yuan '}); tableModel.addRow(new Object[]{'Visual C++ from Introduction to Mastery (2nd edition) ',' Tsinghua University Press ','2010-07-01',' Introduction Series for Software Engineers ','69.8 Yuan '}); table.setRowHeight(30); table.setModel(tableModel);    // Apply table model} protected void do_button_actionPerformed(ActionEvent e){DefaultTableModel model=(DefaultTableModel) table.getModel();    // Get table model int[] selectedRows= table.getselectedrows (); for(int i=0; i

3

The SwingUtils class code is as follows:  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); }}

Recommendation