How to arrange components using BorderLayout? - java

I'm trying to get my GUI to appear as such:
Grocery Cart [Refill]
(TextArea)
I am currently using BorderLayout and I would like to stick with it. How can I get the text area underneath the JLabel and the JButton whilst being in the same JPanel? Here is my code for the specific area:
How do I add the text box underneath the two side by side? Whenever I add it, it just goes next to them.
JPanel newPanel = new JPanel();
JLabel label = new JLabel("Grocery Cart");
label.setFont(new Font("Arial", Font.BOLD, 20));
newPanel.add(label);
contentPane.add(newPanel, BorderLayout.WEST) ;
JButton btnNewButton = new JButton("Refill");
btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 20));
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
newPanel.add(btnNewButton);

If my understanding is correct, here's what you need to do:
JPanel mainPanel = new JPanel(new BorderLayout());
JPanel eastPanel = new JPanel(new BorderLayout());
JTextArea area = new JTextArea("Test content");
JLabel label = new JLabel("Grocery Cart");
label.setFont(new Font("Arial", Font.BOLD, 20));
mainPanel.add(label, BorderLayout.WEST);
JButton btnNewButton = new JButton("Refill");
btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 20));
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
eastPanel.add(btnNewButton, BorderLayout.WEST);
eastPanel.add(area, BorderLayout.CENTER);
mainPanel.add(eastPanel, BorderLayout.CENTER);
contentPane.add(mainPanel, BorderLayout.NORTH);
The main idea is that in order to construct complex layouts with simple layout types such as border and flow, you have to use container hierarchy and get creative with a combination of flow and border layouts.
In my example the label and button aren't resizable and always have their widths equal to their preferred widths. The text area, however is resizable and takes up its container's remaining width.
Note, that all components added to mainPanel are resizable vertically. In order to keep mainPanel to its preferred height you place it to the contentPane's BorderLayout.NORTH or SOUTH for that matter.

Related

JPanel not displaying JLabel

I am trying to make an application, which displays a JLabel and a button, which if clicked switches to another jPanel. For some reason my JLabel is not displaying at all in either case. I would appreciate an expert eye to look over my code and see what I am doing wrong. Thanks in advance.
HomeScreenUI(){
//frame
JFrame frame = new JFrame("Opisa");
//panels, one before button click and one after
JPanel panel = new JPanel();
JPanel panelAfterButtonClick = new JPanel();
panel.setLayout(null);
panelAfterButtonClick.setLayout(null);
//jlabel that isnt displaying + dimensions
JLabel label = new JLabel("Opisa");
Dimension size = label.getPreferredSize();
label.setBounds(100, 100, size.width, size.height);
label.setFont(new Font("Helvetica", Font.PLAIN, 70));
//second jlabel that isn't displaying
JLabel label2 = new JLabel("Opisa");
Dimension size4 = label2.getPreferredSize();
label2.setBounds(100, 100, size4.width, size4.height);
label2.setFont(new Font("Helvetica", Font.PLAIN, 70));
//adding the labels to the panels
panel.add(label);
panelAfterButtonClick.add(label2);
//button that is displaying both before and after
JButton button = new JButton("Click Me..");
JButton buttonAfterClick = new JButton("Clicked Me..");
//dimensions
Dimension size2 = button.getPreferredSize();
button.setBounds(100, 100, size2.width, size2.height);
Dimension size3 = button.getPreferredSize();
buttonAfterClick.setBounds(100, 100, size3.width, size3.height);
//adding the buttons to the jpanel
panel.add(button);
panelAfterButtonClick.add(buttonAfterClick);
//function that changes the panel after the button is clicked
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
frame.setContentPane(panelAfterButtonClick);
frame.invalidate();
frame.validate();
}
});
//adding the panel to the frame and setting the size
frame.add(panel);
frame.setSize(1000,800);
frame.setVisible(true);
}
Check the label bounds, more specifically its size... try setting some fixed values (great enough to show its content like setBounds(100, 100, 250, 80)).
The preferred size is being retrieved before changing the font size, so it is not big enough to show that big characters. Try changing the font first.

