Changing the Size of JTables

plusminusAfter linking up your JTable with a database through JDBC (see previous post here), you might find the need to add and remove rows to your table through the application GUI.

This can actually be done with relative ease.

First, you’ll need a way to invoke the add row/remove row feature.  The way you do this is purely a GUI design issue, so I’ll leave that up to you.

As for my own application, I created a small toolbar at the bottom of a JDialog that contained two JButtons; one for plus, one for minus.

An ActionListener was then added onto each button.  The constructor of the ActionListener will take in a JTable and a copy of the extended AbstractTableModel. When the user clicks the “plus” button, the ActionListener would then invoke the addRow() method on the AbstractTableModel.  When the user clicks the “minus” button, the ActionListener would then invoke the removeRows() method on the AbstractTableModel.  Optionally, we can pass in the currently selected rows and delete all of the currently selected rows.

Database

Adding Rows

The addRow() method is relatively easy to implement in your AbstractTableModel.

First, we’ll have to create a new Object[][] array with an extra row and copy the table contents into the new array.

Then, we’ll set the re-assign the table contents as the new array, and then invoke a refresh request.

public void addRow() {
	Object [][] nContent = new Object[content.length + 1][getColumnCount()];

	System.arraycopy(content, 0, nContent, 0, content.length);

        content = nContent;
	fireTableRowsInserted(0, content.length);
}

Removing Rows

Removing rows is a little bit trickier.  The JTable API allows you to retrieve the number of rows that are selected at any given time.  With this in mind we are able to remove all of the currently selected rows in our JTable.  The JTable‘s getSelectedRows() method will return an int[] array listing the row indices which are currently selected in the JTable.  We will take this array and pass it into our removeRow() method.

public void removeRow(int[] rowIndex) {
	Object[][] nContent = new Object[content.length - rowIndex.length]
                                                      [getColumnCount()];

	for (int row : rowIndex) {
		content[row] = null;
	}

	int i = 0;
	for (int j = 0; j < content.length; j++) {
		if (content[j] != null) {
			nContent[i] = content[j];
			i ++;
		}
	}

	content = nContent;

	fireTableRowsDeleted(0, content.length);

}

First, we’ll go through rowIndex and nullify all rows in content mentioned in the list.

Then, we’ll go through the the content array and copy over non-null rows to our new array nContent.

Finally we’ll set the current table contents to the new array nContent and invoke a refresh by calling fireTableRowsDeleted.

Done!

And there we have it, now you can easily add/remove rows through the GUI.  Now this guide assumes that you are storing all of your data in an Object[][].  If you’re using some sort of List, your life will be a lot easier as you won’t have so much trouble compacting your array.

This entry was posted in Java, Tech and tagged , , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>