Student Course Selection System-Swing+MySQL

Posted by prue_ on Thu, 12 Sep 2019 11:22:23 +0200

Student Course Selection System-Swing+MySQL(2)

JTable form is absolutely an important part of the interface of this course selection system. After all, as a course selection system, most of the information is displayed through JTable form. Through these days, the use of tables is summarized.

First of all, the data from the database is imported.

The simplest way is to directly pass in a two-dimensional array, specifying the table column name array and the table data array to create the table.

Object[] columnNames = new String[] { "Course number", "Name", "time", "Teacher", "Classroom" };
List<Course> courses = new CourseDAO().referList(teacherName);
Object[][] data = new String[courses.size()][5];
int count = 0;
		for (Course course : courses) {
			data[count][0] = String.valueOf(course.courseId);
			data[count][1] = String.valueOf(course.courseName);
			data[count][2] = String.valueOf(course.courseTime);
			data[count][3] = String.valueOf(course.courseTeacher);
			data[count][4] = String.valueOf(course.courseRoom);
			count++;
		}
JTable table = new JTable(dataļ¼ŒcolumnNames);

However, it is very difficult to update the data in real time, and every update array needs a new for loop. Obviously, this method is not desirable. Then, I found another way, AbstractTableModel - a magical Abstract class, write a TableModel, inherit this class, and then implement getRowCount(), getColumnCount(), getValueAt(), the three methods, real-time data update perfect implementation, beauty.

import java.util.List;

import javax.swing.table.AbstractTableModel;

public class TableModel extends AbstractTableModel {

	/**
	* 
	*/
	private static final long serialVersionUID = 1L;

	String[] columnNames = new String[] { "Course number", "Name", "time", "Teacher", "Classroom"};

	// Use List returned from DAO as TableModel data

	public List<Course> courses = new CourseDAO().list();

	// How many rows are returned by courses.size
	public int getRowCount() {
		// TODO Auto-generated method stub
		return users.size();
	}

    // How many columns are returned by courses.size
	public int getColumnCount() {
		// TODO Auto-generated method stub
		return columnNames.length;
	}

	public String getColumnName(int columnIndex) {
		return columnNames[columnIndex];
	}

	public boolean isCellEditable(int rowIndex, int columnIndex) {
		return false;
	}

	// Then return the corresponding properties based on columnIndex
	public Object getValueAt(int rowIndex, int columnIndex) {
		Course course = courses.get(rowIndex);
		if (0 == columnIndex)
			return course.courseId;
		if (1 == columnIndex)
			return course.courseName;
		if (2 == columnIndex)
			return course.courseTime;
		if (3 == columnIndex)
			return course.courseTeacher;
         if (4 == columnIndex)
			return course.courseRoom;
		return null;
	}

}

TableModel model = new TableModel();

JTable table = new JTable(model);

Look, real-time updates are so simple, and the pleasure lasts until I do the query section. I find that I can't upload parameters to TableModel. Well, it seems that I have no affinity with you. I can't help it. I have started a new attempt.

Finally, I took part of both, so my final version is as follows:

public static Object[][] data(int semester){
		List<Course> courses = new CourseDAO().list(semester);
		Object[][] data = new String[courses.size()][5];
		int count = 0;
		for (Course course : courses) {
			data[count][0] = String.valueOf(course.courseId);
			data[count][1] = String.valueOf(course.courseName);
			data[count][2] = String.valueOf(course.courseTime);
			data[count][3] = String.valueOf(course.courseTeacher);
			data[count][4] = String.valueOf(course.courseRoom);
			count++;
		}
		return data;
	}
public CourseMain() throws Exception {
		JFrame frame = new JFrame("Management interface");
		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
		// Get the width and height of the screen
		double width = screen.getWidth();
		double height = screen.getHeight();
		int x = (int) (width - 800) / 2;
		int y = (int) (height - 700) / 2;
		frame.setBounds(x, y, 1000, 600);
		frame.getContentPane().setBackground(Color.WHITE);
		Font font = new Font("Regular script", Font.BOLD, 20);

		Object[] columnNames = new String[] { "Course number", "Name", "time", "Teacher", "Classroom" };
		Object[][] data = data(semester);

		DefaultTableModel model = new DefaultTableModel(data,columnNames) {
			
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			public boolean isCellEditable(int row, int column) {
				return false;
			}
		};

		// Create a Form Component
		JTable table = new JTable(model);
		table.setModel(model);
		// Setting tables can only select one column
		table.setColumnSelectionAllowed(true);
		JScrollPane pane = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
				JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		frame.add(pane, BorderLayout.CENTER);
		// Set the height of the rows in the table
		table.setRowHeight(30);
    
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	frame.setVisible(true);
}

Every time the data is updated, it only needs to call a method to update the data in the array. In the query part, I call another method to implement the query operation. I know that this is not the best solution. Because the button has been clicked, the table will not be updated. For example, after I added the course, the array although. Updated, but the table will not show, so there is a "superfluous" Update button to update my table, can be said to be pseudo real-time updates.

After passing in the data, there are some restrictions and modifications to the table.

		// Setting table header
		JTableHeader header = table.getTableHeader();
		// Setting table headers does not allow rearrangement
		header.setReorderingAllowed(false);
		header.setPreferredSize(new Dimension(0, 40));
		header.setBackground(Color.BLACK);
		header.setForeground(Color.WHITE);
		header.setFont(new Font("Microsoft YaHei", Font.BOLD, 24));

		// Setting tables can only select one column
		table.setColumnSelectionAllowed(true);
		// Set the height of the rows in the table
		table.setRowHeight(30);
		table.setBackground(Color.WHITE);

		// Set the size and manner of text for each column
		DefaultTableCellRenderer render = new DefaultTableCellRenderer();
		render.setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
		render.setFont(new Font("Microsoft YaHei", Font.BOLD, 24));

The effect of my final curriculum management interface is as follows.

[Img-7ocSZaQn-15682790869 (C: Users Dell AppData Local Temp Course Interface.png)]

HorizontalAlignment(DefaultTableCellRenderer.CENTER);
render.setFont(new Font("Microsoft Yahei", Font.BOLD, 24));

The effect of my final curriculum management interface is as follows.

Topics: MySQL Database Java