Data addition of java GUI table

Data addition of java GUI table

Function introduction: Manually add all the information of the user by clicking the button. When the added data exceeds the table frame, a scroll bar will appear automatically to realize the addition of data.
The effect is as follows:

achieve effect

 

Code:

package test;

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

public class Test extends JFrame {

	// public static final ArrayList<Object> list = new ArrayList<Object>();
	private JPanel contentPane;
	private JTextField nameField;
	private JTextField ageField;
	private JTextField sexField;
	private JTextField telField;
	private JTable table;
	private JButton addButton;
	private DefaultTableModel dtm;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Test frame = new Test();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace ();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Test() {
		setTitle("Add user information");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 477, 367);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout (null);

		//user label
		JLabel lblNewLabel = new JLabel("Username:");
		lblNewLabel.setBounds(249, 17, 54, 15);
		contentPane.add(lblNewLabel);

		//age label
		JLabel lblNewLabel_1 = new JLabel("年 龄:");
		lblNewLabel_1.setBounds(249, 77, 54, 15);
		contentPane.add(lblNewLabel_1);

		//gender label
		JLabel lblNewLabel_2 = new JLabel("性  别:");
		lblNewLabel_2.setBounds(249, 147, 54, 15);
		contentPane.add(lblNewLabel_2);

		//phone label
		JLabel lblNewLabel_3 = new JLabel("Phone:");
		lblNewLabel_3.setBounds(249, 221, 54, 15);
		contentPane.add(lblNewLabel_3);

		//Name text input box
		nameField = new JTextField();
		nameField.setBounds(313, 14, 131, 21);
		contentPane.add(nameField);
		nameField.setColumns(10);

		//age text input box
		ageField = new JTextField();
		ageField.setBounds(313, 74, 131, 21);
		contentPane.add(ageField);
		ageField.setColumns(10);

		//Gender text input box
		sexField = new JTextField();
		sexField.setBounds(313, 147, 131, 21);
		contentPane.add(sexField);
		sexField.setColumns(10);

		//phone text input box
		telField = new JTextField ();
		telField.setBounds (313, 218, 131, 21);
		contentPane.add(telField);
		telField.setColumns (10);

		//data add button
		addButton = new JButton("添加");
		addButton.setBounds(309, 283, 93, 23);
		contentPane.add(addButton);

		// scroll window panel
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 10, 218, 250);
		contentPane.add(scrollPane);

		//Add the table frame to the scroll window
		table = new JTable();
		//Set the default model of the table
		dtm = new DefaultTableModel(
				new Object[][] { { "\u5F20\u4E09", "20", "\u7537", "123456" },
						{ "\u674E\u56DB", "20", "\u7537", "123456" }, },
				new String[] { "\u59D3\u540D", "\u5E74\u9F84", "\u6027\u522B", "\u7535\u8BDD" });
		table.setModel(dtm);
		// table.setModel(new DefaultTableModel(
		// new Object[][] { { "\u5F20\u4E09", "20", "\u7537", "123456" },
		// { "\u674E\u56DB", "20", "\u7537", "123456" }, },
		// new String[] { "\u59D3\u540D", "\u5E74\u9F84", "\u6027\u522B",
		// "\u7535\u8BDD" }));
		table.setBounds(10, 10, 200, 200);

		// Set the preferred size [components other than the form need to set the preferred size]
		scrollPane.setPreferredSize(new Dimension(500, 400));
		// Set the scroll bar to appear automatically
		scrollPane.setAutoscrolls(true);
		scrollPane.setViewportView(table);

		/**
		 * Create button click event to add data
		 */
		addButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {

				// Get the contents of the text box separately
				String name = nameField.getText();
				String age = ageField.getText();
				String sex = sexField.getText();
				String tel = telField.getText();

				//Add the content of the textbox to the table
				dtm.addRow(new String[]{name,age,sex,tel});
			}

		});
	}
}

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326535125&siteId=291194637