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, ... );
Related
I have been trying to customize the business theme in Codename One. So far I have added extra buttons. Right now I am trying to get those buttons to be constrained by a y- axis boxlayout, but I am currently getting a IllegalArgumentException. I have set the form to a border layout:
Form hi = new Form("Welcome", new BorderLayout(BorderLayout.CENTER_BEHAVIOR_CENTER_ABSOLUTE));
Button Customer = new Button("Customer");
Button gpsProduct = new Button("Find A product Near You");
Button learnMore = new Button("Learn More");
Button Website = new Button("Visit Our Website");
hi.add(BoxLayout.Y_AXIS, Customer).
add(BoxLayout.Y_AXIS, learnMore).
add(BoxLayout.Y_AXIS, gpsProduct).
add(BoxLayout.Y_AXIS, Website);
hi.show();
Box layout Y isn't a constraint for border layout. It's unclear how you wanted this to look but I'm guessing you want something like this which will arrange the components one after the other:
Form hi = new Form("Welcome", BoxLayout.y());
Button Customer = new Button("Customer");
Button gpsProduct = new Button("Find A product Near You");
Button learnMore = new Button("Learn More");
Button Website = new Button("Visit Our Website");
hi.add(Customer).
add(learnMore).
add(gpsProduct).
add(Website);
hi.show();
Here are two nested examples that place the box in a border layout parent:
Form hi = new Form("Welcome", new BorderLayout());
Button Customer = new Button("Customer");
Button gpsProduct = new Button("Find A product Near You");
Button learnMore = new Button("Learn More");
Button Website = new Button("Visit Our Website");
Container box = new Container(BoxLayout.y());
box.add(Customer).
add(learnMore).
add(gpsProduct).
add(Website);
hi.add(BorderLayout.CENTER, box);
hi.show();
This can be written in shorthand as:
Form hi = new Form("Welcome", new BorderLayout());
Button Customer = new Button("Customer");
Button gpsProduct = new Button("Find A product Near You");
Button learnMore = new Button("Learn More");
Button Website = new Button("Visit Our Website");
hi.add(BorderLayout.CENTER,
BoxLayout.encloseY(Customer, learnMore, gpsProduct, Website););
hi.show();
I want to put 3 fixed size group in the composite in A side. And I want to place the image and the label middle of this group. My example image and code is below. The problem is the groups are resize according to label size in them and labels were written top of the group, but I want the place groups as equal fixed size to cover width of A side and labels should be vertically middle of the group.
private void designComposite() {
Section sectionA = toolkit.createSection(form.getBody(), Section.TITLE_BAR);
sectionA.setText("A");
Composite sectionClientA = toolkit.createComposite(sectionA);
sectionClientA.setLayout(new RowLayout());
Composite dynamicDataComp = toolkit.createComposite(sectionClientA);
dynamicDataComp.setLayout(new RowLayout());
Group group_incom = new Group(dynamicDataComp, SWT.NONE);
group_incom.setLayout(new RowLayout());
Label lbl_img_incom = new Label(group_incom, SWT.CENTER);
Image img_incom = new Image(lbl_img_incom.getDisplay(),
"<path>");
lbl_img_incom.setImage(img_incom);
group_incom.setText("# of Incoming Messages :");
Label lbl_incomMsg = toolkit.createLabel(group_incom, "99", SWT.CENTER | SWT.VERTICAL);
Font incomFont = new Font(lbl_incomMsg.getDisplay(), new FontData("Arial", 12, SWT.BOLD));
lbl_incomMsg.setFont(incomFont);
lbl_incomMsg.pack();
Group group_outgo = new Group(dynamicDataComp, SWT.NONE);
group_outgo.setLayout(new RowLayout());
Label lbl_img_outgo = new Label(group_outgo, SWT.CENTER);
Image img_outgo = new Image(lbl_img_outgo.getDisplay(),
"<path>");
lbl_img_outgo.setImage(img_outgo);
group_outgo.setText("# of Outgoing Messages :");
Label lbl_outgoMsg = toolkit.createLabel(group_outgo, "145639612", SWT.CENTER);
Font outgoFont = new Font(lbl_outgoMsg.getDisplay(), new FontData("Arial", 13, SWT.BOLD));
lbl_outgoMsg.setFont(outgoFont);
lbl_outgoMsg.pack();
Group group_error = new Group(dynamicDataComp, SWT.NONE);
group_error.setLayout(new RowLayout());
Label lbl_img_error = new Label(group_error, SWT.CENTER);
Image img_error = new Image(lbl_img_error.getDisplay(),
"<path>");
lbl_img_error.setImage(img_error);
group_error.setText("# of Error Messages :");
Label lbl_errorMsg = toolkit.createLabel(group_error, "345", SWT.CENTER);
Font errorFont = new Font(lbl_errorMsg.getDisplay(), new FontData("Arial", 13, SWT.BOLD));
lbl_errorMsg.setFont(errorFont);
lbl_errorMsg.pack();
sectionA.setClient(sectionClientA);
}
Use the GridLayout and set the columns equal width:
Composite dynamicDataComp = new Composite(parent, SWT.NONE);
dynamicDataComp.setLayout(new GridLayout(3, true));
dynamicDataComp.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
Group group_incom = new Group(dynamicDataComp, SWT.NONE);
group_incom.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
group_incom.setLayout(new RowLayout());
//...
One way is to use GridLayout for the parent composite and tell it use 3 equal sized columns:
Composite dynamicDataComp = toolkit.createComposite(sectionClientA);
dynamicDataComp.setLayout(new GridLayout(3, true));
Could anybody help me?
I have a table in SWT and simply I want to show some table items but the table only shows one item and the others with vertical scroll. I want to show everything, without scrolling. I tried with the option SWT_NO_SCROLL but is not working. I have a method that creates the table and other that populates it creating a new table item, they´re working good, the problem is that only shows the first item and the other are scrolling.
My code is:
private void createTable(Composite parent){
table = new Table (parent, SWT.BORDER);
TableColumn tcFile = new TableColumn(table, SWT.LEFT);
TableColumn tcStatus = new TableColumn(table, SWT.LEFT);
tcFile.setText("File");
tcStatus.setText("Status");
tcFile.setWidth(500);
tcStatus.setWidth(500);
table.setVisible(true);
table.setHeaderVisible(true);
}
private void populateTable(String file, String status){
TableItem item = new TableItem(table, SWT.LEFT);
item.setText(new String[] { file, status});
}
Composite top = new Composite(parent, SWT.WRAP);
GridLayout layout = new GridLayout();
layout.marginHeight = -5;
layout.marginWidth = 0;
top.setLayout(layout);
Composite banner = new Composite(top, SWT.WRAP);
banner.setLayoutData(new GridData(GridData.FILL, GridData.VERTICAL_ALIGN_BEGINNING, false, false));
layout = new GridLayout();
layout.marginHeight = 0;
layout.marginWidth = 10;
layout.numColumns = 5;
banner.setLayout(layout);
createTable(top);
You haven't specified any layout data for the Table, try something like:
GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
table.setLayoutData(data);
If you don't have anything else that is setting the dialog/window size you may need to specify a table height hint:
GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
data.heightHint = 200; // Vertical size for table
table.setLayoutData(data);
I'm having issues getting libgdxs scrollpane control to work. The code below shows control setup for a simple layout with a label, a List of items inside a scrollpane, and a button. The problem is my scroll pane doesn't show anything other than the vScroll/vScrollKnob ninepatch (that tiny white square)
it just looks like this:
screenshot.
private void setupLayout()
{
String[] listEntries = {"1","2","3","4","5"};
ListStyle listStyle = new ListStyle();
NinePatch example = new NinePatch(new Texture(Gdx.files.internal("data/example.9.png")));
listStyle.selectedPatch = example;
listStyle.font = new BitmapFont();
mList = new List(listEntries,listStyle);
ScrollPaneStyle paneStyle = new ScrollPaneStyle();
paneStyle.vScroll = example;
paneStyle.vScrollKnob = example;
mListScroll = new ScrollPane(mList,paneStyle);
mListScroll.setScrollingDisabled(true, false);
mListScroll.width = 500;
mListScroll.height = 500;
LabelStyle ls = new LabelStyle();
ls.font = new BitmapFont();
ls.fontColor = new Color(1.0f, 1.0f, 1.0f, 1.0f);
mLabel = new Label("Label", ls);
TextButtonStyle buttonStyle = new TextButtonStyle();
buttonStyle.font = new BitmapFont();
mButton = new TextButton("Button",buttonStyle);
Table table = new Table();
table.add(mLabel);
table.row();
table.add(mButton);
table.row();
table.add(mListScroll);
mStage.addActor(table);
}
It works as expected if i don't user the scroll pane and add the list directly to the table like this:
Table table = new Table();
table.add(mLabel);
table.row();
table.add(mButton);
table.row();
table.add(mList); //changed mListScroll(scrollpane class) to mList(List class)
mStage.addActor(table);
screenshot
But then it will extend out the bottom of my screen when there are too many items. What am I doing wrong here? Is there another parameter i need to set on the scrollpane?
I believe the issue you are having is how you are adding things to the table. I would suggest the following code instead of how you are doing it:
Table table = new Table();
table.add(mLabel);
table.row();
table.add(mButton);
table.row();
// Changing the table layout size itself.
table.add(mListScroll).size(500, 500);
mStage.addActor(table);
For more a more detailed explanation refer to TableLayout the quick start here shows you more how using tables for laying out objects.
I have different controls placed on a table using TableEditor.
...
TableItem [] items = table.getItems ();
for (int i=0; i<items.length; i++) {
TableEditor editor = new TableEditor (table);
final Text text1 = new Text (table, SWT.NONE);
text1.setText(listSimOnlyComponents.get(i).getName());
text1.setEditable(false);
editor.grabHorizontal = true;
editor.setEditor(text1, items[i], 0);
editor = new TableEditor (table);
final CCombo combo1 = new CCombo (table, SWT.NONE);
combo1.setText("");
Set<String> comps = mapComponentToPort.keySet();
for(String comp:comps)
combo1.add(comp);
editor.grabHorizontal = true;
editor.setEditor(combo1, items[i], 1);
} //end of for
...
When I try to get the text on the table using getItem(i).getText, I get empty string
...
TableItem [] items = table.getItems ();
for(int i=0; i<items.length; i++) {
TableItem item = items[i];
String col0text = items[i].getText(0); //this text is empty
String col1text = items[i].getText(1); //this text is empty
}
...
Why does getText returns empty strings even when I have text appearing on the table?
Instead of using the text property, you might consider using the data property instead.
Benefits:
you can attach the data (e.g. a control or complex data structure) directly to the table item.
the only thing your table item creation need to know from your table cell editor is the object reference to be stored in the data property.
you don't need that event handling stuff (just read the controls data when you really need it).
Create:
TableItem item = new TableItem(table, SWT.None);
TableEditor editor = new TableEditor(table);
Button checkbox = new Button(table, SWT.CHECK);
checkbox.pack();
editor.setEditor(checkbox,item,0);
item.setData("cb",checkbox); // using key enables you to add more pieces of complex data
Read:
for (TableItem item : table) {
Button checkbox = (Button) item.getData("cb");
if (checkbox.getSelection()) { /* ... do whatever you want */ }
}
When the table shows up, the checkbox is visible and can be clicked. Using the setText method fails in case of transparent background controls -> you will see the text of the table items cell below
your control (I tried this).
Anyway it would be much easier if it would be possible to extend the table item class to hide even the data key. But as usual, subclassing is denied.
in the event listeners for the controls I added
item.setText call
...
combo1.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent evt) {
String sel = combo2.getText();
item.setText(ComponentPortToConnectCol, sel);
}});
...
This gives me the desired result. Thanks OTisler for the clue
How are you setting up your table? I copied your code to debug it and set up the table as shown below (first for loop)
I tried but couldn't reproduce the problem you're seeing. It might be the way you are adding things to the table.
TableEditor Examples
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
final Table table = new Table(shell, SWT.BORDER | SWT.MULTI);
for (int i = 0; i < 3; i++) {
TableItem item = new TableItem(table, SWT.NONE);
item.setText(new String[] { "" + i, "" + i, "" + i });
}
TableItem [] items = table.getItems();
for (int i=0; i<items.length; i++) {
TableEditor editor = new TableEditor (table);
final Text text1 = new Text (table, SWT.NONE);
text1.setText("TEST");
text1.setEditable(false);
editor.grabHorizontal = true;
editor.setEditor(text1, items[i], 0);
editor = new TableEditor (table);
final CCombo combo1 = new CCombo (table, SWT.NONE);
combo1.setText("");
String[] comps = {"A","B","C"};
for(String comp:comps)
combo1.add(comp);
editor.grabHorizontal = true;
editor.setEditor(combo1, items[i], 1);
} //end of for
TableItem [] items2 = table.getItems ();
for(int i=0; i<items2.length; i++) {
TableItem item = items2[i];
String col0text = items2[i].getText(0); //returns '0' first for loop
}