JTable help (resizing of headers and data source) - java

I am quite new to JTable and am perplexed over the plethora of information on it. There are several questions that I wish could be answered by you guys.
How can I set each cell value individually? Is it through implementation of a TableModel (I don't quite know how to use this)? I understand that a 2D matrix can be used to set the data beforehand.
How can I make it such that the below table's headers are nicely positioned(the entries are fully displayed)? If you look carefully at the first entry in the table, Job Grade is hidden partially. I have searched high and low for solution to this but to no avail.
http://i45.tinypic.com/24pxvmx.jpg

The concept is that you subclass DefaultTableModel with your own class. If you wanted to show a table of jobs for example, you might create a class called JobsTableModel. The JobsTableModel would typically have a collection of objects of type Job which might be passed in to the constructor. Your JobsTableModel has to be able to provide answers to the following questions:
How many rows are there
How many columns are there
What is the name of column X
What is the data type of column X
What is the value in cell (X,Y)
It does this by overriding the following methods:
public int getRowCount()
public int getColumnCount()
public String getColumnName(int columnIndex)
public Class<?> getColumnClass(int columnIndex)
public Object getValueAt(int rowIndex, int columnIndex)
The JTable implementation can then use this information to populate the table without you having to worry about setting the values in each cell. Furthermore, the JTable implementation will automatically give you sorting and column re-ordering for free. If you had to populate each cell directly, these would be a nightmare to work with.
As regards getting width right, I don't believe there's any automatic way of doing this, you just have to use some trial and error. You can set the width of column in pixels as follows:
TableColumn column = table.getColumnModel().getColumn(i);
column.setPreferredWidth(width);

As I commented above, the best thing you can do is go through http://docs.oracle.com/javase/tutorial/uiswing/components/table.html.
But to answer your two specific questions:
1) Yes you need a TableModel. You can create a new JTable by passing it arrays, but it just creates a DefaultTableModel in the background anyway. Look up Model-View-Controller architecture for more info on why you need the model.
2) You can set a preferred width on the columns with something like this
table.getColumnModel().getColumn(1).setPreferredWidth(100);
Which will give column 1 a preferred width of 100 pixels (I think it's pixels... don't quote me on that though!).

JTable is one of the most sophisticated components of Swing. So takes some time to give it a kick start. But when you get the handle of it, you will see that JTable is very strong and flexible. Having said that, let me try to answer your questions.
1)How can I set each cell value individually? Is it through
implementation of a TableModel(I don't quite know how to use this)? I
understand that a 2D matrix can be used to set the data beforehand
JTable uses a TableModel instance for the data. If you have an available array or Vector with pre-loaded data, you can simply use DefaultTableModel. If DefaultTableModel is not flexible enough for your requirements, then do not hesitate to implement your TableModel by extending AbstractTableModel. AbstractTableModel has only the following three abstract methods, so it is really easy to implement a concrete TableModel by extending AbstractTableModel. :
public int getRowCount();
public int getColumnCount();
public Object getValueAt(int row, int column);
2)How can I make it such that the below table's headers are nicely
positioned(the entries are fully displayed)? If you look carefully at
the first entry in the table, Job Grade is hidden partially. I have
searched high and low for solution to this but to no avail.
http://i45.tinypic.com/24pxvmx.jpg
You can find an example for automatically setting column widths here: http://www.exampledepot.com/egs/javax.swing.table/PackCol.html
The code example in this link uses all column data for calculating preferred column width. I guess it can be adapted for your requirement as well.

Related

How to update JTableModel after the sorter has changed the rows order?

I have implemented a JTable, on which I can search entries with a textField.
When a request is made, my code gets the DefaultTableModel, looks for the element in each column and row, and then sets the selection in the table to the line where the element was found.
This works, but it's useless if I sort the table by clicking on any column, because the sorting doesn't seem to update the DefaultTableModel.
The table is part of a bigger project which is extremely complicated and full of dependencies so I cannot post a small example, but I think this will sum it up:
Given a DefaultTableModel A full of non-sorted data about a JTable B, where B.setAutoCreateRowSorter() is true , how does one update B after several/any cloumn-sortings of A?
I have read the docs and also looked into this:
http://www.codejava.net/java-se/swing/6-techniques-for-sorting-jtable-you-should-know
As well as dug a bit into TableRowSorter#addRowSorterListener, but it can only tell me that a column was sorted, not really useful. Unless of course I use what that column is to try and sort all the values in a multidimensional array, then clear the table, then assign everything back.. but clearly this is extremely slow for and not really an option in my case.
Refer to this for the info provided by a RowSorterEvent:
https://docs.oracle.com/javase/7/docs/api/javax/swing/event/RowSorterEvent.html
Can someone point me in the right direction ?
When a request is made, my code gets the DefaultTableModel, looks for the element in each column and row...
So don't search the TableModel. You can use the table.getValueAt(...) method to search for the element in each row/column of the table. The data will be accessed in the currently sorted order of the table.
because the sorting doesn't seem to update the DefaultTableModel.
Correct, only the View (JTable) is updated.
If you want to keep searching the TableModel directly then you need to convert the model indexes to the view indexes whenever you want to invoke a JTable method (ie. selecting a table row). This is done by using the following JTable methods:
int columnColumn = table.convertColumnIndexToView(modelColumn);
int row = table.convertRowIndexToView(modelRow);
There are also methods to convert the view values to the model values.

