Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 350670

Summary: Swing: It is not possible to enable a horizontal scroll bar in listboxes
Product: z_Archived Reporter: Ivan Motsch <ivan.motsch>
Component: ScoutAssignee: Project Inbox <scout.core-inbox>
Status: CLOSED FIXED QA Contact:
Severity: enhancement    
Priority: P3 CC: lhu, zimmermann
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   
Whiteboard:

Description Ivan Motsch CLA 2011-06-29 04:58:17 EDT
[on behalf of Sandro Schifferle, BSI AG]

When a listbox contains long entries they are not readable.


In our own project we used the following workaround;

@Override
  protected void execInitField() throws ProcessingException {
    /* set autoResizeColumns off */
    getTable().setAutoResizeColumns(false);

    /* resize the column to the label size*/
    JLabel l = new JLabel();
    int pix_max = 0;

    for (ITableRow tableRow : getTable().getRows()) {
      if (tableRow.isFilterAccepted()) { /* filter inactive rows */
        if (l.getFontMetrics(l.getFont()).stringWidth(tableRow.getCell(1).getText()) > pix_max) {
          pix_max = l.getFontMetrics(l.getFont()).stringWidth(tableRow.getCell(1).getText());
        }
      }
    }

    /* + 30 is because of the checkbox */
    pix_max += 30;

    if (pix_max > getTable().getColumnSet().getColumn(1).getInitialWidth()) {
      getTable().getColumnSet().getColumn(1).setWidth(pix_max);
    }
  }
Comment 1 Ivan Motsch CLA 2011-06-29 05:01:49 EDT
Added new api method for scout on eclipse 3.8.0:
IColumn.isAutoOptimizeWidth
IColumn.setAutoOptimizeWidth
AbstractColumn.getCOnfiguredAutoOptimizeWidth (boolean default false)

The listbox can do the following:
      @Override
      protected void execInitField() throws ProcessingException {
        getTable().setAutoResizeColumns(false);
        getTable().getColumns()[1].setAutoOptimizeWidth(true);
      }

This feature only works inswing since swt does not offer an api to auto size the column (as by double clicking in the middle between two table columns)
Comment 2 Lukas Huser CLA 2011-06-29 08:40:47 EDT
If I understand correctly, the proposed solution to this ticket is a more elegant workaround as the one proposed by S. Schifferle.
To enable a horizontal scrollbar on a listbox I still need to enable it on every listbox (or introduce my own AbstractListboxWithScrollbar and use it instead of AbstractListbox).

Would it be possible to enable this feature on all listboxes by default (e.g. in AbstractListbox)?
Comment 3 Ivan Motsch CLA 2011-06-29 09:17:57 EDT
This is not necessary.

For every such project-specific "default" you are free to create your own template field:
public abstract class AbstractMyListBox extends AbstractListBox{
  ...
  protected void execInitField...{
  }
}

Instead of the basic AbstractListBox you then use the AbstractMyListBox wherever you need that listbox.

This is called "adding a template layer". When there are already many such fields in your project, then simply do a search/replace and you have your template layer.
Comment 4 Lukas Huser CLA 2011-06-29 10:40:49 EDT
Ok, I understand how to create a template layer.
I still think it would be good if no such template layer is necessary and it would be the default behavior of any listbox to show a horizontal scrollbar (only if needed).
The length of the listbox content is often not known to the programmer (e.g. content read from a database, texts defined by an application user etc.). In these cases a horizontal scrollbar is certainly appreciated by the application user.

So I think all applications could profit from such a default behavior of a listbox.
Comment 5 Ivan Motsch CLA 2011-06-29 10:51:04 EDT
Unfortunately this feature is only possible in Swing, not SWT. 
Swt offers no api to make a column "optimal" sized. Even though the double click between two columns does that natively.
Also when used in swing the horizontal grid lines are not anymore drawn on the full width when the listbox is wider than its "optimal" width.
And not to forget existing code that is optimized to have listbox height that matches its item count. This change of default would break their code.

These drawbacks together with the easy way of adding a template layer make it almost mandatory to keep the current default as default and to recommend to you to add that template layer to your project.

Please note that we are not talking about a feature that does not exist but just about changing an existing default behaviour. The possibility to have your desired functionality is added in 3.7.0+
Comment 6 Lukas Huser CLA 2011-06-29 10:57:10 EDT
Ok, thanks for your feedback.
Comment 7 Matthias Zimmermann CLA 2011-11-22 03:06:09 EST
changed status from closed to resolved fixed. bugs are cloesd only after a specific release is shipped.
Comment 8 Matthias Zimmermann CLA 2012-07-09 06:50:47 EDT
ticket closed.
deliverd as part of eclipse scout 3.8.0 (juno release train)