I have a list, which is need to be shown as data. But JTable with accepts data as Object[][], but will give Object[].
How do I display only one column in a table?
Perhaps try the following...
DefaultTableModel model = new DefaultTableModel();
model.addColumn("MyColumnHeader",dataArray);
JTable table = new JTable(model);
Related
I tried to group JTable header and for that I want to get the DefaultTableModel of current JTable. But when I tried to retrieve the table model like this:
DefaultTableModel dm=(DefaultTableModel) tblTet.getModel();
It shows the following exception:
org.jdesktop.swingbinding.JTableBinding$BindingTableModel cannot be
cast to javax.swing.table.DefaultTableModel
Because I am using JTableBeansBinding.
Does anyone know how to solve this problem (for retrieving DefaultTableModel)?
According to the JavaDoc for BindingTableModel, the class doesn't extend DefaultTableModel. Rather, it implements TableModel interface. This means that you cannot cast to DefaultTableModel, only to TableModel:
TableModel dm=(TableModel) tblTet.getModel();
you have to create your table like this :
String[] columnNames = {"Row",
"Category",
"From Date",
"From Time",
"To Date",
"To Time",
"Description",
"Doc"};
Object[][] data = {};//Table Rows
table.setModel(new DefaultTableModel(data,columnNames));
instead of:
table=new JTable(data,columnNames);
then you can cast your table model to defaultTableModel.
This question already has answers here:
JComboBox in a JTable cell
(4 answers)
Closed 7 years ago.
I am trying to add two jcombobox in two row cells using DefaultTableModel addrow() method. Like this:
DefaultTableModel dtm = new DefaultTableModel();
JComboBox jcb1 = new JComboBox;
JComboBox jcb2 = new JComboBox;
JComboBox[] row={jcb1,jcb2};
dtm.addRow(row);
myTable.setModel(dtm);
It happens that the table takes the whole thing and displays into each cell the JComboBox.toString, the properties of each JComboBox instead of displaying the combobox itself the object. Can anyone help me? Thanks
To set the combo box as an editor on a column, you have to get the column using:
TableColumn comboColumn = table.getColumnModel().getColumn(1);
Instead of 1, put the actual column index (0 is for the first column, 1 is for the second etc.). And then:
comboColumn.setCellEditor(new DefaultCellEditor(comboBox));
Also, consider creating your own class that will extend AbstractTableModel and then using your class as table model instead of DefaultTableModel - maybe you don't have to do this, but it will give you more flexibility with defining behavior of jTable.
the size of table limit is set as static limit and i want to change that to dynamic
here jtable & object declared.
public JTable issuetable = null;
static Object[][] data;
here is my jtable
public JTable getIssues() {
issuetable = new JTable();
String[] colName = {"Member", "Book", "Issue Date", "Return Date ",
"Remarks" };
List<Issue>issues=ServiceFactory.getIssueServiceImpl().findAllIssue();
the size of issuedata is limited to 100000 .. i want to change the limit to dynamic..
data=new Object[issues.size()][100000];
for(Issue issue:issues){
data[i][0]=issue.getMemberId().getName();
data[i][1]=issue.getBookId().getName();
data[i][2]=issue.getIssueDate();
data[i][3]=issue.getReturnDate();
data[i][4]=issue.getRemark();
data[i][5]=issue;
i++;
}
if u know answer please share here..
In your previous question, You were using a DefaultTableModel. Keep in mind, a TableModel is a data structure in itself. There is no need at to store the data in two data structures, i.e. your data[][] and the DefaultTableModel. The underlying structure of DefaultTableModel is a dynamic Vector of Vectors.
What you can do is this. Just declare your DefaultTableModel with 0 rows, using this constructor
public DefaultTableModel(Object[] columnNames, int rowCount)
Then just add rows dynamically to the structure with
public void addRow(Object[] rowData) - Adds a row to the end of the model. The new row will contain null values unless rowData is specified. Notification of the row being added will be generated.
So basically, your declaration will be like this
String[] colName = {"Member", "Book", "Issue Date", "Return Date ", "Remarks" };
DefaultTableModel model = new DefaultTableModel(colName, 0);
JTable table = new JTable(model);
Then just add rows like
String member = "Stack";
String book = "overflow";
Data issueDate = date;
....
Object[] row = { member, book, issueDate, returnDate, remarks };
DefaultTableModel model = (DefaultTableModel)table.getModel();
model.addRow(row);
Please read the DefaultTableModel api documentation to see more constructors and methods available
Instead of an array, use a dynamically resizable data structure in your implementation of AbstractTableModel. This EnvDataModel is an example that contains a Map<String, String>.
Instead of copying all the data from your List to the DefaultTableModel you can use your List as the data structure for a custom TableModel. Then you can add/remove Issue object from this TableModel.
See the JButtonTableModel.java example from Row Table Model for an simple example of how the RowTableModel can be extended to give you this functionality.
Using this approach the data is only ever in one place and you can access Issue objects directly from the TableModel.
I have a JTable whose cells are editable. However if i edit a cell and refresh the table. The changes are not saved. This is how i have defined the table:
String [] columnNames = {"Application number",
"Name",
"date",
"fileLoc",
"Country"};
//Data for the table
String [][] data = table.tableArray;//tableArray is an array of array of strings.
TableModel model = new DefaultTableModel(data, columnNames);
JTable mainTable = new JTable(model);
model.isCellEditable(data.length,columnNames.length);
contentPane.add(new JScrollPane (mainTable));
I've had a look online but can't seem to find any advice on saving the changes made to a cell. Any help would be very welcome!
I guess i'm not refreshing the table as such. I use frame.dispose()
and then create a new frame with the table in.
then you lost all changes made in the current DefaultTableModel
don't top create a new JFrame with a new DefaultTableModel and a new JTable
all changes from TableCellEditor are changes dispayed in JTables view
JTable (with its model) is prepared for this job, don't to reacreate these Objects on runtime
DefaultTableModel has implemented all notifiers, there no needed to override any others events, nor to fireXxxXxx() programatically, but those events are required for AbstractTableModel
define add this code line for your JTablemainTable.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
DefaultTableModel respesenting your required 2D array
I have added a combobox in a JTable, the adding code as follows:
Vector<String> header = new Vector<String>();
Vector data = new Vector();
String[] h = new String[]{"Music", "Movie", "Sport"};
header.add("Code");
header.add("Name");
header.add("Salary");
header.add("Hobby");
loadData(); // Add some data to the table
DefaultTableModel tblModel;
tblModel = (DefaultTableModel) this.tblEmp.getModel();
tblModel.setDataVector(data, header);
// Adding combobox to the last column
TableColumn hobbyColumn = tblEmp.getColumnModel().getColumn(3);
hobbyColumn.setCellEditor(new MyComboBoxEditor(h));
Things worked fine until I dynamically add a new row to the table using the code:
Vector v = new Vector();
v.add("E333");
v.add("Peter");
v.add(343);
v.add(""); // This last colum is the combobox so I put it as ""
data.add(v);
tblEmp.updateUI();
Data is added to the table but the combobox in the last column cannot be selected anymore. The combobox is still displayed when I click on the row but cannot select a value.
How can I handle this problem, please?
Never use the updateUI() method. Read the API to see what this method actually does. It has nothing to do with changing the data in a model.
JTable already supports a combo box editor so there is no need to create a custom MyComboBoxEditor. Read the JTable API and follow the link to the Swing tutorial on "How to Use Tables", for a working example of using a combo box as an editor.