How to make table with multiple lines per row?

I'm trying to find a way to make a Java table (Swing component) that can wrap the list of columns into multiple lines per row.
/[ Column Hdr A ][ Column Hdr B ]
Hdr |[ Column Hdr C ][columnHdr D ][ColumnHdr E ]
\[ column Hdr F ][COlhdr G][colhdr H][CH I]
/[ Cell 1 A ][ Cell 1 B ]
Row 1|[ Cell 1 C ][cell 1 D ][Cell 1 E ]
\[ Cell 1 F ][Cell 1 G][Cell 1 H][C 1I]
...
Where each column size is independent of any other. IE: I'm not doing spanning or column header grouping. It should retain the column resizing, hiding and sorting features. Drag and drop re-ordering would be nice but isn't necessary.
So I've searched everywhere for something like this. All I've found are various schemes for spanning cells or using fixed width sub-columns. There was one person who claimed to have done it by overriding getRect, but there was no code to look at, so I'm not sure how that would work wrt to resizing or hiding columns, and how would you specify which columns went where?
I've considered just extending TableColumn to include a "sub-row" property but that means also having custom TableColumnModel, JTableHeader and Jtable, AND Jpanel. And I suspect that the renderer and all the LookandFeel UIs would also have to be modified.
An ugly hack that occurred to me is to create one table per sub-row of columns, and then use some form of mutant jpanel to expose the rendered rows interleaved down the y axis. I'm not sure that would work with scroll bars though.
So, does anyone have a neat, concise way to implement this? Any suggestions about how to proceed?
I would hope that you would re-organize the output to be more human readable. While this does fit the data onto the page, a human reading it would have a great deal of problems trying to understand what table cell belonged to what header and which row was which.
Can you create rows with only the 'important' data visible first, then the user could click (or some gesture) to open the more detailed results? Or include multiple cell values together in a multi-line cell to reduce the data to a single row per record?
This gives you two great benefits. First the data will be understandable at a glance, and second, you can use the existing table implementations and save yourself the significant development work.
Finally, have you tried using an HTML widget to display the data? You could create the table as divs in HTML and have the widget wrap the rows. Then you could style the cells for each column to be a fixed width.
I can't offer you any code, but you could look into customizing the table model (class MyTableModel extends AbstractTableModel {...}).
The approach could be to overload the SetValueAt(Object value, int row, int col) method so that writing row 1, col 3 for example would actually write row 2 , col 1 and so on to display multiple lines for 1 line of you data table
Do you indeed need the behaviour which prevents some columns from scrolling?
It is a quite standard usability solution. I would recommend you to come up with a dialog allowing to hide and freeze columns. In this case users could organize the viewport the way they feel best at the moment. Here is an example of scroll freezing.
The second option may well be HTML. You can generate whatever you want this way. Though this solution may have a limitation if you need to edit your data.
Ergonomics uggestions:
Is it acceptable to present headers or data vertically? Sometimes it is better readable.
Basically JTable supports adding swing components including nested tables or cells. please refer to the ff links
com.lang.java.gui discussion
a similar thread

JTable.clearSelection() vs Jtable.getSelectionModel.clearSelection() - When to use what?

I need to cancel all selections within a JTable model object. Java provides this function "clearSelection()" which does, what I need, as far as I understand.
But I am confused why this function can be called on a JTable object as well as on a selection model for a JTable object:
1) mytable.clearSelection();
2) mytable.getSelectionModel().clearSelection();
Both ways work, but I do not understand in what situation a clearSelection() of a SelectionModel (like at 2) ) would make any sense. As far as I understood SelectionModels, they are used to decide what kind of selections a JTable allows. I use the SelectionModel to only allow a Selection of exactly one row
//allow only one row to be selected
mytable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
Which way is to be preferred in what kind of situation? Is there a good reason not to use way 1?
I would be glad if anyone has some beginner friendly explanation for that. Thx in advance.
Here is the implementation of JTable#clearSelection()
public void clearSelection() {
selectionModel.clearSelection();
columnModel.getSelectionModel().clearSelection();
}
As you can see, there is two ListSelectionModel which are cleared, because you can select column and/or row and/or cell.
From Oracle tutorial :
JTable uses a very simple concept of selection, managed as an
intersection of rows and columns. It was not designed to handle fully
independent cell selections.
A ListSelectionModel handle all aspect of the selection such as which row is selected, how can we select some rows, etc... Not only the kind of selection !
More information in the Oracle JTable tutorial
Usually when you see two methods like that it is because the table will invoke the SelectionModel.clearSelection() method for you. So the table method is a convenience method.
In this case the actual code is:
public void clearSelection()
{
selectionModel.clearSelection();
columnModel.getSelectionModel().clearSelection();
}
So both the row and column selection models are cleared.

