Reputation: 1979
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.
TableModel
(I don't quite know how to use this)? I understand that a 2D matrix can be used to set the data beforehand.http://i45.tinypic.com/24pxvmx.jpg
Upvotes: 1
Views: 193
Reputation: 11266
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.
Upvotes: 1
Reputation: 2842
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:
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);
Upvotes: 1
Reputation: 2494
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!).
Upvotes: 1