Search This Site
Oct 12 2008 16:34 UTC | ||||||||||
XML ManagerStandard Edition |
• Try the Demo! • Download the FREE Trial! • Product Home Page |
• Getting Started Guide • How You Can Save Money • All Your Purchase Options |
This Swing GUI example shows you how to:
To compile and run the Java programs in this example, either import them into your IDE
(say, Eclipse or NetBeans), or follow
the standard manual compilation instructions.
When running the programs, run them in the same folder as this README.htm file, so that
they can find their input files.
Let's outline our application. It's a graphical application that looks like a very simple spreadsheet. At the top we'll have two buttons: [Load CSV File] and [Save CSV File]. When you load a CSV file, we'll display the data in the file as a table underneath the buttons.
Most of the code in this example is the Java Swing code to setup the graphical user interface elements. If you'd like to know more about this code, take a look at the Sun Swing Tutorial. We'll focus on the CSV Manager code here.
First, let's create a CsvManager object to do our CSV loading and saving.
iCsvManager = new CsvManager(); iCsvSpec = iCsvManager.getCsvSpec(); iCsvSpec.setProperty( "TableModel.dataHasHeaders", iHasHeaders ); iCsvSpec.setProperty( "TableModel.editable", true );
In this code, we're assuming that there is a iCsvManager member variable
that we are using to store our CsvManager object. The main thing that we
are doing is setting the options that we'll need for the data in our JTable.
First, we set the TableModel.dataHasHeaders setting using the value of the iHasHeaders
variable. This is another member variable we're using, which is initially set to true.
This means that the first line of the CSV file will be considered to be the data headers. The data fields in the
the first line will become the column names of the table. In our GUI, we'll also include a tick box
to turn this setting on and off.
Second, we set the TableModel.editable setting to true. This makes the TableModel
returned by CSV Manager editable. This in turn means that the user can edit cells in the JTable
that we will display.
So what do we do when the user clicks on the [Load CSV File] button? Here's the code:
JButton loadB = new JButton( "Load CSV File" );
loadB.addActionListener( new AbstractAction() {
public void actionPerformed( ActionEvent e ) {
File csvF = askForCsvFile( true );
TableModel tm = iCsvManager.loadTableModel(csvF);
iTable.setModel( tm );
}
} );
(This code follows the standard Swing idiom for creating a button and associating an action with the button using an inner class.)
First, we ask the user for the location of the CSV file. The askForCsvFile method handles this with the
built-in Swing file dialog window. Then we use the CsvManager.loadTableModel method to actually load the file.
Finally, we take the TableModel that CSV Manager has created for us, and we pass it to the JTable that we are
displaying (contained in the iTable member variable).
Now the user has a chance to edit the data in the displayed table. When they have finished making their changes, they will want to save the CSV file. Here's how we handle that:
JButton saveB = new JButton( "Save CSV File" );
saveB.addActionListener( new AbstractAction() {
public void actionPerformed( ActionEvent e ) {
File csvF = askForCsvFile( false );
TableModel tm = iTable.getModel();
iCsvManager.saveTableModel( csvF, tm );
}
} );
This works very much the same as the case for loading. Except that we get the TableModel out of the
JTable, and then we save it using the CsvManager.saveTableModel method.
We've left out all the error checking code from these examples. If you want to see all the code, take
a look at the LoadJTable.java file.
We've also included a sample csv file, states.csv, that you can use
to test out the application.
Here is a list of all the files used in this example. Note that the actual source code is slightly longer than the examples above, which have been abridged for clarity.
Feel free to experiment with these classes and see what happens. You can also use them as the basis for your own Java Beans solution.
Please feel free to email us at examples@ricebridge.com if you have any questions or comments about this example.
Got a question for us?
Just Ask!
$15 Gift Certificate for every bug you find.