BoxLayout only showing last component added

Looked at some previous posts pertaining to my subject but too no avail.
Trying to align components using BoxLayout but I cannot get it to work. I have tinkered with it for some time now with different results but I can't figure it out. I have used the default FlowLayout with no problems, I am trying to learn and expand my knowledge and BoxLayout will be better for my program. I want everything to stay in alignment if the User resizes their application window. I've adjusted all the sizes this way after just trying to get it to work and failing.
package GUI;
import javax.swing.*;
import java.awt.*;
/**
* Created by Thunderfoot on 7/31/2016. Keep Growing!
* Graphical User Interface
* Needs 3 JPanels(Text area + scroll pane)(2 Buttons) (1 Button), a JTextArea, JScrollPane, and 3 JButtons
*/
public class PrimaryFrame extends JFrame {
//Class variables
private static JPanel panel1, panel2, panel3;
public static JTextArea output;
//Constructor
public PrimaryFrame() {
//Frame component attributes
final Dimension FRAME_SIZE = new Dimension(400, 400);
final Dimension PANEL1_SIZE = new Dimension(400, 250);
final Dimension PANEL2_SIZE = new Dimension(400, 40);
final Dimension PANEL3_SIZE = new Dimension(400, 40);
//JFrame is PrimaryFrame
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setPreferredSize(FRAME_SIZE);
setMaximumSize(FRAME_SIZE);
setTitle("Fighting Game");
//JPanel for Text
panel1 = new JPanel();
panel1.setLayout(new BoxLayout(panel1, BoxLayout.PAGE_AXIS));
panel1.setMinimumSize(PANEL1_SIZE);
panel1.setPreferredSize(PANEL1_SIZE);
panel1.setMaximumSize(PANEL1_SIZE);
panel1.setBackground(Color.BLACK);
//JPanel for Attack and Kick Buttons
panel2 = new JPanel();
panel2.setLayout(new BoxLayout(panel2, BoxLayout.LINE_AXIS));
panel2.setMinimumSize(PANEL2_SIZE);
panel2.setPreferredSize(PANEL2_SIZE);
panel2.setMaximumSize(PANEL2_SIZE);
panel2.setBackground(Color.BLUE);
//JPanel for Power Attack Button
panel3 = new JPanel();
panel3.setLayout(new BoxLayout(panel3, BoxLayout.PAGE_AXIS));
panel3.setMinimumSize(PANEL3_SIZE);
panel3.setPreferredSize(PANEL3_SIZE);
panel3.setMaximumSize(PANEL3_SIZE);
panel3.setBackground(Color.ORANGE);
panel3.add(Box.createHorizontalGlue());
panel3.add(Box.createVerticalGlue());
//JTextArea & JScrollPane
output = new JTextArea();
output.setEditable(false);
JScrollPane outputScroller = new JScrollPane(output, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
outputScroller.setMaximumSize(new Dimension(375, 250));
outputScroller.setBorder(BorderFactory.createLineBorder(Color.RED));
panel1.add(outputScroller);
panel1.add(Box.createHorizontalGlue());
panel1.add(Box.createVerticalGlue());
//Attack Button
JButton attackButton = new JButton(" ATTACK ");
attackButton.setMaximumSize(new Dimension(75, 30));
attackButton.setBorderPainted(true);
//Kick Button
JButton kickButton = new JButton(" KICK ");
kickButton.setMaximumSize(new Dimension(75, 30));
kickButton.setBorderPainted(true);
//Add components
panel2.add(attackButton);
panel2.add(Box.createHorizontalGlue());
panel2.add(Box.createVerticalGlue());
panel2.add(kickButton);
panel2.add(Box.createHorizontalGlue());
panel2.add(Box.createVerticalGlue());
//Power Attack Button
JButton powAttButton = new JButton(" POWER ATTACK ");
powAttButton.setMaximumSize(new Dimension(150, 30));
powAttButton.setBorderPainted(true);
panel3.add(powAttButton);
panel3.add(Box.createHorizontalGlue());
}
public void buildGUI() {
//Add components and build GUI Frame
this.add(panel3);
this.add(panel2);
this.add(panel1);
//Set attributes
//Pack components together inside of frame
pack();
//Center of screen
setLocationRelativeTo(null);
//Make frame visible
setVisible(true);
}
}
You have to set the Layout of your PrimaryFrame.
I suggest you add an additional line to your buildGUI() method:
public void buildGUI() {
//defines the Layout for the main Frame
this.setLayout(new GridLayout(3,1)) //its up to you wich Layout you use
//Add components and build GUI Frame
this.add(panel3);
this.add(panel2);
this.add(panel1);
//Set attributes
//Pack components together inside of frame
pack();
//Center of screen
setLocationRelativeTo(null);
//Make frame visible
setVisible(true);
}
Notice GridLayout(3,1) will generate a layout with three rows and one column

Java swing jpanel dimension

JLabel titleLabel = new JLabel(title, SwingConstants.CENTER);
titleLabel.setForeground(Color.DARK_GRAY);
titleLabel.setFont(new Font("Comic Sans MS", Font.PLAIN, 15));
add(titleLabel, BorderLayout.PAGE_START);
JLabel descLabel = new JLabel("<html>description");
descLabel.setForeground(Color.GRAY);
descLabel.setFont(new Font("Comic Sans MS", Font.PLAIN, 13));
add(descLabel, BorderLayout.PAGE_START);
JLabel picLabel = new JLabel(new ImageIcon(Home.class.getResource("/icon/img.jpg")));
picLabel.setIconTextGap(-310);
picLabel.setOpaque(true);
picLabel.setLayout(null);
add(picLabel);
JPanel buttonPanel = new JPanel();
buttonPanel.setBorder(new LineBorder(Color.BLACK, 1, true));
JButton btnDetails = new JButton("Dettagli");
JButton btnDetails2 = new JButton("Modifica");
buttonPanel.add(btnDetails);
buttonPanel.add(btnDetails2);
add(buttonPanel);
I want to change the dimension of the panel (see the image) and align it with the image.
Any suggestion ?
Change the layout of your main panel to use BorderLayout.
Then add the picLabel to be add(picLabel, BorderLayout.CENTER) and your buttonPanel as add(buttonPanel, BorderLayout.SOUTH) and you should get better layout.
Packing the dialog / frame will also help.
You cannot add two labels to the same position in a BorderLayout. Your label descLabel will replace your titleLabel
You could do that like the buttons: Create another panel, add both labels vertically and add that to your main panel with BorderLayout.NORTH (or PAGE_START)

How to center buttons in a panel

Although I'm using BorderLayout.CENTER, my group of buttons still appears to be aligning to the north of the panel. If I use BorderLayout.SOUTH their relative position is the same as BorderLayout.CENTER but to the south of the panel.
How can I get them to be in the middle of the panel?
Is there anything I'm doing that is glaringly wrong?
public void createExecuteArea() {
JButton connectButton = new JButton("Connect");
connectButton.setPreferredSize(new Dimension(100, 40));
JButton disconnectButton = new JButton("Disconnect");
disconnectButton.setPreferredSize(new Dimension(100, 40));
JButton abortButton = new JButton("Abort");
abortButton.setPreferredSize(new Dimension(100, 40));
executePanel = new JPanel();
executePanel.setLayout(new BorderLayout());
JPanel buttonPanel = new JPanel();
buttonPanel.add(connectButton);
buttonPanel.add(disconnectButton);
buttonPanel.add(abortButton);
executePanel.add(buttonPanel, BorderLayout.CENTER);
}
The following changes to my code resolved my issues.
public void createExecuteArea() {
JButton connectButton = new JButton("Connect");
connectButton.setPreferredSize(new Dimension(100, 40));
JButton disconnectButton = new JButton("Disconnect");
disconnectButton.setPreferredSize(new Dimension(100, 40));
JButton abortButton = new JButton("Abort");
abortButton.setPreferredSize(new Dimension(100, 40));
executePanel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JPanel buttonPanel = new JPanel();
buttonPanel.add(connectButton);
buttonPanel.add(disconnectButton);
buttonPanel.add(abortButton);
executePanel.add(buttonPanel, c);
}
The issue is with executePanel and what layout it is using. You don't give it an explicit layout, and so it uses a BorderLayout by default. If you want to center your buttons within this JPanel, then consider using a different layout, perhaps a GridBagLayout.
For more specific help, consider creating and posting a minimal example program.

Java cardlayout dosen't show textarea

i want the user to click on a image and the panel with cardlayout changes for every image clicked. so i have one panel with a textarea and one with just a blue backgound, when i start the program the panel with the textarea show without textarea, when i click the image it show the blue panel, why cant i see the textarea?
i have removed the location of the image in the code
frame = new JFrame("Sandwich deLuxe");
frame.setBounds(100, 100, 741, 522);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(10, 132, 705, 89);
frame.getContentPane().add(panel);
panel.setLayout(null);
JToolBar toolBar = new JToolBar();
toolBar.setBounds(10, 0, 705, 22);
frame.getContentPane().add(toolBar);
panelCont.setBounds(10, 221, 544, 251);
frame.getContentPane().add(panelCont);
panelCont.setLayout(cl);
JPanel panelTest1 = new JPanel();
JTextArea txtrGhg = new JTextArea();
txtrGhg.setForeground(Color.BLACK);
txtrGhg.setBackground(UIManager.getColor("Button.background"));
txtrGhg.setEditable(false);
txtrGhg.setText("Velkommen til Sandwich deLuxe\r\n\r\nK\u00F8b din mad her!\r\n\r\n1. V\u00E6lg en af kategoriene fra oven.\r\n2. V\u00E6lg dinne retter/sandwichs.\r\n3. Bekr\u00E6ft k\u00F8bet i indk\u00F8bskurven.\r\n4. Din bestilling er modtaget og vi g\u00E5r straks \r\n i gang med at tilberede din mad. ");
txtrGhg.setFont(new Font("Monospaced", Font.PLAIN, 18));
panelTest1.add(txtrGhg);
panelCont.add(panelTest1, "1");
JPanel panelTest2 = new JPanel();
panelTest2.setBackground(Color.BLUE);
panelCont.add(panelTest2,"2");
cl.show(panelCont, "1");
JLabel lblNewLabel = new JLabel("New label");
lblNewLabel.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent arg0) {
cl.show(panelCont, "2");
}
});
lblNewLabel.setIcon(new ImageIcon(""));
lblNewLabel.setBounds(28, 11, 97, 67);
panel.add(lblNewLabel);
JScrollPane scrollBar = new JScrollPane(txtrGhg, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollBar.setViewportBorder(null);
scrollBar.setPreferredSize(new Dimension(300,200));
scrollBar.setBorder(BorderFactory.createEmptyBorder());
panelCont.add(scrollBar, "name_171726698118247");
panelCont.setOpaque(true);
At the end of your code you create a JScrollPane thwarting the setup you have created before. You pass txtrGhg in JScrollPane’s constructor. So the scroll pane reparents your text area as your JTextArea cannot be contained in two different containers at the same time. After that, panelTest1 does not contain your JTextArea “txtrGhg” anymore but the CardLayout will still show panelTest1 as you have requested. But it’s empty.

Categories

Resources