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 ?
Related
I am using the org.eclipse.swt.widgets.Combo class and I am doing the following
Combo myCombo = new Combo(container, SWT.READ_ONLY);
myCombo.add("1");
myCombo.add("2");
//later on
myCombo.setText(""); //will not work because READ_ONLY
The user will choose an element of the combo, and I am providing a reset button where I want the value to be set to null. However, according to the javadoc, the setText method is ignored when the receiver is READ_ONLY. I like the Combo being read only because I only want the user to select what I provide.. But I want to set the value back to null or "" if possible. Can I do this with a the read only receiver? Or what is another good way of doing this?
Thanks!
Use combo.deselectAll() to reset the selection.
Alternatively you could use:
combo.deselect(combo.getSelectionIndex());
In both cases getSelectionIndex() will return -1 afterwards.
These methods appear as if the Combo supports multi-selection, which it doesn't. However strange they may appear, they do reset the selection.
The best way is to use ComboViewer
List<String> input = new ArrayList<String>();
input.add("1");
input.add("2");
combo = new ComboViewer(container, SWT.READ_ONLY);
combo.setLabelProvider(new LabelProvider());
combo.setContentProvider(ArrayContentProvider.getInstance());
combo.setInput(input);
And to clear it
combo.setSelection(StructuredSelection.EMPTY);
Regards
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.
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.