JPanel, problems with resizing - java

I'm a newbie.
Trying to make auto resize border. I made border on my frame with 2 panels. I added panels with border into first panel.
I want border which retreated from all edges. In this border panel I also added text panel and button.
When I expand the window, or resize it panel with border is resizing too. But there is not indents from edges when I am using BorderLayout.
public class App {
private JFrame frame;
private JPanel panel;
private JPanel panel_1;
private JTextField textField;
private JButton addBtn;
public static void main(String args[]) {
App app = new App();
app.initialize();
app.frame.pack();
app.frame.setVisible(true);
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
frame.getContentPane().add(panel, BorderLayout.NORTH);
panel.setLayout(new BorderLayout(0, 0));
panel_1 = new JPanel();
panel_1.setPreferredSize(new Dimension(784, 40));
panel_1.setBorder(new LineBorder(new Color(0, 0, 0)));
panel.add(panel_1, BorderLayout.CENTER);
textField = new JTextField();
textField.setPreferredSize(new Dimension(6, 24));
panel_1.add(textField);
textField.setColumns(50);
addBtn = new JButton("Add");
addBtn.setPreferredSize(new Dimension(70, 24));
panel_1.add(addBtn);
}
}
This is with BorderLayout - http://snag.gy/S43C2.jpg.
Also I tried with FlowLayout in panel - http://snag.gy/ndjDG.jpg
Can you help me please?

The problem is that because you set the border on a panel that you add into BorderLayout.NORTH. When you resize the window, BorderLayout.NORTH section will only resize horizontally, that's why the border will not be resized correctly.
public static void main(String args[]) {
JavaApplication11 app = new JavaApplication11();
app.initialize();
app.frame.pack();
app.frame.setVisible(true);
}
private JFrame frame;
private JPanel panel;
private JTextField textField;
private JButton addBtn;
private void initialize() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
frame.add(panel);
Border border = new CompoundBorder(new EmptyBorder(5, 10, 15, 20), new LineBorder(Color.BLACK));
panel.setBorder(border);
textField = new JTextField(50);
panel.add(textField);
addBtn = new JButton("Add");
panel.add(addBtn);
}

I'm assuming that this is the GUI you're trying to create.
To create this GUI, you need to use multiple JPanels with more than one Swing layout manager.
Here's the hierarchy of Swing components I would use.
JFrame - border layout
JPanel - main panel, border layout
JPanel - text, button panel, border layout, border north
JTextField - border center
JButton - border east
JScrollPane - border center
JTable
JPanel - button panel, flow layout, border south
JButton (3)
You get the spacing by setting an empty border on the JPanels and JScrollPane. The empty border can be as wide as you wish.

Related

My JTextArea's are not respecting the size of the component and are being cut off

I have been working on this for hours. I honestly cannot figure it out. I have JTextArea's inside a JSplitPane which is inside a JPanel with a JButton and all that is put in my JFrame. I am using Layout managers. I have tried using pack(). I have tried using preferred sizes. Without the JPanel my button does not display in the proper location or switch buttons in other Tabs. With the JPanel it cuts off all my text, stops the scroll function(yes I have tried setting the TextAreas to always have horizontal and vertical scroll bars...does not solve the problem where text just stops wrapping for no apparent reason).
public static void main(String[] args) throws IOException
{
JFrame frame = new JFrame();
Deck blackjack = new Deck(Deck.TYPE[0]);
JTextArea textBlackjackUnshuffled = new JTextArea();
JTextArea textBlackjackShuffle = new JTextArea();
JButton shuffleButtonBlackjack = new JButton(new ImageIcon(ImageIO.read(new File("res/shuffle.png"))));
JToolBar toolBarBlackjack = new JToolBar("Blackjack");
JSplitPane splitPaneBlackjack = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
JPanel panel = new JPanel();
JTabbedPane tabbedPaneBlackJack = new JTabbedPane();
JTabbedPane tabbedPaneCanasta = new JTabbedPane();
JTabbedPane tabbedPanePinochle = new JTabbedPane();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
textBlackjackUnshuffled.setColumns(10);
textBlackjackUnshuffled.setLineWrap(true);
textBlackjackUnshuffled.setWrapStyleWord(true);
textBlackjackUnshuffled.setEditable(false);
textBlackjackUnshuffled.setFont(new Font("DejaVu Sans", Font.PLAIN, 100));
textBlackjackUnshuffled.append(blackjack.toString());
textBlackjackShuffle.setColumns(10);
textBlackjackShuffle.setLineWrap(true);
textBlackjackShuffle.setWrapStyleWord(true);
textBlackjackShuffle.setEditable(false);
textBlackjackShuffle.setFont(new Font("DejaVu Sans", Font.PLAIN, 100));
textBlackjackShuffle.append(blackjack.toString());
shuffleButtonBlackjack.setBorderPainted(false);
shuffleButtonBlackjack.setFocusPainted(false);
shuffleButtonBlackjack.setContentAreaFilled(false);
splitPaneBlackjack.add(new JScrollPane(textBlackjackUnshuffled));
splitPaneBlackjack.add(new JScrollPane(textBlackjackShuffle));
panel.add(splitPaneBlackjack, BorderLayout.CENTER);
panel.add(shuffleButtonBlackjack, BorderLayout.PAGE_END);
tabbedPaneBlackJack.addTab("Blackjack", panel);
frame.add(tabbedPaneBlackJack);
frame.setSize(new Dimension(Toolkit.getDefaultToolkit().getScreenSize()));
frame.setVisible(true);
}
You're adding the JScrollPanes to the panel in BorderLayout positions, but have not set the layout manager of panel to BorderLayout. In this situation, panel will be using JPanel's default layout manager, FlowLayout, a manager which is not smart enough to respect the scroll pane's preferred sizes.
Your code needs:
panel.setLayout(new BorderLayout());

