Swing Components / Tables
Subtitles of the Movie
You can display large tables of data by using combinations of the BoxLayout manager to form the rows and columns, but you can also use the JTable component and let it do the layout work for you. To do it, you need to use both the JTable component and the JScrollPane. The two of them work together. You can't reliably display a JTable without using JScrollPane. You declare the column headings as an array of strings like this and you declare the data for each cell in a two-dimensional array like this. This array can hold any object that can be converted into a strong for display. In Java, every object has a string form so any kind of object can be used. In this example, the first column is made up of integer objects and all the rest are strings. You use the data and column array declaration on the constructor. This fills the table of the right size according to the size of the data arrays. Then you set the size of the display of the components. The size you choose will determine the height, the number of rows displayed and the overall width. In the width, all the columns are displayed. The width is divided evenly among the columns so they'll all be the same size. This method call makes certain that the table expands to fill the space assigned to it. This is where the partnership with the JScrollPane comes in. You create a JScrollPane using the table as an argument on the constructor and it is this pane that then is added to this display. There's the table with the column headings across the top. Not all the rows are displayed, but the scroll bar on the right allows you to see all the rows. Notice that the columns are all the same width. You can actually vary that but in an odd sort of way. Now this program is exactly like the last one except that a few lines of code have been added. This for loop has been inserted. Before the loop, a call is made to get the column model. Inside the loop, the column model is used to retrieve the table column object for each of the columns. Inside the loop, the width of the first two columns is set to 50 and the width of the other columns are all set to one hundred. Now, these settings are relative, not absolute. The actual column width is set automatically, but the width allocated to each one is relative to the width numbers assigned to them. That is the first two columns will only be half as wide as the others. Now, here's the result. Here it is. The same table as before but the first two columns are only half the width of the other three. You can set it up so the user is able to select data, but doing so isn't as straightforward as maybe it should be. To receive selection events, you need to implement the list selection listener interface. Now, this is the same program again with a few lines of code added. This code was added so the program will respond when the mouse selects any individual cell's data. These two lines of code set up the JTable component so it will respond to the mouse one data cell at a time. These two lines of code extract the selection model from the table and add this object to the list of those notified when the row number changes. In similar fashion, the selection model of the column model is added to the list so this object then is notified when the column number changes. Here is the method that is called each time a row or column number different from the current one has been selected. Notice this method call here. This method is called once when the mouse button is pressed and again when it's released. The get value adjusting method is true only when the mouse button is pressed, so this call eliminates duplicates. If duplicates don't bother you, you can leave this line out. Now, this method is called every time a row number changes and again every time a column number changes. So when they both change, it's called twice. Again, if duplicates don't bother you, you can leave this out. This example just prints the row and column numbers and the contents of the table at the location. Here's how it works. Any cell selection you make is reported to the program. By the way, it is possible to read information entered by the user. The string you read from the array will be the string as entered by the user. The JTable component can be used for both displaying and entering data, but some components are only for displaying information. The next lesson shows you an example of that in the form of a progress bar.
Tutorial Information
| Course: | Java 6 |
| Author: | Arthur Griffith |
| SKU: | 33858 |
| ISBN: | 1-934743-59-3 |
| Release Date: | 2008-02-29 |
| Duration: | 7 hrs / 92 lessons |
| Work Files: |
Yes |
| Captions: | Available on CD and Online University |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |
VTC Sign up & Benefits
- Unlimited Access
- 81,350 Video Tutorials (20,800 free)
- Video Available as Flash or QuickTime
- Over 782 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 