How can I position a Button (vertically) at the center using a FormLayout (see here)? Using:
final Button button = new Button(shell, SWT.NONE);
button.setText("Button");
final FormData layoutData = new FormData();
layoutData.top = new FormAttachment(50);
layoutData.left = new FormAttachment(0);
layoutData.right = new FormAttachment(100);
button.setLayoutData(layoutData);
I end up with
Which is not surprising, since I told it to put the top of the button at the center (layoutData.top = new FormAttachment(50);). How can I instead put the center of the button at the center?
You can specify an offset with the constructor:
new FormAttachment(int numerator, int offset)
Looks like this:
You can compute the offset using:
final Button button = new Button(shell, SWT.NONE);
button.setText("Button");
final FormData layoutData = new FormData();
/* Compute the offset */
int offset = -button.computeSize(SWT.DEFAULT, SWT.DEFAULT).y / 2;
/* Create the FormAttachment */
layoutData.top = new FormAttachment(50, offset);
layoutData.left = new FormAttachment(0);
layoutData.right = new FormAttachment(100);
button.setLayoutData(layoutData);
Related
I'm making a sales system, and in the area where the total sums are described, I can't click on the buttons.
I tried to remove buttons from the gridpane and they work for me but when I integrate them into the gridpane they lose focus or stop working
Pane root = new Pane();
Separator top = new Separator(Orientation.HORIZONTAL);
top.setPrefWidth(1310);
Separator left = new Separator(Orientation.VERTICAL);
left.setPrefHeight(150);
left.setPadding(new Insets(0,0,10,0));
Separator low = new Separator(Orientation.HORIZONTAL);
low.setPadding(new Insets(140,10,0,0));
low.setPrefWidth(1323);
Separator right = new Separator(Orientation.VERTICAL);
right.setPrefHeight(150);
right.setPadding(new Insets(0,0,10,1310));
Label descuento = new Label("Descuento: ");
Label sub = new Label("Sub-Total: ");
Label iva = new Label("Iva: ");
Label total = new Label("Total: ");
Label rsub = new Label("0000.00");
Label riva= new Label("16%");
Label rtotal = new Label("0.0000");
Button btn_generate = new Button("generate");
MenuItem opc = new MenuItem("Opc");
MenuItem opc1= new MenuItem("opc1");
MenuButton menu1 = new MenuButton("Type",null,opc,opc1);
MenuItem opc2 = new MenuItem("opc2");
MenuItem opc3 = new MenuItem("opc3");
MenuButton menu2 = new MenuButton("Opc",null,opc2,opc3);
GridPane Gtotal = new GridPane();
Gtotal.setHgap(20);
Gtotal.setVgap(10);
Gtotal.setPadding(new Insets(10,0,0,10));
ColumnConstraints column1 = new ColumnConstraints(rtotal.getPrefWidth(),rtotal.getPrefWidth(),rtotal.getPrefWidth());
ColumnConstraints column2 = new ColumnConstraints(total.getPrefWidth()+450,total.getPrefWidth()+450,total.getPrefWidth()+450);
ColumnConstraints column3 = new ColumnConstraints(riva.getPrefWidth(),riva.getPrefWidth(),riva.getPrefWidth());
ColumnConstraints column4 = new ColumnConstraints(iva.getPrefWidth()+330,iva.getPrefWidth()+330,iva.getPrefWidth()+330);
ColumnConstraints column5 = new ColumnConstraints(rsub.getPrefWidth(),rsub.getPrefWidth(),rsub.getPrefWidth());
ColumnConstraints column6 = new ColumnConstraints(sub.getPrefWidth(),sub.getPrefWidth(),sub.getPrefWidth());
Gtotal.getColumnConstraints().add(0,column1);
Gtotal.getColumnConstraints().add(0,column2);
Gtotal.getColumnConstraints().add(0,column3);
Gtotal.getColumnConstraints().add(0,column4);
Gtotal.getColumnConstraints().add(0,column5);
Gtotal.getColumnConstraints().add(0,column6);
GridPane.setConstraints(descuento, 0, 0);
GridPane.setConstraints(menu1, 1, 0);
GridPane.setConstraints(menu2, 2, 0);
GridPane.setConstraints(sub, 0, 1);
GridPane.setConstraints(rsub, 1, 1);
GridPane.setConstraints(iva, 2, 1);
GridPane.setHalignment(iva, HPos.RIGHT);
GridPane.setConstraints(riva, 3, 1);
GridPane.setConstraints(total, 4, 1);
GridPane.setHalignment(total, HPos.RIGHT);
GridPane.setConstraints(rtotal, 5, 1);
GridPane.setConstraints(btn_generate, 5, 2);
Gtotal.getChildren().addAll(descuento,sub,rsub,iva,riva,total,rtotal,btn_generate,menu1,menu2);
root.getChildren().addAll(top,left,Gtotal,low,right);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
I hope that when running the program I can click on the buttons.
Marcos Antonio You are really making work for yourself not using Scene Builder
That said here is 3 lines of code that make a button fire an event
Button btnOK = new Button("OK");
btnOK.addEventHandler(ActionEvent.ACTION,filter);
btnOK.setOnAction((ActionEvent evt) -> {
The problem was the separators that overlapped the other layout by the padding
I have created an SWT Table and added columns to it. I want to apply bold font to header row alone. So my code looks like below
Table table= new Table(top, tableStyle);
Font font = new Font(null, StringUtils.EMPTY, 9, SWT.BOLD);
for (int i = 0; i < titles.length; i++)
{
new TableColumn(table, SWT.NONE);
header.setFont(i, font);
header.setText(i, titles[i]);
}
font.dispose();
In the above code, I have disposed as it is a good practice to do. But this is removing the font style from the header. If I remove the last line, font remains applied.
Is there any mistake over here? Or is this the expected behavior?
You need to wait and only dispose() the Font when you no longer need it. You could tie the disposal to the dispose event of the table so you don't have to manually dispose it:
public static void main(String[] args)
{
final Display d = new Display();
Shell s = new Shell(d);
s.setLayout(new FillLayout());
Table table = new Table(s, SWT.NONE);
Font font = new Font(null, "", 12, SWT.BOLD);
for (int i = 0; i < 3; i++)
{
TableItem item = new TableItem(table, SWT.NONE);
item.setFont(font);
item.setText("" + i);
}
table.addListener(SWT.Dispose, e -> font.dispose());
s.pack();
s.open();
while (!s.isDisposed())
{
if (!d.readAndDispatch())
d.sleep();
}
d.dispose();
}
I am trying to create a simple menu interface with 4 rows of various buttons and labels using GridLayout with FlowLayout inside each grid for organising the elements. However the space for the buttons and labels which should only take 1 line takes up a huge amount of space.
This is what my interface looks like minimized:
This is what it looks like maximized:
I am looking to set the maximum size of the labels panels/grid so that it only takes a small amount of space.
I am trying to make all the elements visible without anything being hidden with as small a window size as possible like this:
This is my code:
public class Window extends JFrame{
public Window() {
super("TastyThai Menu Ordering");
}
public static void main(String[] args) {
Window w = new Window();
w.setSize(500, 500);
w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel title = new JLabel("TastyThai Menu Order", SwingConstants.CENTER);
title.setFont(title.getFont().deriveFont(32f));
//generate page title
Container titlePanel = new JPanel(); // used as a container
titlePanel.setBackground(Color.WHITE);
FlowLayout flow = new FlowLayout(); // Create a layout manager
titlePanel.setLayout(flow);// assign flow layout to panel
titlePanel.add(title); // add label to panel
w.getContentPane().add(BorderLayout.NORTH,titlePanel);
//generate row containers
Container r1 = new JPanel(new FlowLayout());
Container r2 = new JPanel(new FlowLayout());
Container r3 = new JPanel(new FlowLayout());
Container r4 = new JPanel(new FlowLayout());
//generate mains radio buttons
Container mains = new JPanel(new GridLayout(7, 0));
mains.setBackground(Color.RED);
JLabel mainsHeader = new JLabel("Mains");
mains.add(mainsHeader);
String[] mainsChoices = {"Vegetarian", "Chicken", "Beef", "Pork", "Duck", "Seafood Mix"};
JRadioButton[] mainsRadioButton = new JRadioButton[6];
ButtonGroup mainsButtons = new ButtonGroup();
for(int i = 0; i < mainsChoices.length; i++) {
mainsRadioButton[i] = new JRadioButton(mainsChoices[i]);
mains.add(mainsRadioButton[i]);
mainsButtons.add(mainsRadioButton[i]);
}
//generate noodles radio buttons
Container noodles = new JPanel(new GridLayout(7, 0));
noodles.setBackground(Color.GREEN);
JLabel noodlesHeader = new JLabel("Noodles");
noodlesHeader.setFont(noodlesHeader.getFont().deriveFont(24f));
noodles.add(noodlesHeader);
String[] noodlesChoices = {"Pad Thai", "Pad Siew", "Ba Mee"};
JRadioButton[] noodlesRadioButton = new JRadioButton[3];
ButtonGroup noodlesButtons = new ButtonGroup();
for(int i = 0; i < noodlesChoices.length; i++) {
noodlesRadioButton[i] = new JRadioButton(noodlesChoices[i]);
noodles.add(noodlesRadioButton[i]);
noodlesButtons.add(noodlesRadioButton[i]);
}
//generate sauces radio buttons
Container sauces = new JPanel(new GridLayout(7, 0));
sauces.setBackground(Color.BLUE);
JLabel saucesHeader = new JLabel("Sauce");
saucesHeader.setFont(saucesHeader.getFont().deriveFont(24f));
sauces.add(saucesHeader);
String[] saucesChoices = {"Soy Sauce", "Tamarind Sauce"};
JRadioButton[] saucesRadioButton = new JRadioButton[2];
ButtonGroup saucesButtons = new ButtonGroup();
for(int i = 0; i < saucesChoices.length; i++) {
saucesRadioButton[i] = new JRadioButton(saucesChoices[i]);
sauces.add(saucesRadioButton[i]);
saucesButtons.add(saucesRadioButton[i]);
}
//generate extras check boxes
Container extras = new JPanel(new GridLayout(7, 0));
extras.setBackground(Color.YELLOW);
JLabel extrasHeader = new JLabel("Extra");
extrasHeader.setFont(extrasHeader.getFont().deriveFont(24f));
extras.add(extrasHeader);
String[] extrasChoices = {"Mushroom", "Egg", "Broccoli", "Beansrpout", "Tofu"};
JCheckBox[] extrasBoxes = new JCheckBox[5];
for(int i = 0; i < extrasChoices.length; i++) {
extrasBoxes[i] = new JCheckBox(extrasChoices[i]);
extras.add(extrasBoxes[i]);
}
JLabel selectionPrice = new JLabel("Selection Price: $ ");
JLabel selectionPriceVal = new JLabel("_______________");
JButton addToOrder = new JButton("Add to Order");
JLabel totalPrice = new JLabel("Total Price: $ ");
JLabel totalPriceVal = new JLabel("_______________");
JButton clearOrder = new JButton("Clear Order");
JRadioButton pickUp = new JRadioButton("Pick Up");
JRadioButton delivery = new JRadioButton("Delivery");
ButtonGroup pickupDelivery = new ButtonGroup();
pickupDelivery.add(pickUp);
pickupDelivery.add(delivery);
JButton completeOrder = new JButton("Complete Order");
Container menuSelection = new JPanel(new GridLayout(4,0));
menuSelection.add(r1);
r1.add(mains);
r1.add(noodles);
r1.add(sauces);
r1.add(extras);
menuSelection.add(r2);
r2.add(selectionPrice);
r2.add(selectionPriceVal);
r2.add(addToOrder);
menuSelection.add(r3);
r3.add(totalPrice);
r3.add(totalPriceVal);
r3.add(clearOrder);
menuSelection.add(r4);
r4.add(pickUp);
r4.add(delivery);
r4.add(completeOrder);
w.getContentPane().add(BorderLayout.CENTER, menuSelection);
w.setVisible(true);
}
}
GridLayout does not support that. All rectangles have the same size.
Take a look at the GridBagLayout, which supports dynamic resizing and much more.
ScrolledComposite scrollFormItemComposite = new ScrolledComposite(c, SWT.V_SCROLL | SWT.BORDER);
GridData formItemCompositeGridData = new GridData(SWT.FILL, SWT.FILL, true, true);
formItemCompositeGridData.horizontalSpan = 4;
scrollFormItemComposite.setLayoutData(formItemCompositeGridData);
GridLayout formItemLayout = new GridLayout(1, true);
formItemLayout.marginHeight = 0;
formItemLayout.marginWidth = 0;
formItemLayout.verticalSpacing = 0;
formItemLayout.horizontalSpacing = 0;
scrollFormItemComposite.setLayout(formItemLayout);
Composite formItemComposite = new Composite(scrollFormItemComposite, SWT.RESIZE);
formItemComposite.setLayout(new FillLayout(SWT.VERTICAL|SWT.HORIZONTAL));
FormProvider formProvider = new FormProvider();
formProvider.createForms(formItemComposite);
scrollFormItemComposite.setContent(formItemComposite);
scrollFormItemComposite.setExpandHorizontal(true);
scrollFormItemComposite.setExpandVertical(true);
scrollFormItemComposite.setMinSize(formItemComposite.computeSize(300,SWT.DEFAULT));
The previous lines of code give me the following output. I want the contents to shrink as the form becomes smaller. I dont want the horizontal scroll bar to appear. How do I prevent the text box from being hidden?
I am trying the following code to set scrollcomposite for TabItem "item2". But couldn't get the scroll bar.
Here I created two tabItem , where I need to set scrollcomposite / scrollbar for only item2 not for item1
Display display = new Display();
final Shell shell = new Shell(display);
final TabFolder tabFolder = new TabFolder(shell, SWT.BORDER);
Rectangle clientArea = shell.getClientArea();
tabFolder.setLocation(clientArea.x, clientArea.y);
// First Tab Item
TabItem item = new TabItem(tabFolder, SWT.NONE);
item.setText("TabItem " + 1);
Composite comp = new Composite(tabFolder, SWT.NONE);
GridLayout gl = new GridLayout();
GridData wgd = new GridData(GridData.FILL_BOTH);
comp.setLayout(gl);
comp.setLayoutData(wgd);
Button button = new Button(comp, SWT.PUSH);
button.setText("Page " + 1);
Button button2 = new Button(comp, SWT.PUSH);
button2.setText("Page " + 1);
Button button3 = new Button(comp, SWT.PUSH);
button3.setText("Page " + 1);
Button button4 = new Button(comp, SWT.PUSH);
button4.setText("Page " + 1);
item.setControl(comp);
ScrolledComposite sc = new ScrolledComposite(tabFolder, SWT.BORDER
| SWT.H_SCROLL | SWT.V_SCROLL);
// second tab item
TabItem item2 = new TabItem(tabFolder, SWT.NONE);
item2.setText("TabItem " + 1);
Composite comp2 = new Composite(tabFolder, SWT.NONE);
GridLayout gl2 = new GridLayout();
GridData wgd2 = new GridData(GridData.FILL_BOTH);
comp2.setLayout(gl2);
comp2.setLayoutData(wgd2);
Button buttonq = new Button(comp2, SWT.PUSH);
buttonq.setText("Page " + 1);
Button button2q = new Button(comp2, SWT.PUSH);
button2q.setText("Page " + 1);
sc.setContent(comp2);
sc.setExpandHorizontal(true);
sc.setExpandVertical(true);
sc.setMinSize(comp2.computeSize(SWT.DEFAULT, SWT.DEFAULT));
sc.setShowFocusedControl(true);
item2.setControl(comp2);
tabFolder.pack();
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
When I added following code, tabItem2 was empty:
item2.setControl(comp2);
Please help me to solve this
Several things here.
First use layouts for everything. The tabFolder.setLocation is causing confusion, use FillLayout instead.
So replace
Rectangle clientArea = shell.getClientArea();
tabFolder.setLocation(clientArea.x, clientArea.y);
with
shell.setLayout(new FillLayout());
Second, the Composite for the second tab must be owned by the ScrolledComposite.
So change
Composite comp2 = new Composite(tabFolder, SWT.NONE);
to
Composite comp2 = new Composite(sc, SWT.NONE);
Finally the ScrolledComposite must be the control for the second tab, so change
item2.setControl(comp2);
to
item2.setControl(sc);