liquid layout.. make buttons float right - java

With this code the buttons align left, but the glue doesn't expand.. I wanted buttons 2 and 3 to float right.
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
JPanel panelBottom = new JPanel();
frame.getContentPane().add(panelBottom, BorderLayout.SOUTH);
panelBottom.setLayout(new FlowLayout(FlowLayout.LEFT));
JButton btnNewButton = new JButton("1");
panelBottom.add(btnNewButton);
Component glue = Box.createGlue();
panelBottom.add(glue);
JButton btnNewButton_1 = new JButton("2");
panelBottom.add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("3");
panelBottom.add(btnNewButton_2);

In order to use glue, you need to use a BoxLayout.

Related

How to align JLabels and JPanels to the left inside BoxLayout?

I want to align Labels and a Panel (containing Buttons) to the left inside a vertical BoxLayout.
As long as I don't add the panel to the BoxLayout everything is aligned to the left perfectly, but adding it screws everything up.
import javax.swing.*;
import java.awt.*;
public class BoxLayoutDemo{
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JPanel five = new JPanel();
JButton plus = new JButton("+");
JButton minus = new JButton("-");
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
Font testFont = new Font("Arial", Font.BOLD, 20);
JLabel label1 = new JLabel("Label1");
label1.setFont(testFont);
JLabel label2 = new JLabel("Label2");
label2.setFont(testFont);
five.setLayout(new BoxLayout(five, BoxLayout.X_AXIS));
plus.setMinimumSize(new Dimension(30, 30));
plus.setMaximumSize(new Dimension(30, 30));
minus.setMinimumSize(new Dimension(30, 30));
minus.setMaximumSize(new Dimension(30, 30));
five.add(plus);
five.add(minus);
panel.add(label1);
panel.add(five);
panel.add(label2);
panel.setOpaque(true);
panel.setBackground(Color.red);
frame.setMinimumSize(new Dimension(200, 200));
frame.getContentPane().add(panel, BorderLayout.EAST);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
Your components need to have the same "x alignment":
label1.setAlignmentX(Component.LEFT_ALIGNMENT);
label2.setAlignmentX(Component.LEFT_ALIGNMENT);
five.setAlignmentX(Component.LEFT_ALIGNMENT);
Read the section from the Swing tutorial on Fixing Alignment Problems for more information.
You can use invisible components as fillers.
private static Box leftAlignedInHorizontalBox(Component component) {
Box box = Box.createHorizontalBox();
box.add(component);
box.add(Box.createHorizontalGlue());
return box;
}
and then:
panel.add(leftAlignedInHorizontalBox(label1));

How to add 2 columns of checkboxes to Jpanel

I want to add several (about 8) checkboxes in 2 columns below the string and the textbox and above the buttons "Aceptar" and "Cancelar"
I have tried to do a Jpanel inside another Jpanel, but i does not work since the subpanel does not align where i want to (or i dont know how to align it)
JLabel texto_titulo_qliksense = new JLabel();
texto_titulo_qliksense.setText("Nombre del cuadro de mando :");
texto_titulo_qliksense.setBounds(20, 50, 700, 25);
JCheckBox checkBox1 = new JCheckBox("Stream 1");
checkBox1.setBounds(75,100, 50,50);
JCheckBox checkBox2 = new JCheckBox("Stream 2");
checkBox2.setBounds(10,150, 50,50);
textField = new JTextField(80);
textField.setBounds(200,53,220,20);
JLabel texto3 = new JLabel();
texto3.setText("Usuario: " + usuario);
JButton aceptar = new JButton();
aceptar.setText("Aceptar");
aceptar.setActionCommand(comandoAceptar);
aceptar.setBounds(90, 250, 100, 30);
aceptar.addActionListener(this);
JButton cancelar = new JButton();
cancelar.setText("Cancelar");
cancelar.setActionCommand(comandoCancelar);
cancelar.setBounds(250, 250, 100, 30);
cancelar.addActionListener(this);
panel = new JPanel();
panel.setBorder(null);
subpanel = new JPanel();
subpanel.setBorder(BorderFactory.createTitledBorder("Subpanel 1"));
subpanel.setBounds(250, 450, 250, 250);
subpanel.setAlignmentX(java.awt.Component.CENTER_ALIGNMENT);
subpanel.setAlignmentX(java.awt.Component.CENTER_ALIGNMENT);
subpanel.setVisible(true);
panel.setBounds(250, 100, 1000, 1000);
its a very small program, wont be easier just to use absolute positioning?
Easier than this?
JPanel ui = new JPanel(new BorderLayout(4,4));
ui.setBorder(new EmptyBorder(40,4,40,4));
JPanel pageStartPanel = new JPanel(new FlowLayout());
ui.add(pageStartPanel, BorderLayout.PAGE_START);
pageStartPanel.add(new JLabel("Nombre del cuadro de mando:"));
pageStartPanel.add(new JTextField(12));
JPanel checkPanel = new JPanel(new GridLayout(0,2));
ui.add(checkPanel, BorderLayout.CENTER);
for (int ii=1; ii<9; ii++) {
checkPanel.add(new JCheckBox("Checkbox " + ii));
}
JOptionPane.showConfirmDialog(
null,
ui,
"Exportar CdM de Qliksense",
JOptionPane.OK_CANCEL_OPTION);

Borderlayout Gridlayout

public class Editor{
public static void main(String[] args) {
JFrame f = new JFrame("Editpr");
f.setLayout(new GridLayout(5, 2, 25, 54));
JButton button1 = new JButton("1");
JButton button1 = new JButton("10");
JTextArea ausgabe = new JTextArea();
ausgabe.setText("Text");
ausgabe.setEditable(false);
f.add(ausgabe);
f.add(button1);
f.add(Button2)
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(550, 550);
f.setVisible(true);
}
I need help with this Code.
I want this is the grid layout in a Border Layout . The buttons to the center. Textarea should be in BorderLayout below. Who can help a newbie.
Here is an example of a panel with GridLayout, at the center of a panel with BorderLayout , and a text area to the south.
Note that I added random buttons to fill the grid, since your GridLayout has 5 rows and 2 columns (You probably planned to add some more components).
public static void main(final String[] args) {
JFrame f = new JFrame("Editpr");
JPanel content = new JPanel();
content.setLayout(new BorderLayout());
JPanel buttonsPanel = new JPanel();
buttonsPanel.setLayout(new GridLayout(5, 2, 25, 54));
JButton button1 = new JButton("1");
JButton button2 = new JButton("2");
buttonsPanel.add(button1);
buttonsPanel.add(button2);
// random filling to demonstrate the result of the filled grid
buttonsPanel.add(new JButton("3"));
buttonsPanel.add(new JButton("4"));
buttonsPanel.add(new JButton("5"));
buttonsPanel.add(new JButton("6"));
buttonsPanel.add(new JButton("7"));
buttonsPanel.add(new JButton("8"));
buttonsPanel.add(new JButton("9"));
buttonsPanel.add(new JButton("10"));
JTextArea ausgabe = new JTextArea();
ausgabe.setText("Text");
ausgabe.setEditable(false);
content.add(buttonsPanel, BorderLayout.CENTER);
content.add(ausgabe, BorderLayout.SOUTH);
f.setContentPane(content);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(550, 550);
f.setVisible(true);
}

How is it possible to add a container for border layout?

I want to add labels and buttons above and below the border layout. How can I do that? Here is what I did:
import java.awt.*;
import javax.swing.*;
class homework{
public static void main(String[] args) {
JFrame frame= new JFrame("border layout");
frame.setVisible(true);
JLabel label=new JLabel("Container of BorderLayout");
JButton button1 = new JButton("NORTH");
JButton button2 = new JButton("SOUTH");
JButton button3 = new JButton("EAST");
JButton button5 = new JButton("CENTER");
JButton button4 = new JButton("WEST");
JPanel panel1=new JPanel();
JPanel panel2=new JPanel();
frame.add(panel2);
label.setLayout(new FlowLayout(0));
panel2.add(label);
panel1.setLayout(new BorderLayout());
panel1.add(button1,BorderLayout.NORTH);
panel1.add(button2,BorderLayout.SOUTH);
panel1.add(button3,BorderLayout.EAST);
panel1.add(button4,BorderLayout.WEST);
panel1.add(button5,BorderLayout.CENTER);
frame.add(panel1);
frame.pack();
}
}
Above and below of border layout, set new 2 containers (for example JPanel) and make them flow layout. enter image description here
JPanel panel = new JPanel();
frame.getContentPane().add(panel, BorderLayout.NORTH);
JRadioButton rdbtnNewRadioButton = new JRadioButton("New radio button");
panel.add(rdbtnNewRadioButton);
JRadioButton rdbtnNewRadioButton_1 = new JRadioButton("New radio button");
panel.add(rdbtnNewRadioButton_1);
JPanel panel_1 = new JPanel();
frame.getContentPane().add(panel_1, BorderLayout.SOUTH);
JLabel lblNewLabel = new JLabel("New label");
panel_1.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("New label");
panel_1.add(lblNewLabel_1);
add something like that before frame.pack(); code.
Actually what do you mean by above and below? Do you mean north and south? If It is you should have something like this enter image description here
and you should write code this way
import java.awt.*;
class homework{
public static void main(String[] args) {
JFrame frame= new JFrame("border layout");
frame.setVisible(true);
JLabel label=new JLabel("Container of BorderLayout");
JButton button3 = new JButton("EAST");
JButton button5 = new JButton("CENTER");
JButton button4 = new JButton("WEST");
JPanel panel1=new JPanel();
JPanel panel2=new JPanel();
frame.getContentPane().add(panel2);
label.setLayout(new FlowLayout(0));
panel2.add(label);
panel1.setLayout(new BorderLayout());
panel1.add(button3,BorderLayout.EAST);
panel1.add(button4,BorderLayout.WEST);
panel1.add(button5,BorderLayout.CENTER);
frame.getContentPane().add(panel1);
JPanel panel = new JPanel();
panel1.add(panel, BorderLayout.NORTH);
JLabel lblNewLabel = new JLabel("New label");
panel.add(lblNewLabel);
JRadioButton rdbtnNewRadioButton = new JRadioButton("New radio button");
panel.add(rdbtnNewRadioButton);
JPanel panel_1 = new JPanel();
panel1.add(panel_1, BorderLayout.SOUTH);
JLabel lblNewLabel_1 = new JLabel("New label");
panel_1.add(lblNewLabel_1);
JRadioButton rdbtnNewRadioButton_1 = new JRadioButton("New radio button");
panel_1.add(rdbtnNewRadioButton_1);
frame.pack();
}
}
In two conditions, you should add two containers into your code and make them flow layout.

GUI Layout for User Interface

I am trying to get a userinterface page to display correctly on my page. It has to display a certain way on the screen but I cant get the correct Lay out to show up. Any suggestions or Help?! I've tried many things but Java can get a bit confusing. ATTACHED IS A LINK FOR IMAGE OF HOW ITS SUPPOSED TO LOOK
https://courses.eas.asu.edu/cse205/current/assignments/assignment6/assignment6.html
public CreatePanel(Vector accountList, TransferPanel tPanel)
{
this.accountList = accountList;
this.transferPanel = tPanel;
JLabel label1 =new JLabel("Account ID: ");
JLabel label2 = new JLabel("Amount: ");
JTextField field1 = new JTextField();
field1.setPreferredSize(new Dimension(250,70));
JTextField field2 = new JTextField();
field2.setPreferredSize(new Dimension(250,70));
button1 = new JButton("Create an Account");
JTextArea textArea = new JTextArea();
textArea.setPreferredSize(new Dimension(500, 600));
textArea.append("No account");
textArea.setEditable(true);
JPanel panel1 = new JPanel();
panel1.setLayout(new GridLayout(3,3));
panel1.add(label1);
panel1.add(field1);
panel1.add(label2);
panel1.add(field2);
JPanel panel2 = new JPanel();
panel2.setLayout(new BorderLayout());
panel2.add(button1, BorderLayout.SOUTH);
JPanel panel3 = new JPanel();
panel3.setLayout(new BorderLayout());
panel3.add(textArea, BorderLayout.WEST);
add(panel1);
add(panel3);
add(panel2);
//ActionListener listener = new ButtonListener();
//button1.addActionListener(listener);
}

Categories