How do I left align a JLabel inside a JPanel?

I am trying to build a status bar for my login dialog box but the label doesn't align to the left of the status panel. Here is my code.
public class LoginDialog extends JDialog {
private static final long serialVersionUID = 1L;
protected JLabel lblTopSpace = null;
protected JPanel loginPanel = null;
protected JPanel statusPanel = null;
public LoginDialog(String title) {
super((Dialog)null);
this.setTitle(title);
Initialize();
}
protected void Initialize() {
lblTopSpace = new JLabel("Login into Bookyard");
lblTopSpace.setForeground(this.getBackground());
loginPanel = new LoginPanel();
statusPanel = new JPanel();
statusPanel.setBorder(new BevelBorder(BevelBorder.LOWERED));
statusPanel.setSize(this.getWidth(), 50);
JLabel lblStatus = new JLabel("Status");
lblStatus.setFont(new Font("Verdana", Font.PLAIN, 12));
lblStatus.setHorizontalAlignment(SwingConstants.LEFT);
statusPanel.add(lblStatus);
this.setLayout(new BorderLayout());
Container container = this.getContentPane();
container.add(lblTopSpace, BorderLayout.NORTH);
container.add(loginPanel, BorderLayout.CENTER);
container.add(statusPanel, BorderLayout.SOUTH);
this.pack();
}
}
Here is what it looks like presently.
What am I missing?
Your label is inside a panel that is inside the contentpane of the dialog. So the label is managed with the layout of its parent panel. But you don't set any particular layout for it, then it's a FlowLayout, and your label is then centered in it with a size the minimal one required to let text appear. Then the label is left aligned in its own area, but this one is centered in the panel.
Either change the layout of the panel to let the label extends in it (add a BorderLayout and set the label in north, center or south of it), or remove the panel that seems not useful (and let the label extends in the south of the contentpane.
statusPanel.setLayout(new BorderLayout());
statusPanel.add(lblStatus,BorderLayout.SOUTH);
or
container.add(lblStatus,BorderLayout.SOUTH);

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

How do I get these two buttons on the bottom of my program

Write a program that displays two buttons labeled “Green” and “Orange”.
If the user clicks on the green button, the background of the window changes to green. If the user clicks on the orange button, the background of the window changes to Orange.
Create a JFrame for this GUI. The GUI employs the default layout manager. A JPanel is needed.
Place the two buttons inside the panel and add the panel to the south region of the border layout.
Notice the text in the title bar. The green button should have white text and a green background. The orange button should have black text with an orange background.
Below is what I have so far, it doesn't seem to work.
public class LabAssign91 extends JFrame implements ActionListener{
private JPanel loc1Panel;
private JButton greenButton, orangeButton;
public LabAssign91()
{
super("Colored Buttons");
setLayout(new GridLayout(2, 2));
setSize(300,250);
setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(loc1Panel);
loc1Panel = new JPanel();
add(loc1Panel, BorderLayout.SOUTH);
greenButton = new JButton("Green");
greenButton.addActionListener(this);
loc1Panel.add(greenButton, BorderLayout.WEST);
greenButton.setBackground(Color.green);;
orangeButton = new JButton("Orange");
orangeButton.addActionListener(this);
loc1Panel.add(orangeButton, BorderLayout.EAST);
orangeButton.setBackground(Color.orange);
}
public static void main(String[] args) {
LabAssign91 app = new LabAssign91();
}
public void actionPerformed(ActionEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
I have used BorderLayout for the JFrame and FlowLayout for the ButtonPanel. ButtonPanel is the bottom panel of the frame.
frame = new JFrame();
frame.setLayout(new BorderLayout());
topPanel = new JPanel();
topPanel.add(new JLabel("Top Panel"));
middlepanel = new JPanel();
middlepanel.add(new JLabel("Middle Panel"));
bottomPanel = new JPanel();
bottomPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
bottomPanel.add(new JButton("Orange"));
bottomPanel.add(new JButton("Green"));
frame.add(topPanel, BorderLayout.NORTH);
frame.add(middlepanel, BorderLayout.CENTER);
frame.add(bottomPanel, BorderLayout.SOUTH);
The default layout for a JFrame is BorderLayout which has a SOUTH constraint. So there is no need for this statement.
//setLayout(new GridLayout(2, 2));
The default layout for a JPanel is a FlowLayout. So the following statements do nothing:
loc1Panel.add(greenButton, BorderLayout.WEST);
loc1Panel.add(orangeButton, BorderLayout.EAST);
Read the section from the Swing tutorial on Using Layout Managers. There is a section on using a BorderLayout and on using a FlowLayout. I don't know if you are supposed to use just panels with a BorderLayout or panels with a combination of BorderLayout and FlowLayout. I'll let you fix the code to meet your requirement.

Jpanel size after adding jlabel component

I have a central panel. This is my parent panel. I am adding 3 panels to the parent panel.
The panels are going to be stacked vertically. Like a title panel, then a middle panel, then a bottom panel. I just want to focus on my title panel. When I create a jlabel using text. The label shows and the panel borders stretches the entire width of the parent panel, which is what I want.
private JPanel titlePanel() {
String text = "<html><b><big><font color=#5C8C5C>Help Dialog</font></big></b></html>";
JLabel textLabel = new JLabel(text, JLabel.CENTER);
JPanel p = new JPanel();
p.setLayout(new BoxLayout(p, BoxLayout.LINE_AXIS));
p.add(textLabel);
p.setBorder(BorderFactory.createLineBorder(Color.black));
return p;
}
I am actually wanting to use a icon as the label and not html text. So make the changes to the code.
private JPanel titlePanel() {
Registry appReg = Registry.getRegistry(this);
ImageIcon ediLabelIcon = appReg.getImageIcon("ToolLabel.ICON");
JLabel textLabel = new JLabel(ediLabelIcon, JLabel.CENTER);
JPanel p = new JPanel();
p.setLayout(new BoxLayout(p, BoxLayout.LINE_AXIS));
p.add(textLabel);
p.setBorder(BorderFactory.createLineBorder(Color.black));
return p;
}
Now the label shows, but the border of the panel is only as wide as the label and not stretched out the width of the parent panel.
I am trying to figure out to extend the panel border the width of the parent panel and not just as wide as the label. This is the code for the parent panel.
private void createDialog() {
Component titlePanel = titlePanel();
Component verbiagePanel = verbiagePanel();
Component closeButtonPanel = closeButton();
setTitle("HELP Dialog");
centerPanel = new JPanel();
centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.PAGE_AXIS));
centerPanel.setPreferredSize(new Dimension(600, 300));
centerPanel.add(titlePanel);
centerPanel.add(Box.createRigidArea(new Dimension(0, 10)));
centerPanel.add(verbiagePanel);
centerPanel.add(Box.createHorizontalGlue());
centerPanel.add(Box.createRigidArea(new Dimension(0, 10)));
centerPanel.add(closeButtonPanel);
getContentPane().add(centerPanel);
this.pack();
}
Using HTML in JLabel text switched the mechanism which calculate preferred size fot JLabel.
Now I can't explain it in detail, but if you change creating title label to
JLabel textLabel = new JLabel("<html></html>", ediLabelIcon, JLabel.CENTER);
your label will be stretched out to parent panel width.
Or you may choose another layout manager such as GridBagLayout. With GridBagLayout you can force stretch any component to its parent width.

Categories

Resources