1

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();

}
}
Logan Wlv
  • 3,274
  • 5
  • 32
  • 54

2 Answers2

1

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

SomeDude
  • 13,876
  • 5
  • 21
  • 44
  • Thanks it is extending well, but what should I add to **hide** the button? On the application that I am working on, I will have an external button that will **on/off** `buttonB` so the `JTree` component can fill the whole space. – Logan Wlv Jun 29 '17 at 09:09
  • If you hide/setvisible(false) on buttonB how can you switch it on again that is push an invisible button? Can you show code that you were talking about ? – SomeDude Jun 29 '17 at 11:39
  • May be you can have a checkbox instead of a push button. Upon checking on, expand the tree, upon checking off, collapse the tree – SomeDude Jun 29 '17 at 11:58
  • On my real application (RCP Application) I have a toolbar with a button **on/off**, here `buttonB` is just a random swing component added to my `panelB` as example but I'm not using it as **one/off** button. – Logan Wlv Jun 29 '17 at 12:00
  • By on/off do you mean a toggle button ? A toggle button is different from button with SWT.PUSH – SomeDude Jun 29 '17 at 12:02
  • so to test if it displays well, no need to press anything. I did just add some codes after `display.sleep();` -> run -> check if it is displaying well (on this specific example) – Logan Wlv Jun 29 '17 at 12:02
  • on the toolbar it is a SWT.PUSH button – Logan Wlv Jun 29 '17 at 12:03
  • If you hide the SWT.PUSH button, how can you push it again? – SomeDude Jun 29 '17 at 12:05
  • On the real application it is not hidden since it is on a toolbar at the top. In this example there is no toolbar button to hide/print, so to test it I just add codes after `display.sleep();` , I did some edits to explain how to test if it works on this specific example, I don't know if it is clear? I can rewrite the example if it is still not clear – Logan Wlv Jun 29 '17 at 12:14
  • The JButton is just a random component for the example, it should not be used – Logan Wlv Jun 29 '17 at 12:15
  • test 1 & test 2 on this example , are like a simulation of what happens when you push one time and two times the toolbar button on my real application. – Logan Wlv Jun 29 '17 at 12:17
  • I don't understand why do you need panelB if all you need is expand the tree ( i.e. make it occupy whole space ) versus shrinking it ? I updated my answer. please check it. I added selection listener to swt.push button instead. – SomeDude Jun 29 '17 at 13:18
  • because panelB (in my real application) has many swing components that I want to hide sometimes to have a better sight on my JTree. In my question it is a really simplified situation when I just want to find a way to extend the JTree when panelB is **temporary** hidden so I can reproduce it on my real application. – Logan Wlv Jun 29 '17 at 14:19
0

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

Florian S.
  • 386
  • 1
  • 12