I am wanting to make each cell in a row a different length..
Here is a picture to help.
So the Policy cell and the text to the right is fine. However, Section 1 and Section 2 I want to be 50/50.. Not 20/80 or whatever it is now. I have started using the WindowsBuilder tool instead of doing this by hand. Is this possible to do?
To lay out controls in the requested manner with a GridLayout in SWT you will have to group the controls of each row into a composite of their own like so:
shell.setLayout( new RowLayout( SWT.VERTICAL ) );
Composite composite1 = new Composite( shell, SWT.NONE );
composite1.setLayout( new GridLayout( 2, false ) );
createLabel( composite1, "2020" );
createLabel( composite1, "808080808080" );
Composite composite2 = new Composite( shell, SWT.NONE );
composite2.setLayout( new GridLayout( 2, false ) );
createLabel( composite2, "50505050" );
createLabel( composite2, "50505050" );
private static Label createLabel( Composite parent, String text ) {
Label label = new Label( parent, SWT.NONE );
label.setText( text );
return label;
}
However, to me a FormLayout seems more suitable to solve the given problem:
shell.setLayout( new FormLayout() );
FormData leftFormData = new FormData();
leftFormData.top = new FormAttachment( 0 );
leftFormData.left = new FormAttachment( 0 );
leftFormData.right = new FormAttachment( 20 );
Label leftLabel = createLabel( shell, "2020", leftFormData );
FormData rightFormData = new FormData();
rightFormData.top = new FormAttachment( 0 );
rightFormData.left = new FormAttachment( leftLabel );
rightFormData.right = new FormAttachment( 100 );
createLabel( shell, "808080808080", rightFormData );
private static Label createLabel( Composite parent, String text, Object layoutData ) {
Label label = new Label( parent, SWT.NONE );
label.setText( text );
label.setLayoutData( layoutData );
return label;
}
If you find the formData and formAttachment code too verbose, you may have a look at Slim Down SWT FormLayout Usage
And for an in-depth discussion of SWT layouts I recommend the Understanding Layouts in SWT article.
EDIT: Missed that you are using SWT GridLayout instead of the Swing one. My solution works with Swing so keep that in mind.
You may want to use the GridBagLayout. It's more complicated than the GridLayout but offers greater flexibility
See some documentation on how to use it here
Related
I am trying to set the weight of the splitter to 0.9 yet it does not seem to work. What am I missing and what am i to do? I've checked this post, yet I could not neither understand nor solve the problem of mine. What I want basically is
something like this though the split pane and the table is always %50,%50. So splitter.setResizeWeight( 0.9 ); is not working.
Here's the code of the panel:
public FlightPanel( final SomeOtherClass category, final SomeClass dar2 )
{
this.detailsPanel = new JPanel( new GridLayout( 0, 1 ) );
this.sum = new JPanel( new GridLayout( 0, 1 ) );
this.model =
new FlightPanelTableModel(...);
this.timeTable = new JTable( this.model );
this.timeTable.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );
this.setLayout( new GridLayout( 0, 1 ) );
this.treeView = new FlightPanelTreeView( dar2 );
ToolTipManager.sharedInstance().registerComponent( this.treeView );
this.detailsPanel.add( this.treeView );
final JSplitPane splitter =
new JSplitPane( JSplitPane.HORIZONTAL_SPLIT, new JScrollPane( this.timeTable ),
new JScrollPane( this.detailsPanel ) );
splitter.setResizeWeight( 0.9 );
this.sum.add( splitter );
this.add( this.sum );
}
How could I solve it?
Thanks in advance.
I am a newbie to SWT GUI development. I am using SWT for developing Eclipse plug-ins. Basically, I wanted two radio option groups and some textboxes beneath the second radio option group; something like below.
Heading1
. A radio button
. B radio button
Heading2
. C radio button
. D radio button
Textbox1 Textbox2 Textbox3
Since I needed those text boxes relative to each other,I used FormLayout. But it gives me exception:
Caused by: java.lang.ClassCastException: org.eclipse.swt.layout.FormData cannot be cast to org.eclipse.swt.layout.GridData*
But I haven't used GridData.
Exception occurs at line 129.
I am unable to add Textbox2 relative to Textbox1.My code is below.
Shell sh = new Shell(parent_shell,SWT.PRIMARY_MODAL | SWT.TRAIL | SWT.CASCADE);
System.out.println("execute");
FormLayout fL = new FormLayout();
sh.setLayout(fL);
sh.setText("Configure");
sh.setSize(330,300);
sh.setActive();
Composite composite = new Composite( sh, SWT.NONE );
GridLayout layout = new GridLayout();
layout.marginWidth = 0;
layout.marginHeight = 0;
composite.setLayout( layout );
FormData fd = new FormData(); //line 100
fd.top = new FormAttachment(0,0);//line 101
fd.left = new FormAttachment(0,0);//line 102
//fd.right = new FormAttachment(26);//line 103
//fd.bottom = new FormAttachment(10);//line 104
composite.setLayoutData( fd );
RadioGroupFieldEditor rgfe = new RadioGroupFieldEditor("User Choice",
"Heading1", 1, new String[][]{
{"A radio button","a"},{"B radio button","b"}
}, composite,false);
RadioGroupFieldEditor rgfe1 = new RadioGroupFieldEditor("User Choice1",
"Heading2", 1, new String[][]{
{"C radio button","c"},{"D radio button","d"}
}, composite,false);
Text ol = new Text(composite,SWT.READ_ONLY);
ol.setText("Output Location");
ol.setEnabled(false);
// FormData fd1 = new FormData();
// fd.top = new FormAttachment(fd);
// fd.left = new FormAttachment(1);
// fd.right = new FormAttachment(26);
// fd.bottom = new FormAttachment(10);
// ol.pack();
// ol.setLayoutData(fd1); //line 129 --> exception occurs
while (!sh.isDisposed()) {
if (!parent_display.readAndDispatch()) {
parent_display.sleep();
}
}
}
The RadioGroupFieldEditor assumes that its parent has a GridLayout set.
I suggest you embed the RadioGroupFieldEditor in a separate Composite that has a GridLayout set. Then you can attach FormData to the Composite to control its layout.
For example:
Composite composite = new Composite( sh, SWT.NONE );
GridLayout layout = new GridLayout();
layout.marginWidth = 0;
layout.marginHeight = 0;
composite.setLayout( layout );
FormData compositeFormData = new FormData();
...
composite.setLayoutData( compositeFormData );
RadioGroupFieldEditor fieldEditor = new RadioGroupFieldEditor( ..., composite, ... );
Is it possible to center the text inside a label vertically? I'm using:
message1 = new Label(shell, SWT.VERTICAL | SWT.CENTER);
This centers the text horizontally, but not vertically.
CLabel label = new CLabel(shell, SWT.CENTER);
Maybe you should try using org.eclipse.swt.custom.CLabel (JavaDoc).
The code above exactly centers the text in the label vertically.
The text within a label is always aligned at the top. The VERTICAL style only applies when SEPARATOR is set. In this case it displays a single vertical or horizontal line and the text is ignored.
But you can center the label itself within the parent. For example, this snippet centers a label within the containing shell by using a GridLayout:
public static void main( String[] args ) {
Display display = new Display();
Shell parent = new Shell( display );
Label label = new Label( parent, SWT.NONE );
label.setText( "some text" );
label.setBackground( display.getSystemColor( SWT.COLOR_GREEN ) );
parent.setLayout( new GridLayout( 1, false ) );
label.setLayoutData( new GridData( SWT.CENTER, SWT.CENTER, true, true ) );
parent.open();
while( !parent.isDisposed() ) {
if( !display.readAndDispatch() )
display.sleep();
}
display.dispose();
}
I am building a Java application using SWT. One of the requirements of the application is that it have multiple windows. Rather than having "forever independent" windows, I thought it would be cool to implement a feature like in most browsers where you have a single, tabular window, where each tab can be dragged out to create a separate window. After a little research using the Google, it seems possible to accomplish this using JavaFX, but is it possible (and relatively easy) to achieve the same functionality in SWT? Thanks in advance.
While my take might be a little late, here it is nonetheless. Below snippet is a rough POC that allows to drag an item from the CTabFolder and when the item is dropped outside of the bounds of the folder, a shell is opened to show items' content.
public static void main( String[] args ) {
Display display = new Display();
final Shell shell = new Shell( display );
shell.setLayout( new FillLayout() );
final CTabFolder folder = new CTabFolder( shell, SWT.BORDER );
for( int i = 0; i < 4; i++ ) {
CTabItem item = new CTabItem( folder, SWT.CLOSE );
item.setText( "Item " + i );
Text text = new Text( folder, SWT.MULTI );
text.setText( "Content for Item " + i );
item.setControl( text );
}
Listener dragListener = new Listener() {
private CTabItem dragItem;
public void handleEvent( Event event ) {
Point mouseLocation = new Point( event.x, event.y );
switch( event.type ) {
case SWT.DragDetect: {
CTabItem item = folder.getItem( mouseLocation );
if( dragItem == null && item != null ) {
dragItem = item;
folder.setCapture( true );
}
break;
}
case SWT.MouseUp: {
if( dragItem != null && !folder.getBounds().contains( mouseLocation ) ) {
popOut( dragItem, folder.toDisplay( mouseLocation ) );
dragItem.dispose();
dragItem = null;
}
break;
}
}
}
};
folder.addListener( SWT.DragDetect, dragListener );
folder.addListener( SWT.MouseUp, dragListener );
shell.pack();
shell.open();
while( !shell.isDisposed() ) {
if( !display.readAndDispatch() )
display.sleep();
}
display.dispose();
}
private static void popOut( CTabItem tabItem, Point location ) {
Control control = tabItem.getControl();
tabItem.setControl( null );
Shell itemShell = new Shell( tabItem.getParent().getShell(), SWT.DIALOG_TRIM | SWT.RESIZE );
itemShell.setLayout( new FillLayout() );
control.setParent( itemShell );
control.setVisible( true ); // control is hidden by tabItem.setControl( null ), make visible again
itemShell.pack();
itemShell.setLocation( location );
itemShell.open();
}
While this example uses a CTabFolder it should also be possible to use the (native) TabFolder instead.
What is certainly missing is visual feedback while dragging an item and a means to abort a drag operation (e.g. Esc key), and probably some more things...
I am encountering a strange behaviour using a JLayeredPane. When I click on empty area of the topmost (displayed one) panel, Swing actually clicks on a button that is at that exact position but on a hidden layer.
Here is how I build the JLayeredPane:
volatilityOptionsPane = new OptionsPane( this, optionsBo, mpp.getSkewParams() );
volatilityOptionsPane.setBounds( 0, 0, 300, 750 );
screenOptionsPane = new CScreenOptionsPanel( this, optionsBo );
screenOptionsPane.setBounds( 0, 0, 300, 760 );
divFwdOptionsPane = new DivFwdOptionsPanel( this, optionsBo );
divFwdOptionsPane.setBounds( 0, 0, 300, 770 );
emptyOptionsPane = new JPanel();
emptyOptionsPane.setBounds( 0, 0, 300, 900 );
layeredOptionPane.add( volatilityOptionsPane, new Integer( 3 ) );
layeredOptionPane.add( emptyOptionsPane, new Integer( 2 ) );
layeredOptionPane.add( divFwdOptionsPane, new Integer( 1 ) );
layeredOptionPane.add( cscreenOptionsPane, new Integer( 0 ) );