JTree does not extend automatically, Swing embedded into SWT

131 views Asked by At

So I am working with Swing components embedded into SWT.

Here a simplified version of my issue, basically I have two Panel objects (panelA, panelB) with some Swing components, embedded in my Shell. I want to add an on/off option on the panelB, doing that I did except my JTree to fill out the whole Shell while panelB is off, but it didn't...

The only way I found is recreating the components but I feel like it is not the good way to do things. (For putting off the component I am using setVisible()) Any ideas to make the JTree component fill the space when panelB is off?

import javax.swing.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;


public class testCW {

public static void main(String[] args) {

  Display display = new Display();
  Shell shell = new Shell(display);
  shell.setText("Test");
  shell.setLayout(new FillLayout());


  Composite compositeA = new Composite(shell,SWT.EMBEDDED);
  java.awt.Frame frameA = SWT_AWT.new_Frame(compositeA);
  java.awt.Panel panelA = new java.awt.Panel(new java.awt.BorderLayout());
  frameA.add(panelA);
  JScrollPane scrollPaneA = new JScrollPane(new JTree());
  panelA.add(scrollPaneA);


  Composite compositeB = new Composite(shell,SWT.EMBEDDED);
  java.awt.Frame frameB = SWT_AWT.new_Frame(compositeB);
  java.awt.Panel panelB = new java.awt.Panel(new java.awt.BorderLayout());
  frameB.add(panelB);
  JButton buttonB = new JButton("Button");
  panelB.add(buttonB);



  shell.open();
  while (!shell.isDisposed()) {
    if (!display.readAndDispatch())
    display.sleep();
    // test 1 : hide panelB or compositeB? -> run -> JTree extends well?
    // test 2 : hide panelB or compositeB? -> print panelB(compositeB)
    // -> run  -> displays well ?
    // panelB.setVisible(false);
    // compositeB.dispose();     //TEST
  }
  display.dispose();

}
}
2

There are 2 answers

0
Florian S. On

Do you have to stick with FillLayout? Whenever you want to hide components in SWT it is recommended to use GridLayout. Here is an example of how to do it: http://www.java2s.com/Code/Java/SWT-JFace-Eclipse/ExcludeawidgetfromaGridLayout.htm

13
SomeDude On

You could add a parent composite that holds the compositeA , compositeB like below :

final Composite composite = new Composite( shell, SWT.NONE);
composite.setLayout(new GridLayout( 2,false) );

Then add your composites to that parent like :

Composite compositeA = new Composite(composite, SWT.EMBEDDED);
GridData filldataA = new GridData(GridData.FILL_BOTH);
compositeA.setLayoutData(filldataA);

Note that I added GridData for the compositeA to occupy whole space initially.

Add selection listener to your SWT.PUSH button on the toolbar. Lets say - your SWT.PUSH button is swtButton. Then add a selection listener to it like below :

swtButton.addSelectionListener(new SelectionAdapter()
{
    private boolean expandTree = true;
    @Override
    public void widgetSelected(SelectionEvent e) 
    {
        Layout lo = composite.getLayout();
        GridLayout glo = (GridLayout)lo;
        if ( expandTree )
        {
            glo.makeColumnsEqualWidth = true;
            expandTree = false;
        }
        else
        {
            glo.makeColumnsEqualWidth = false;
            expandTree = true;
        }

        composite.layout();
    }
});

Basically you have a boolean in your action listener - expandTree - that toggles between button clicks, depending on true or false - make the parent composite switch between making the columns equal width true or false.

IMO you don't need compositeB