fireTableDataChanged has no effect on JTable

I have a problem with updating my JTable in Java Swing.
The datas I want to show changes a few times per second and I look for a efficient way to update the data in the JTable.
I used the method setModel() to update the data, and it works, BUT it has 2 drawbacks:
If the user resize the table columns in the header, then he wil get about 10 exceptions (I think because the model is no longer available because it changes a few times per second)
The information of the length of the resized column (in Pixel) get lost, every time the data (and so also the TableModel) changed.
For the TableModel i use my own model ResultSetTableModel which extends AbstractTableModel.
This ResultSetTableModel has a method setResultSet(ResultSet rs) and overwrites the method getValueAt(x,y)...
As I told if I set a new ResultSet to my ResultSetTableModel and then add it to the JTable by the method setModel(resultSetTableModel) it works, but it has the 2 drawbacks i told.
So I think I can solve this problem with the method fireTableDataChanged() but I tried many possibilities but get no change.
Do you know, where I have to place the fireevent?
At the moment I try this, but it doesn't work and I don't know why:
private ResultSetTableModel resultSetTableModel;
private DataFetcher dataFetcher;
private JTable table;
...
//works fine
public void initaialUpdateTable() {
resultSetTableModel = new CachingResultSetTableModel(dataFetcher.getRS());
table.setModel(resultSetTableModel);
}
//does not work
public void updateTable(){
resultSetTableModel.setResultSet(dataFetcher.getRS());
resultSetTableModel.fireTableDataChanged();
}
If I every times call initaialUpdateTable(), it works fine, but i want that just the data changes and not the whole model
Thanks for your answers
Michael
but i want that just the data changes and not the whole model
Hmm how can I..., there is no only one ...
1) Something that you can see in the GUI is TableView, only presentation layer, and all data are always stored in the TableModel
2) If you don't declare any TableModel, this doesn't mean that there isn't exist, still are there DefaultTableModel
3) Your private ResultSetTableModel resultSetTableModel; must extend AbstractTableModel,
4) If you'll to block any of fireXxxXxxChanged();, then no changes goes back to the TableView,
5) Basic stuff here, start with fireTableCellUpdated(row, col);
EDIT
More informations about TableModels here, here or search for ResultSetTableModel, TableFromDatabase
Sorry I don't have a concrete answer to your question, but I couldn't quite fit all that I want to say in a comment.
I used the method setModel() to update the data
You should probably stick to a single model that provides methods to modify its data. These methods should appropriately notify listeners when something has changed.
Here's a really awesome article that shows how to implement a high-performance, multi-threaded table with frequently changing data. You could probably use a lot of the example source code.
How to Create Frequently-Updated JTables that Perform Well

Working with data and a JTable?

I have a JTable that I want to use to display some data (a String and a Boolean in each row). The data is maintained by my own class. Is there some way to bind the data model to the JTable, so that when I add to the model, the JTable is dynamically updated and when I remove something from the model, the row is removed from the JTable?
I have previously worked with Flex and Actionscript, and this is very easy to do there with data binding, so I'm just wondering how it's done in Java.
Thanks.
You will need to have your dataset implement the TableModel interface. if you do that then you can apply it to the JTable. If you extend AbstractTableModel you will inherit some event firing methods that your table will handle and will update the view. see this tutorial. Note that the default implementation of JTable will renderer your data for you, and if a Boolean is found, it will show up as a check box.
You'll probably find both the Java JTable tutorial and the JTable API documentation helpful in understanding how JTable works, but otherwise here's a quick rundown.
The premise of a JTable is that it is paired with an object that implements the TableModel interface, which by default is an instance of DefaultTableModel. The table model object is made up of a list of columns, each of which has its own data type (String and Boolean in your case), and a list of rows containing the actual data for the table.
Whenever the JTable is drawn by the swing drawing code, it repeatedly calls the method:
public Object getValueAt(int row, int col)
Thus, when you add data to the table model, it is always rendered as you expect in the next screen refresh (dynamically).
The only thing you really need to worry about, then, is getting the data from your object into the table model and back out again. Other than that, JTable takes care off all the heavy lifting.
While implementing TableModel is easy enough for simple cases, you might want to consider a true binding approach (my favorite is Glazed Lists - watch the 30 second video on how easy this is and you'll be won over). Beans Binding (now Better Beans Binding) also has an implementation of observable lists that might be useful (although I much prefer the Glazed Lists approach)

Categories

Resources