SWT: Table with cells that span columns - java

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.

Related

centering a button in 2 grids using GridBagLayout (Java Swing)

I am curious if it possible to center a JButton in two X grids of a GridBagLayout? I've done some searching through the API but haven't found much. I have also drawn up a small picture to clarify what I am trying to do.
Thank you!
Example Image:
I've done some searching through the API but haven't found much.
Read the section from the Swing tutorial on How to Use GridBagLayout.
You would need to concentrate on the Specifying Constraints section:
the gridwidth constraint will allow the button to span multiple columns
the anchor constraint will allow the button to be centered within the two columns.
the fill constraint will need to be turned off.
The above assumes that you actually have other components on the panel in each of the columns. You can't just randomly say a single component takes up two columns.
So start with the demo code in the tutorial an modify it to have the button on the bottom centered.

Why do I have to call expand when aligning widget inside the cell in libgdx?

I am new to libgdx. I was referring to the documentation here TableLayout-Alignment
Similar to this example, for simplicity's sake, let's say I have a nameLabel and a nameText widgets.
So when I do
table.add(nameLabel).width(100);
table.add(nameText).width(100);
I will get two widgets of of same size adjacent to each other in the middle of outside table as by-default it is centered aligned. something like this:
Now what I want to do, I want to send "nameLabel" to the extreme left and "nameText" to the extreme right.
What I am doing is that
table.add(nameLabel).width(100).left();
table.add(nameText).width(100).right();
But it won't work, until I expand both of these widgets as explained in the example above. Can anyone explain to me, why I have to expand for this alignment to work?
Calling cell.expand() make the cell expand its size to vertical or horizontal or both. Not everybody always want the cell expand after created by default.
The left and right functions only work if the cell is expanded.

How to scroll horizontally within a single column of a resizeable JTable?

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.

Vertical scrollbar covering the last column data of jtable(last column data is right aligned)

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.

Align an image in a SWT TableViewer

I just found out, that its somehow impossible to align an image in a swt tableviewer. Creating a TableColumn with SWT.RIGHT (for instance) has no effect on that column if its labelprovider returns an image for it.
Question: Is there any other way to align an image instead of modifiying the image file itself and put some extra pixel into it?
I believe that you're running into a limitation of the underlying platform (or something to that effect). You can have total control over what goes into the cell if you draw it yourself. There is a snippet that shows you how to do this.
Table example snippet: draw images on right side of table item
Of course, you'll also have to draw any text you want in the cell.

Categories