as many other persons, I want my JTable being actualized after adding rows.
Here you can see the fragments of my code.
ArrayList<> foundR = new ArrayList<FoundResult>();
JTable resultsTable = new JTable();
AbstractTableModel resultsModel = new FoundResultTableModel(foundR);//<- model which contains FoundResults as rows
JScrollPane scroller = new JScrollPane();
resultsTable.setModel(resultsModel);
scroller.add(resultsTable);
this.add(scroller, BorderLayout.CENTER);// a big panel which contains scroller and other components
Then a add ActionListener that uses following method:
foundR.clear();
foundR.addAll( ...);//<- so the foundR ArrayList is refreshed
resultsModel.fireTableDataChanged();
resultsModel.fireTableStructureChanged();
rowCount of results Model shows that the model is refreshed (the number of rows differes from the previeous variant), but the table still doesn't appear. I tried to insert resultsTable.repaint() but it also dind't help.
UDP(NB) I discovered, that the problem is concentrated in this scroller Panel.
If I add the table directly to the big panel it is refreshed (but I cannot see all the results, since I cannot scroll down)
this.add(resultsTable, BorderLayout.CENTER);
If I use a scroller, nothing is shown. Do you know why?
Just recreate the model and set it to your table.
foundR.clear();
foundR.addAll( ...);//<- so the foundR ArrayList is refreshed
AbstractTableModel resultsModel = new FoundResultTableModel(foundR);
resultsTable.setModel(resultsModel);
Add this to your code,
resultsTable.revalidate();
Since your model has been changed but the change in model should be notified to the table also.
Related
Ia m new to vVadin. I created one project with grid with two columns but i want to add one Textfield column and one checkbox column and check all checkboxes when click on header checkbox.
List<Person> people = Arrays.asList(
new Person("Nicolaus Copernicus", 15),
new Person("Galileo Galilei", 15),
new Person("Johannes Kepler", 15));
TextField txt =new TextField();
CheckBox chk=new CheckBox();
// Create a grid bound to the list
Grid<Person> grid = new Grid<>();
grid.setItems(people);
grid.addColumn(Person::getName).setCaption("Name");
grid.addColumn(Person::getAge).setCaption("Year of birth");
grid.addColumn(Person-> new TextField());
layout.addComponents(grid);
setParent(layout);
can anyone suggest me.how to add those two columns
Selection via check boxes works with the multi selection mode, see docs. On the same page you can read about ComponentRenderer which allows to put any component in a column. Note that this feature is available since Vaadin 8.1. released few days ago.
Im currently using the following code
tableModel.setDataVector(data, columnNames);
jTable2 = new JTable(tableModel);
JOptionPane.showMessageDialog(null, new JScrollPane(jTable2));
and when I execute it, the resulting size looks like below
How can I make message dialog bigger, so that we can see the column headings? I want to remove those '...'
Thanks!
Set a preferredSize to the JScrollPane ?
I use the following code to display a table.
final Vector<Vector<String>> vct = refreshDatas();
final Vector<String> Cols = new Vector<String>();
Cols.add("OID");
Cols.add("Name");
this.tmodel = new DefaultTableModel(vct,Cols);
this.table.setModel(this.tmodel);
this.table.setBounds(50, 200, 300, 250);
this.table.setSize(200, 200);
this.table.setVisible(true);
but only the contents is displayed. The header OID and Name are not displayed.
See the 'Adding a table to a container' section in the table tutorial.
If you add the table yourself, you must make the headers visible as well. If you add your table to a scrollpane, the scrollpane will take care of this for you.
Copy-paste from that tutorial:
If you are using a table without a scroll pane, then you must get the table header component and place it yourself. For example:
container.setLayout(new BorderLayout());
container.add(table.getTableHeader(), BorderLayout.PAGE_START);
container.add(table, BorderLayout.CENTER);
Sidenote: it should not be needed to call setBounds nor setSize. Just make sure your parent Container has a decent LayoutManager and it will take care of the size
just add the table in to the JScrollPane, It automatically display the table headers.
Using the Visual Editor in Eclipse I started a Swing UI containing a table (JTable) with 2 columns (JTableColumn). Adding data to the table this way:
final DefaultTableModel model = (DefaultTableModel) this.jTable.getModel();
model.addRow(new Object[] {"Column 1", "Column 2"});
generated an ArrayIndexOutOfBoundsException. I solved this by setting the number of columns of the model backing the table:
model.setColumnCount(this.jTable.getColumnCount());
But after this call, the column headers of the table I defined using the UI editor, are changed to "A" and "B". Now I'm wondering if I should go on and correct the generated code like I did, or is there a better way to build UI's with Visual Editor?
To be complete, this is the generated code to define the table and columns:
private JTable getJTable() {
if (this.jTableSongs == null) {
final TableColumn tableColumn1 = new TableColumn();
tableColumn1.setHeaderValue("Header 1");
final TableColumn tableColumn2 = new TableColumn();
tableColumn2.setHeaderValue("Header 2");
this.jTableSongs = new JTable();
this.jTableSongs.addColumn(tableColumn1);
this.jTableSongs.addColumn(tableColumn2);
}
return this.jTable;
}
I don't know about this specific problem (table columns) but in the past i had similar problems with VE.
If you don't have the number of columns in the Property View, you have to specify it programmatically, editing the code, but VE will not remove hand added code.
or is there a better way to build UI's
with Visual Editor?
The problem with using code generators is that you spend more time learning the IDE and not learning Java. The better approach is to use the IDE for debugging and so on and build the GUI's yourself so you are in full control and the code can be moved from one IDE to another.
I don't know what the generated code should look like but the following looks inconsistent:
this.jTable = new JTable();
this.jTableSongs.addColumn(tableColumn1);
this.jTableSongs.addColumn(tableColumn2);
A jTable variable is created (and returned from the method) but the columns are added to jTableSongs. So it looks to me like jTable has 0 columns which could cause an Exception.
I have this structure:
<JFrame>
<JPanel backgroundcolor = "pink">
<JScrollPane>
<JTable>!!!Data here !!!</JTable>
</JScrollPane>
</JPanel>
</JFrame>
How do i stretch the ScrollPane it to cover the full window without using setSize?
This is how it looks like now:
alt text http://img22.imageshack.us/img22/8491/17747996.png
Thanks!
Mmmph! Nobody offered a simple solution such as using BorderLayout as layout manager for my JScrollpane container!
I am not familiar with the XML file format.
If it is coded, you may need to code something like this:
JScrollPane1 = new JScrollPane();
JPanel1.add(JscrollPane1);
JScrollPane1.setBounds(5,29,636,122);
JTable1 = new JTable();
JPanel1.add(JTable1);
JScrollPane1.setBounds(5,434,553,3097);
JScrollPane1.setViewportView(JTable1);
Use setPreferredScrollableViewportSize() and a suitable layout.
Edit: You'll also need setFillsViewportHeight(), as discussed in Adding a Table to a Container.