java swing mvc - irányítástechnika és informatika tanszék · java swing mvc author: péter...

6
BME Department of Control Engineering and Information Technology Software laboratory 3. 2011. 1 Java Swing MVC Author: Péter Budai, BME IIT, 2011. The first four tasks must be solved by the end of the lesson. The following URL contains the API description of the classes to be used: http://download.oracle.com/javase/6/docs/api/ The following tutorial may be a great help: http://download.oracle.com/javase/tutorial/uiswing/components/table.html 1 Opening project Download the skeleton of the Eclipse project from the web site of the laboratory. This is a ZIP archive which can be directly imported into Eclipse. In order to do that select the menu item File/Import... then pick Existing projects into workspace! In the next windows look for the downloaded file then click Finish (see below). The imported project contains two Java files and a .dat file which are part of a simple student administration program. The students.dat file contains the student data (name, neptun, signature, mark) saved with Java serialization. One of the Java classes (StudentFrame) realizes the user interface of the application which currently only loads the student data from the .dat file, shows an

Upload: vuphuc

Post on 10-Apr-2018

217 views

Category:

Documents


1 download

TRANSCRIPT

BME Department of Control Engineering and Information Technology Software laboratory 3. 2011.

1

Java Swing MVC

Author: Péter Budai, BME IIT, 2011.

The first four tasks must be solved by the end of the lesson.

The following URL contains the API description of the classes to be used:

http://download.oracle.com/javase/6/docs/api/

The following tutorial may be a great help:

http://download.oracle.com/javase/tutorial/uiswing/components/table.html

1 Opening project

Download the skeleton of the Eclipse project from the web site of the laboratory. This is a ZIP

archive which can be directly imported into Eclipse. In order to do that select the menu item

File/Import... then pick Existing projects into workspace! In the next windows look for the

downloaded file then click Finish (see below).

The imported project contains two Java files and a .dat file which are part of a simple student

administration program. The students.dat file contains the student data (name, neptun, signature,

mark) saved with Java serialization. One of the Java classes (StudentFrame) realizes the user

interface of the application which currently only loads the student data from the .dat file, shows an

BME Department of Control Engineering and Information Technology Software laboratory 3. 2011.

2

empty window, and saves all data if the window is closed. The StudentData class is for storing the

student data in memory.

Examine the code of the application, then execute it. You should get the following:

Important: only modify the source code where the comments do not forbid this!

2 Creating the table

Add a new table (JTable) to the center area of the window (BorderLayout.CENTER)! Make sure the

table occupies the whole window (setFillsViewPortHeight method), and it should be scrollable! In

order to do this it must be placed into a scroll pane (JScrollPane).

To supply data to the table its data source must be specified. Use the data object inside the class

StudentFrame as data source. (Pass it as a parameter to the constructor of JTable or set it later using

the setModel method).

The StudentData class itself cannot serve as a data source. It must implement the TableModel

interface to do this. Suggestion: Instead of realizing this interface, make SudentData as a subclass of

the AbstractTableModel class! This way you only have to implement three of the methods, the

others are implemented by the superclass.

Note: The table has 4 columns (name, neptun, signature, mark) and as many rows as many students

are in the students list.

BME Department of Control Engineering and Information Technology Software laboratory 3. 2011.

3

If you did everything right, the result should look like the following:

3 Editing cells

Make the last column editable! To do this override the isCellEditable and the setValueAt method of

the class AbstractTableModel.

Execute the application, modify a cell value, exit the application and start it again! If you did

everything right, the modified value should reloaded when the application is restarted.

4 Modifying the data model

Make it possible to add new students to the table!

Add a new panel (JPanel) to the bottom of the window (BorderLayout.SOUTH), on which two labels

(JLabel) and two text fields (JTextField, 20 and 6 characters wide) should appear for the name and

neptun code. There should also be a button (JButton) with the caption “New Student”!

BME Department of Control Engineering and Information Technology Software laboratory 3. 2011.

4

Suggestion: The JTextFields containing the name and the neptun code should be added as attributes

to the window class (e.g. nameField and neptunField), since they will be needed in the action

listener of the button! The layout of the panel can be the default FlowLayout.

The StudentData class should be extended with the following method:

public void addStudent(String name, String neptun) { ... }

This method should add a new student to the student list. The name and neptun code are passed as

parameters to the method. The signature should be false and the mark should be 0 (zero) by default.

The table (view) should be notified that the student list (model) has been changed. This can be done

by calling one of fireXXX methods of the AbstractTableModel superclass. Which method should be

used?

Add a new event handler (ActionListener) inside the StudentFrame class to the button! When the

button is pressed, a new student should be added to the model with the name and neptun specified

in the text fields.

If everything is done correctly, the new student will appear in the last row in the table:

BME Department of Control Engineering and Information Technology Software laboratory 3. 2011.

5

5 Customizing the columns

Modify the program so that the headers of the table columns contain Name, Neptun, Signature,

Mark, respectively. To do this, override the getColumnNames method of AbstractTableModel.

Also override the getColumnClass method to achieve the following column formats (the third

column should contain checkboxes for the boolean values, the fourth column should contain the

marks justified to the right):

Suggestion: To return the types of the columns the appropriate class should be returned. This can be

done by writing the .class reserved word after the name of the class. For example, String.class

Boolean.class and Integer.class.

6 Sorting

Modify the application so that if we click to the headers of the columns, the rows will be sorted

based on this column. If we click again on the same column header, the rows should be sorted

descending. If we click a third time, the rows should be sorted increasing again. Etc.

To do this, a sorter object (TableRowSorter) should be specified (setRowSorter) to the JTable.

Hint: The solution is easier than you would expect. If you have any problems, look at the JTable

Tutorial! (The link for this is at the beginning of this document.)

BME Department of Control Engineering and Information Technology Software laboratory 3. 2011.

6

7 Customizing the cells

Modify the application so that the rows have colored backgrounds. If a student passed the subject

the row should be green. If the student failed the subject (i.e. he has no signature or he has a mark

less than two), the row should be red:

To do this you should create a StudentTableCellRenderer class inside StudentFrame implementing

the TableCellRenderer interface. An instance (or multiple instances) of this class then should be

assigned (setDefaultRenderer) to the JTable.

The getTableCellRendererComponent method of StudentTableCellRenderer should return the Java

Swing component which is used to present the content of the cell.

Hint: Since there are three different column types it is recommended to create a wrapper for the

original default renderer (table.getDefaultRenderer()) and only modify the component returned by

this wrapped renderer:

class StudentTableCellRenderer implements TableCellRenderer {

private TableCellRenderer renderer;

public StudentTableCellRenderer(TableCellRenderer defRenderer) {

this.renderer = defRenderer;

}

public Component getTableCellRendererComponent(JTable table,

Object value, boolean isSelected, boolean hasFocus,

int row, int column) {

Component component = renderer.getTableCellRendererComponent(

table, value, isSelected, hasFocus, row, column);

// Find the appropriate student from the data field of

// StudentFrame, determine if he has passed or failed the

// subject, then set the component’s background color:

// component.setBackground(...)

return component;

}

}

Be aware that the sorting may disrupt the indexing of the rows. The real row index can be calculated

using the appropriate RowSorter object: table.getRowSorter().convertRowIndexToModel(row)