I want the table to resize and occupy the space left by vertical scrollbar when I delete the rows.
This is the table with vertical scrollbar:
And when I delete the rows, then I get this the table without vertical scrollbar:
.
I tried using
natTable natTable = new NatTable(panel, SWT.NO_BACKGROUND | SWT.NO_REDRAW_RESIZE | SWT.DOUBLE_BUFFERED | SWT.V_SCROLL);
and was working fine but the horizontal scrollbar was not coming even after I tried to set it from the viewportlayer.
First, of course with that code the horizontal scrollbar is not coming, you did not set the style bit to include a horizontal scrollbar. You should leave the default style bits in place and let NatTable do the necessary actions.
Second, you are not providing enough information on your setup. Are you using percentage sized columns? What are you triggering to delete a row? Is it a custom command or the NatTable default command?
What I know is that there is an issue with percentage sizing and setting the scrollbar invisible because that does not trigger a resize on the Composite. Feel free to create a ticket so I can have a look at this.
Related
I would like to create the main menu for my program and I have difficulties with Swing.
How would you code an alignment like this?
The middle elements would be the options like exit or settings and this window should be able to be resized and its contents should get bigger proportionally.
Single column GridLayout with vertical padding declared in the constructor. A grid layout will stretch components to fit the available space.
Add a large EmptyBorder to the JPanel that contains the 3 buttons, and that's the job done.
I have a composite(innerComposite) within a ScrolledComposite(sc). At runtime, additional UI components can be added. So I'm using the code below to set the minimum size of the sc to enable scrolling, in case that the additional UI components "overflow" the sc.
sc.setMinSize(
innerComposite.computeSize(
innerComposite.getSize().x, SWT.DEFAULT
)
);
One issue is with the SWT Multi-Line textfield inside this sc/innerComposite.
textBox = new org.eclipse.swt.widgets.Text(parent, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL);
When I enter a long text into this multi-line textfield, the vertical scrollbar will appear, (which is what I wanted!). However, when I call the code above to recompute the size and setting the sc.setMinSize()...the multi-line textfield's height will expand to fit the length of the text entered. This is not what I wanted. I want this height to remain on with the scrollbar and not resize to fit the text entered.
I know computeSize will cast its children to resize to the preferred size. I don't want this to happen to the multiline textfield as it has the capability of scrolling the vertical bar.
How can I prevent this from happening?
An alternative way of solving your problem is writing your own custom layout and setting that to your inner composite. This way you can control exactly how the controls will appear.
The solution is to use GridLayout in the content Composite. This allows you to set a GridData as layoutData on the text which contains both a wHint+hHint (which is used when doing computeSize() on the composite ) but still activate verticalGrab which will attribute any additional space to this composite.
In other words: the preferred size of the GridLayouted Composite uses the hHint/wHint from the GridData's of its children. This allows you to set the preferred size of you Text while still expanding the text using GridData.verticalGrab if extra space is available.
i solved this by setting the width/height!
I am making a dialog for the purpose of selecting multiple file paths. My dialog consists of two panels. One for buttons such as "Add" and "Remove", and a second panel containing a JTable wrapped in a scrollPane. The table has only one column. The cells of the table are not editable directly. When a user selects a file using a JFileChooser, the full path of that file will be added to the table. Although my dialog is resizeable, I still need a horizontal scroll behavior in the event that the file path is longer than the user's screen is wide.
I have researched the combination of resizeable table and horizontal scroll bar. That is similar, but not my issue. The typical scroll behavior is that the columns are scrolled, not the contents of the columns. I need the contents of a single column to scroll horizontally.
doesn't matter whether you scroll a multiple or only a single column: the basic issue is to get the horizontal scrollBar to start with :-)
There are two screws to tweak:
- enable horizontal scrolling by setting the table's resizeMode: default is to always fit the table's size to the size of the scrollPane, that is no scrolling
- resize the column width to fit its content
In a core JTable that maps into pseudo-code like
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
// on receiving a TableModelEvent which might increase the column width
// calculate new width by measuring pref of the renderer
int newWidth = ...
// set it as pref of the column
table.getColumnModel().getColumn(0).setPreferredWidth(newWidth);
The catch is that without resizeMode, you are always responsible to sizing the column: it its width is less than the scrollPane, there's an empty region at its trailing side.
JXTable (part of SwingX project), supports an addition sizing mode which fills the available horizontal space as long as the table's prefWidts is less than parent width and shows a horizontal scrollBar if needed
table.setHorizontalScrollEnabled(true);
// on receiving a TableModelEvent which might increase the column width
// tell the table to re-evaluate
table.packColumn(0);
I selected kleopatra's answer as correct because it addresses my specific question regarding table manipulation. I am adding this answer because I ended up solving my root problem in a different manner.
I chose to use a JList to represent my file paths instead of a single column table. The only real reason that I had wanted to use the JTable was because of the appearance of the lined rows that a table has, and because of my unfamiliarity with JList. I discovered how to edit the appearance of the JList by extending the DefaultListCellRenderer. Because I now knew about editing the appearance, the JList's natural resizing and scroll behavior made it a much more natural fit to my needs.
In a Java desktop app with SWT-based GUI, we have a table in which some rows must span multiple columns. This was solved with the approach shown in this official SWT Snippet: Snippet239.java
However, much later it was discovered that there's a major problem with this approach on Ubuntu with the default Ambiance/Radiance theme: There are always vertical lines between the columns, even for cells that span multiple columns. This is shown in the following screenshots:
As you can see, in the first screenshot there's a vertical line between Column 1 and Column 2. Does anybody have an idea how to get rid of these lines?
We've already tried the following:
Table.setLinesVisible(false): Doesn't work, the vertical lines don't go away.
Use owner-draw-based label providers to draw over those vertical lines: Doesn't work, the vertical lines seem to be drawn on top of everything that is drawn by the label providers.
Attach paint listener to table to draw over the vertical lines: Doesn't work, because there are lots of glitches whenever the table is scrolled or otherwise updated.
The Nebula project has a Grid widget where cells can span multiple columns, and it doesn't have a problem with vertical lines, because the entire table is drawn non-natively. However, we can't justify the effort to replace our table with a Grid widget just to fix the vertical lines problem on a particular platform with certain themes. Also, the Grid widget seems to be a pre-release alpha version.
Based on the things we've already tried, I assume the only way left is to muck around in the platform-specific internals of SWT, but I don't even know where to start with something like that.
We solved this problem by using a Table with a single owner-draw column that emulates multiple columns.
I am using a jtable which have 8 rows by default. When I add a new row using a button click a vertical scrollbar comes into picture and covers up my data of last column in jtable which is right aligned. How can I overcome this?
Specify setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS) on the JScrollPane. This will leave room for the scroll bar when (if) it becomes necessary.
Sounds like you need to give your table a little bit more space to account for the addition of a scroll bar. You will need to possibly mess with the preferredSize.