How do I apply multiple layouts in Codename One? - java

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

Related

ClassCastException when placing RadioGroupFieldEditor in FormLayout

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, ... );

Java Swing TextField looking very small

I am trying to add a textfield next to some check boxes, but for some reason it looks only 1 char in size?
JButton addStaff = new JButton("Add Staff");
westPanel.add(addStaff);
// bind the button with the listener
addStaff.addActionListener(new StaffHandler());
c1 = new JCheckBox("Home Only?");
westPanel.add(c1);
c2 = new JCheckBox("Offer ShortHand?");
westPanel.add(c2);
t1 = new JTextField("");
westPanel.add(t1);
My output is the following, which looks a bit rubbish.
You can use the constructor new JTextField(int columns)
e.g. JTextField t1 = new JTextField(20);

TextArea Issues in GWT

Whenever I create a new TextArea, all the other textareas next to it seem to be shifted downward.
Can anyone show me the proper way to create two textareas side by side and have them each at a specific location? Right now, text area 2 goes below text area 1 even though i want them at the same level.
Code Now:
FlexTable flexTable = new FlexTable();
VerticalPanel mainPanel = new VerticalPanel();
TextArea t1 = new TextArea();
TextArea t2 = new TextArea();
t1.setCharacterWidth(30);
t1.setVisibleLines(25);
t2.setCharacterWidth(30);
t2.setVisibleLines(25);
flexTable.setWidget(74,10,t1);
flexTable.setWidget(74,70,t2);
mainPanel.add(flexTable);
RootPanel.get().add(mainPanel);
If you want both TextArea to show up in the same FlexTable cell you should wrap those two TextArea with a HorizontalPanel.
HorizontalPanel horizontalPanel = new HorizontalPanel();
FlexTable flexTable = new FlexTable();
...
TextArea t1 = new TextArea();
TextArea t2 = new TextArea();
...
horizontalPanel.add(t1);
horizontalPanel.add(t2);
flexTable.setWidget(74, 10, horizontalPanel);
RootPanel.get().add(mainPanel);

Writing actionListner to display form input on console upon confirmation

I am currently constructing a GUI which allows me to add new cruises to the system. I want to write an actionListner which once the user clicks on "ok" then the form input will be displayed as output on the console.
I currently have the following draft to complete this task:
ok = new JButton("Add Cruise");
ok.setToolTipText("To add the Cruise to the system");
ok.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event){
int selected = typeList2.getSelectedIndex();
String tText = typeList2[selected];
Boolean addtheCruise = false;
addtheCruise = fleet.addCruise();
fleet.printCruise();
if (addtheCruise)
{
frame.setVisible(false);
}
else
{ // Report error, and allow form to be re-used
JOptionPane.showMessageDialog(frame,
"That Cruise already exists!", "Error",
JOptionPane.ERROR_MESSAGE);
}
}
});
buttonPanel.add(ok);
Here is the rest of the code to the Form input frame:
contentPane2 = new JPanel (new GridLayout(18, 1)); //row, col
nameInput = new JLabel ("Input Cruise Name:");
nameInput.setForeground(normalText);
contentPane2.add(nameInput);
Frame2.add(contentPane2);
Cruisename = new JTextField("",10);
contentPane2.add(Cruisename);
Frame2.add(contentPane2, BorderLayout.CENTER);
Frame2.setVisible(true);
confirmPanel = new JPanel ();
confirmPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
confirmPanel.setBorder(BorderFactory.createLineBorder(Color.PINK));
addItinerary = new JButton("Add Itinerary");
confirmPanel.add(Add);
confirmPanel.add(addItinerary );
Frame2.add(confirmPanel, BorderLayout.PAGE_END);
Frame2.setVisible(true);
contentPane2.add(Cruisename);
Frame2.add(contentPane2);
// add spacing between comboboxes
contentPane2.add(Box.createRigidArea(new Dimension(5,0)));
contentPane2.setBorder(BorderFactory.createLineBorder(Color.black));
//Label for start and end location jcombobox
CruiseMessage = new JLabel ("Enter Start and End Location of new Cruise:");
CruiseMessage.setForeground(normalText);
contentPane2.add(CruiseMessage);
Frame2.add(contentPane2);
/**
* creating start location JComboBox
*/
startL = new JComboBox();
final JComboBox typeList2;
final String[] typeStrings2 = {
"Select Start Location", "Tobermory","Oban", "Isle of Mull", "Isle of Harris",
"Lewis and Harris", "Stornoway", "Skye", "Portree"};
startL = new JComboBox(typeStrings2);
contentPane2.add(startL);
Frame2.add(contentPane2, BorderLayout.CENTER);
Frame2.setVisible(true);
// add spacing between comboboxes
contentPane2.add(Box.createRigidArea(new Dimension(5,0)));
/**
* creating end location JComboBox
*/
endL = new JComboBox();
final JComboBox typeList3;
final String[] typeStrings3 = {
"Select End Location", "Tobermory","Oban", "Isle of Mull", "Isle of Harris",
"Lewis and Harris", "Stornoway", "Skye", "Portree"};
endL = new JComboBox(typeStrings3);
contentPane2.add(endL);
Frame2.add(contentPane2, BorderLayout.CENTER);
Frame2.setVisible(true);
// add spacing between comboboxes
contentPane2.add(Box.createRigidArea(new Dimension(5,0)));
//Label for select ship jcombobox
selectShipM = new JLabel ("Select Ship to assign Cruise:");
selectShipM.setForeground(normalText);
contentPane2.add(selectShipM);
Frame2.add(contentPane2);
/**
* creating select ship JCombobox
* select ship to assign cruise
*/
selectShip = new JComboBox();
final JComboBox typeList4;
final String[] typeStrings4 = {
"Select Ship", "Dalton Princess", "Stafford Princess" };
selectShip = new JComboBox(typeStrings4);
contentPane2.add(selectShip);
Frame2.add(contentPane2, BorderLayout.CENTER);
Frame2.setVisible(true);
I need all form inputs from the code above to be displayed on the console upon completion.
Summary:
1. I have two ships in one fleet
2. To add new cruise, all fields (Name, start date, end date, ship) must be selected.
The Problem:
1. I keep coming up with errors when creating " fleet = new Fleet();" in my constructor. Even though I have declared it in my class.
2. In the draft code below, line 5 states "typeList2", however, I have two JComboBox's - two different type Strings for both drop down menu's (Shown in the rest of the code). How do I input both typeLists to the output rather than just one?
Thank you.

libgdx scrollpane doesn't display

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.

Categories

Resources