I'm trying to create a vertically scrolling table within a dialog, without success. When I create the table, it auto-expands to whatever height it needs to be regardless of screen size/resolution. Lots of data creates a table that extends well below the bottom of the screen.
My code goes something like this (not verbatim, may be a type-o or two):
final Table table = new Table(parent, SWT.SINGLE |
SWT.FULL_SELECTION | SWT.SCROLL_LINE | borderStyle | orientation );
table.setHeaderVisible(true);
table.setLinesVisible(true);
TableViewer tableView = new TableViewer(table);
// code yanked to set up the columns (movable == false,
// resiable == true, with set text, widths, and toolTipText)
tableView.setContentProvider(new ArrayContentProvider());
// BeanLabelProvider's getColumnText returns the beanDescriptor's
// getReadMethod.invoke().toString()
tableView.setLabelProvider(new BeanLableProvider(MyClass.class));
tableView.setInput(anArrayOfMyClass);
All this code lives in my dialog class's createDialogArea().
At any rate, I've tried the following:
treeView.setItemCount() trims the data, it doesn't limit the amount of data displayed.
table.getVerticalBar() returns a valid ScrollBar, and that scrollbar.getVisible() == true;?! Really? Prove it. Didn't think so. setVisible(true) does nothing.
table.setBounds()? Nope. Even after I call layout() and/or redraw().
The above was done within createDialogArea(). If I call setBounds() within MyDialog.initializeBounds() it shrinks the table and adds scrollbars (gasp!), but the dialog doesn't resize. Calling table.getParent().layout(true) resizes the table, not the dialog. Augh!
In initBounds, I have the following code:
Shell shell = getShell();
shell.setBounds(foo, bar, baz qux);
myTable.setBounds(foo, bar, baz-fudgeX, qux-fudgeY);
myTable.getVerticalBar().setVisible(true); // see, it wasn't null!
// this line doesn't seem to have any effect, I can comment it out and have
// the same apparent result.
shell.layout(true);
// If I call shell.pack() the table snaps back to its original size (huge).
Without the call to pack(), the okay/cancel buttons are clipped off the bottom of the dialog. With it, the entire exercise is a moot point.
Why do I get the feeling I'm missing something Really Basic that'll make me want to smack my forehead when someone else points it out?
What layout do you use for your Shell? Most layouts simply ask the controls for their preferred size, during a call of layout(). The preferred size of your Table is the size, where all rows can be displayed. If there's no more constraints, the layout resizes the Shell to that preferred size, which is not what you want.
To prevent this, you'll need to set a height hint for your layout on your Table. An example for that is this snippet. If you remove the line
data.heightHint = 200;
the resulting Shell is way too high.
Related
I have a JTable that when clicking on the row expands it and filters all other rows in the table. When the user clicks on this expanded row again it returns to its normal size and the filter shows all the table again. All this works fine but when returning from the view of a single expanded row to the whole table I would like to set the scroll pane vertical scroll bar value to that one which is the position of the row that have been expanded. I correctly compute this value and try to set it with
scrollPane.getVerticalScrollBar().setValue(value);
but as I have found with scrollPane.getVerticalScrollBar().getMaximum() the scroll bar doesn't update its values after sorter.setRowFilter(filter); invocation. getMaximum() returns the height of that single row that have been expanded and for that reason scroll bar fails to set the updated value (that is typically much greater). This is the code:
// after this call the table shows all its rows again
sorter.setRowFilter(filter);
System.out.println("maximum=" + scrollPane.getVerticalScrollBar().getMaximum());
// shows old value when the table has consisted from a single expanded row
scrollPane.getVerticalScrollBar().setValue(value); // fails
Can anyone tell me how to let the scroll bar to know that the view of JScrollPane has changed so it can correctly set its maximum value? (though the scroll bar itself seems to be ok and looks like it has look properly scrolling all table). I have tried to call revalidate and updateUI methods on the JTable but it doesn't help.
scrollPane.getVerticalScrollBar().setValue(value);
Try wrapping the above code in a SwingUtilities.invokeLater(). This will add the code to the end of the event queue.
You need to revalidate the JScrollPane, but only after you invalidate the JTable. Thus, something like
sorter.setRowFilter(filter);
sorter.invalidate();
// shows old value when the table has consisted from a single expanded row
scrollPane.revalidate();
I am using a jface tableviewer with a tablecolumnlayout (for it's parent composite) in my eclipse RCP application.
I see that, in my view, the columns are equally spaced out to cover the entire width of the table.. So far so good.
We have a functionality where we need to save & load the table layout . Basically user can hide/ re-order any number of columns and he wishes to save that particular layout..
I am using the eclipse preferences API to save/retrieve the table layout.
The view opens with a default layout (with all columns & with the default ordering ) when the view is opened for the 1st time. Hence, I would save a DEFAULT layout(with the default ordering & column widths) whenever the view loads so that user can come back to the original layout at any time.
But, the problem here is when I try to load the default layout, I see that the columns are NOT equally spaced out and there is an empty column at the end.
What can i possibly do to achieve the default behaviour where columns get equally spaced out to cover the width of the table?
Note: I tried invoking the layout() of table's parent composite or calling the redraw of both table as well as composite but it did not work out.
I solved the above mentioned issue.
SWT increases the given width of the columns during view rendering so that the entire table width is occupied.
I was able to get the increased width of each column only when the view was about to be rendered (during part Activation ).
If I asked for the column width just after using the tablecolumnlayout, I was still getting the predefined width.
As a solution to this, I moved my code to save the default layout during part activation so that the actual increased width is obtained.
I'm using a FieldEditorPreferencePage for my Preference Window. I'm also using FieldEditor Abstract base class for all my field editors in the Preference window.
I have 3 radio buttons and as the user clicks on the radio button a text will be displayed at the bottom of the window educating the user on the selected options, My problem is that, the text of the label is very long and does not fit into the Preference window. I want the label text to wrap up automatically as the size of the window grows or shrinks, I tried every possible thing to wrap up the text but instead the text gets lost, for example I have tried :
GridData g = new GridData(GridData.FILL_BOTH);
g.widthHint = 200;
Label l = new Label(getFieldEditorParent(), SWT.BEGINNING | SWT.WRAP);
l.setLayoutData(g);
l.setText("Long Msg");
getFieldEditorParent() returns a parent composite for a field editor.
Apart from the above code, I have also tried all the other possible combinations of parameters for GridData and the Label, but the result in the UI part remains the same(text will get lost if the size of the text exceeds the size of the window). I can set the size of the window to fit the text, but that is hard-coding.
How can I possibly wrap up the label text as the window's size grows and shrinks?
Is this an issue with FieldEditorPreferencePage? Is there a conflict using a label in a preference page? Someone please explain in brief as to why this happens?
I've been tasked with doing refactoring to a Java Swing application we have here that's pretty crufty. My job is to clean up the code and make it more dynamic as we're going to start using it with a second project and it needs to be able to adjust appropriately. There is a small portion of one window that contains
A button
A JFormattedTextField that is used to select dates.
A 3X4 table of JLabels that display data.
The person who originally wrote this simply used a GridBagLayout JPanel and then hardcoded everything, including the table's header and row label's and left empty JLabel's in the dynamic data position. When the dynamic information is received setText is called with the text data. Part of my refactoring will cause the entire table to be dynamic in dimension as well as content so I decided to make the table a sub-panel with a GridLayout and dynamically set the contents and dimensions with this piece of code:
public void updateInfoPanel(ArrayList rows) {
System.out.println("Updating Info Panel!");
//genericInfo is the new sub panel in question.
genericInfo.removeAll();
GridLayout layout = new GridLayout();
layout.setColumns(rows.get(0).length);
layout.setRows(rows.size());
genericInfo.setLayout(layout);
for(String[] row : rows) {
for(String element : row) {
genericInfo.add(new Label(element));
}
}
}
I have verified that this is only ever getting called one time per window creation but the entire window is now incredibly sluggish. It can take >5 seconds to respond to clicks in other parts of the frame that used to respond in fractions of a second. Does anyone know what would cause this? Is there something about GridLayouts that I don't understand?
Try calling this code on the EDT.
No it appears you understand GridLayouts. The problem is elsewhere, look at other code that you might have changed, and do some profiling to determine the true source of the slowdown.
Background:
I am doing some UI work in an eclipse environment where I fill a combo control with some values. The string values have an different length and often a length greater than the combo width and the width of the parent composite.
Problem:
When I open the dropdown list, the width of the list is greater than the width of the parent composite and the user could not see the complete value of the list entry.
I have tried to use the "setTextLimit" option, but without success. For me it is perfectly adequate if I could set the width of the dropdon list with a constant value.
Code example:
this.mComponentName = new Combo (lComponentComposite, SWT.BORDER);
this.mComponentName.setTextLimit(COMBO_TEXT_LIMIT);
GridData componentNameGridData = new GridData();
componentNameGridData.widthHint = 166;
this.mComponentName.setLayoutData(componentNameGridData);
this.mComponentName.addSelectionListener(this.mComboSelectionAdapter);
this.mComponentName.addKeyListener(this.mComboKeyAdapter);
Greetings
dirk
While creating a combobox specify Horizontal scroll also
this.mComponentName = new Combo (lComponentComposite, SWT.BORDER|SWT.H_SCROLL);
This will not let the text overflow
That is really a good question. After digging through developer forums and even the source code, I lean towards saying it is not possible.
I solved the problem temporarily by switching to a CCombo, but I do not like the solution as I believe one of SWT's strength is in using native widgets, and the CCombo does not look as good (at least on Win7 and OS X).
When you select a drop-down combo box or drop-down list box to size it, only the right and left sizing handles are active. Use these handles to set the width of the box as it is displayed initially.
Choose the drop-down arrow at the right of the combo box.
The outline of the control changes to show the size of the combo box with the drop-down area extended.
Use the bottom sizing handle to change the initial size of the drop-down area.
Choose the drop-down arrow again to close the drop-down portion of the combo box.
Have you tried passing some other value as your second parameter in your Combo initialization (first line) ? Something other than SWT.BORDER?
I've tried finding out if it is possible to display strings in multiple rows (as a single combo box item), but with no success. This would reduce the width of your items. Try adding \n to a couple of strings that you add to the combo box, and see if it will work. If it works, you can automate this process later by parsing through the string, and checking whether the space character count gets to high and adding \n after every fifth or sixth blank character. I think codejammer's suggestion is the best one, but I can't upvote yet.
Partially solved the problem adding to item names some spaces (have set the number of spaces by trial and error). Now combo is wider and all texts are visible, however horizontal scroll is visible, too.
String p = " ";
combo.add("Long.... item name 1"+p);
combo.add("Long item name ..... 2"+p);
...
Another soultion is resize combo when it gain focus and restore size after lost focus. Example below.
final Point p = combo.getSize();
combo.addFocusListener(new FocusListener() {
#Override
public void focusLost(FocusEvent e) {
((Control) e.getSource()).setSize(p);
}
#Override
public void focusGained(FocusEvent e) {
((Control) e.getSource()).setSize(new Point(400, p.y));
}
});
combo.setPreferredSize(new Dimension(FIX_WIDTH, FIX_HEIGHT));
Try this
JComboBox CB = new JComboBox();
CB.setPreferredSize(new Dimension(int, int));
p.add(CB);// which p is a JPanel