I'm working on a character creation panel for a role-playing game. It has a JPanel using GridBagLayout and within it a JPanel using FlowLayout.
Originally, when I didn't use FlowLayout it looked like this:
I needed to add another component on the x-axis. The weightx value would increase and become an even value. It would make all my titles off-centre and I didn't like that. I was thinking I could put some of my components inside a JPanel using FlowLayout, maybe wrap it in a JScrollPane or something nice:
Unfortunately the JPanel using FlowLayout is not being anchored by the JPanel using GridBagLayout. It went to the side of the display despite being set to anchor GridBagConstraints.BASELINE.
My Code:
public class TheLifeOfErnestRhodes extends JFrame {
private static JFrame frame = new JFrame("The Life of Ernest Rhodes");
private static Color black = new Color(40,40,40);
private static Color gold = new Color(255,223,0);
private static Color sienna = new Color(255,82,45);
private static Color stone = new Color(119,136,153);
private static Font titleFont = new Font("Serif", Font.BOLD + Font.ITALIC, 48);
private static Font textFont = new Font("Serif", Font.PLAIN, 20);
private static Font buttonFont = new Font("Serif", Font.BOLD + Font.ITALIC, 20);
private static void setFrame() {
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setResizable(false);
Container pane = frame.getContentPane();
JPanel backPanel = new JPanel();
backPanel.setBackground(black);
backPanel.setVisible(true);
pane.add(backPanel);
pane.add(charScreen());
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
setFrame();
}
});
}
private static JPanel charScreen() {
GridBagLayout layout = new GridBagLayout();
JPanel charScreen = new JPanel(layout);
charScreen.setBackground(black);
charScreen.setVisible(true);
//Static labels for the two headings/titles on this screen.
GridBagConstraints c = new GridBagConstraints();
c.gridx = 1;
c.gridy = 0;
c.anchor = GridBagConstraints.NORTH;
c.insets = new Insets(80,0,20,0);
JLabel heading = new JLabel("THE LIFE OF ERNEST RHODES");
heading.setForeground(gold);
heading.setFont(titleFont);
charScreen.add(heading, c);
c.gridy++;
c.anchor = GridBagConstraints.BASELINE;
c.insets = new Insets(0,0,10,0);
JLabel title = new JLabel("CHARACTER CREATION");
title.setForeground(stone);
title.setFont(titleFont);
charScreen.add(title, c);
c.gridy++;
c.insets = new Insets (10, 20, 10, 20);
JPanel namePanel = new JPanel(new FlowLayout());
namePanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
JLabel labelName = new JLabel("FULL NAME:");
labelName.setForeground(stone);
labelName.setFont(textFont);
labelName.setVisible(true);
namePanel.add(labelName, c);
JTextField firstName = new JTextField("Taylor", 20);
firstName.setMinimumSize(firstName.getPreferredSize());
firstName.setBackground(black.darker());
firstName.setForeground(stone);
firstName.setFont(textFont);
firstName.setHorizontalAlignment(JTextField.CENTER);
firstName.setBorder(null);
firstName.setVisible(true);
namePanel.add(firstName);
JTextField lastName = new JTextField("Woodhouse", 20);
lastName.setMinimumSize(lastName.getPreferredSize());
lastName.setBackground(black.darker());
lastName.setForeground(stone);
lastName.setFont(textFont);
lastName.setHorizontalAlignment(JTextField.CENTER);
lastName.setBorder(null);
lastName.setVisible(true);
namePanel.add(lastName);
JLabel displayName = new JLabel("TYPE A NAME & PRESS 'ENTER'");
displayName.setForeground(stone);
displayName.setFont(textFont);
displayName.setVisible(true);
namePanel.add(displayName, c);
charScreen.add(namePanel);
c.gridx = 0;
c.gridy++;
c.fill = GridBagConstraints.NONE;
JLabel availablePts = new JLabel("AVAILABLE POINTS: 5");
availablePts.setForeground(stone);
availablePts.setFont(textFont);
availablePts.setVisible(true);
charScreen.add(availablePts, c);
c.gridx++;
JLabel allocatedPts = new JLabel("ALLOCATED POINTS:");
allocatedPts.setForeground(stone);
allocatedPts.setFont(textFont);
allocatedPts.setVisible(true);
charScreen.add(allocatedPts, c);
c.gridx++;
JLabel selectTrait = new JLabel("SELECT TRAITS:");
selectTrait.setForeground(stone);
selectTrait.setFont(textFont);
selectTrait.setVisible(true);
charScreen.add(selectTrait, c);
c.gridx = 0;
c.gridy++;
JLabel labelInv = new JLabel("Investigation");
labelInv.setForeground(gold);
labelInv.setFont(textFont);
labelInv.setVisible(true);
charScreen.add(labelInv, c);
c.gridy++;
JLabel labelPers = new JLabel("Persuasion");
labelPers.setForeground(gold);
labelPers.setFont(textFont);
labelPers.setVisible(true);
charScreen.add(labelPers, c);
c.gridy++;
JLabel LabelStl = new JLabel("Stealth");
LabelStl.setForeground(gold);
LabelStl.setFont(textFont);
LabelStl.setVisible(true);
charScreen.add(LabelStl, c);
c.gridx++;
c.gridy = 4;
JLabel allocatedInt = new JLabel("0");
allocatedInt.setForeground(stone);
allocatedInt.setFont(textFont);
allocatedInt.setVisible(true);
charScreen.add(allocatedInt, c);
c.gridy++;
JLabel allocatedPers = new JLabel("0");
allocatedPers.setForeground(stone);
allocatedPers.setFont(textFont);
allocatedPers.setVisible(true);
charScreen.add(allocatedPers, c);
c.gridy++;
JLabel allocatedAth = new JLabel("0");
allocatedAth.setForeground(stone);
allocatedAth.setFont(textFont);
allocatedAth.setVisible(true);
charScreen.add(allocatedAth, c);
c.gridx++;
c.gridy = 4;
JLabel keenEye = new JLabel("Keen Eye");
keenEye.setForeground(sienna);
keenEye.setFont(textFont);
keenEye.setVisible(true);
charScreen.add(keenEye, c);
c.gridy++;
JLabel interrogator = new JLabel("Interrogator");
interrogator.setForeground(gold);
interrogator.setFont(textFont);
interrogator.setVisible(true);
charScreen.add(interrogator, c);
c.gridy++;
JLabel sleuth = new JLabel("Sleuth");
sleuth.setForeground(gold);
sleuth.setFont(textFont);
sleuth.setVisible(true);
charScreen.add(sleuth, c);
c.gridx = 1;
c.gridy++;
JLabel reset = new JLabel("Reset Stats");
reset.setForeground(gold);
reset.setFont(textFont);
reset.setVisible(true);
charScreen.add(reset, c);
c.gridy++;
JLabel gender = new JLabel("GENDER:");
gender.setForeground(stone);
gender.setFont(textFont);
gender.setVisible(true);
charScreen.add(gender, c);
c.gridx = 0;
c.gridy++;
JLabel male = new JLabel("Male");
male.setForeground(gold);
male.setFont(textFont);
male.setVisible(true);
charScreen.add(male, c);
c.gridx++;
JLabel female = new JLabel("Female");
female.setForeground(gold);
female.setFont(textFont);
female.setVisible(true);
charScreen.add(female, c);
c.gridx++;
JLabel other = new JLabel("Other");
other.setForeground(sienna);
other.setFont(textFont);
other.setVisible(true);
charScreen.add(other, c);
c.gridx = 1;
c.gridy++;
c.anchor = GridBagConstraints.PAGE_END;
JLabel clickNext = new JLabel("Continue");
clickNext.setForeground(stone);
clickNext.setFont(titleFont);
clickNext.setVisible(true);
charScreen.add(clickNext, c);
}
}
Miscellaneous:
I tried doing the SSCCE thing. I couldn't bear the thought of someone running my code without the colors and fonts!
There are coloured JLabels used as 'buttons'. The Mac's button icons are really ugly so I went rogue. The above code is modified to only show the concerned components. In my program the JLabels have MouseListeners added to them.
If you're wondering about the game, it's a murder mystery and the player is the investigator! Obviously, they're investigating the life and death of 'Ernest Rhodes'.
Thank you all in advance!
Cheers,
Aadi
namePanel.add(labelName, c); is pointless, as namePanel is using a FlowLayout, passing it a GridBagLayoutConstraint is pointless as it's meaningless to FlowLayout
charScreen.add(namePanel); is effectively passing the "default" GridBagLayoutConstraint, meaning it will be laid out at the discretion of GridBagLayout, which isn't going to help you.
Maybe you meant charScreen.add(namePanel, c);, where c is the constraints you passed to namePanel
For example...
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
public class TheLifeOfErnestRhodes extends JFrame {
private static JFrame frame = new JFrame("The Life of Ernest Rhodes");
private static Color black = new Color(40, 40, 40);
private static Color gold = new Color(255, 223, 0);
private static Color sienna = new Color(255, 82, 45);
private static Color stone = new Color(119, 136, 153);
private static Font titleFont = new Font("Serif", Font.BOLD + Font.ITALIC, 48);
private static Font textFont = new Font("Serif", Font.PLAIN, 20);
private static Font buttonFont = new Font("Serif", Font.BOLD + Font.ITALIC, 20);
private void setFrame() {
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
// frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
// frame.setResizable(false);
Container pane = frame.getContentPane();
JPanel backPanel = new JPanel();
backPanel.setBackground(black);
backPanel.setVisible(true);
pane.add(backPanel);
pane.add(charScreen());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new TheLifeOfErnestRhodes().setFrame();
}
});
}
private JPanel charScreen() {
GridBagLayout layout = new GridBagLayout();
JPanel charScreen = new JPanel(layout);
charScreen.setBackground(black);
charScreen.setVisible(true);
//Static labels for the two headings/titles on this screen.
GridBagConstraints c = new GridBagConstraints();
c.gridx = 1;
c.gridy = 0;
c.anchor = GridBagConstraints.NORTH;
c.insets = new Insets(80, 0, 20, 0);
JLabel heading = new JLabel("THE LIFE OF ERNEST RHODES");
heading.setForeground(gold);
heading.setFont(titleFont);
charScreen.add(heading, c);
c.gridy++;
c.anchor = GridBagConstraints.BASELINE;
c.insets = new Insets(0, 0, 10, 0);
JLabel title = new JLabel("CHARACTER CREATION");
title.setForeground(stone);
title.setFont(titleFont);
charScreen.add(title, c);
c.gridy++;
c.insets = new Insets(10, 20, 10, 20);
// JPanel namePanel = new JPanel(new FlowLayout());
charScreen.add(new NamePane(), c);
c.gridx = 0;
c.gridy++;
c.fill = GridBagConstraints.NONE;
JLabel availablePts = new JLabel("AVAILABLE POINTS: 5");
availablePts.setForeground(stone);
availablePts.setFont(textFont);
availablePts.setVisible(true);
charScreen.add(availablePts, c);
c.gridx++;
JLabel allocatedPts = new JLabel("ALLOCATED POINTS:");
allocatedPts.setForeground(stone);
allocatedPts.setFont(textFont);
allocatedPts.setVisible(true);
charScreen.add(allocatedPts, c);
c.gridx++;
JLabel selectTrait = new JLabel("SELECT TRAITS:");
selectTrait.setForeground(stone);
selectTrait.setFont(textFont);
selectTrait.setVisible(true);
charScreen.add(selectTrait, c);
c.gridx = 0;
c.gridy++;
JLabel labelInv = new JLabel("Investigation");
labelInv.setForeground(gold);
labelInv.setFont(textFont);
labelInv.setVisible(true);
charScreen.add(labelInv, c);
c.gridy++;
JLabel labelPers = new JLabel("Persuasion");
labelPers.setForeground(gold);
labelPers.setFont(textFont);
labelPers.setVisible(true);
charScreen.add(labelPers, c);
c.gridy++;
JLabel LabelStl = new JLabel("Stealth");
LabelStl.setForeground(gold);
LabelStl.setFont(textFont);
LabelStl.setVisible(true);
charScreen.add(LabelStl, c);
c.gridx++;
c.gridy = 4;
JLabel allocatedInt = new JLabel("0");
allocatedInt.setForeground(stone);
allocatedInt.setFont(textFont);
allocatedInt.setVisible(true);
charScreen.add(allocatedInt, c);
c.gridy++;
JLabel allocatedPers = new JLabel("0");
allocatedPers.setForeground(stone);
allocatedPers.setFont(textFont);
allocatedPers.setVisible(true);
charScreen.add(allocatedPers, c);
c.gridy++;
JLabel allocatedAth = new JLabel("0");
allocatedAth.setForeground(stone);
allocatedAth.setFont(textFont);
allocatedAth.setVisible(true);
charScreen.add(allocatedAth, c);
c.gridx++;
c.gridy = 4;
JLabel keenEye = new JLabel("Keen Eye");
keenEye.setForeground(sienna);
keenEye.setFont(textFont);
keenEye.setVisible(true);
charScreen.add(keenEye, c);
c.gridy++;
JLabel interrogator = new JLabel("Interrogator");
interrogator.setForeground(gold);
interrogator.setFont(textFont);
interrogator.setVisible(true);
charScreen.add(interrogator, c);
c.gridy++;
JLabel sleuth = new JLabel("Sleuth");
sleuth.setForeground(gold);
sleuth.setFont(textFont);
sleuth.setVisible(true);
charScreen.add(sleuth, c);
c.gridx = 1;
c.gridy++;
JLabel reset = new JLabel("Reset Stats");
reset.setForeground(gold);
reset.setFont(textFont);
reset.setVisible(true);
charScreen.add(reset, c);
c.gridy++;
JLabel gender = new JLabel("GENDER:");
gender.setForeground(stone);
gender.setFont(textFont);
gender.setVisible(true);
charScreen.add(gender, c);
c.gridx = 0;
c.gridy++;
JLabel male = new JLabel("Male");
male.setForeground(gold);
male.setFont(textFont);
male.setVisible(true);
charScreen.add(male, c);
c.gridx++;
JLabel female = new JLabel("Female");
female.setForeground(gold);
female.setFont(textFont);
female.setVisible(true);
charScreen.add(female, c);
c.gridx++;
JLabel other = new JLabel("Other");
other.setForeground(sienna);
other.setFont(textFont);
other.setVisible(true);
charScreen.add(other, c);
c.gridx = 1;
c.gridy++;
c.anchor = GridBagConstraints.PAGE_END;
JLabel clickNext = new JLabel("Continue");
clickNext.setForeground(stone);
clickNext.setFont(titleFont);
clickNext.setVisible(true);
charScreen.add(clickNext, c);
return charScreen;
}
public class NamePane extends JPanel {
public NamePane() {
JLabel labelName = new JLabel("FULL NAME:");
labelName.setForeground(stone);
labelName.setFont(textFont);
labelName.setVisible(true);
add(labelName);
JTextField firstName = new JTextField("Taylor", 20);
// firstName.setMinimumSize(firstName.getPreferredSize());
firstName.setBackground(black.darker());
firstName.setForeground(stone);
firstName.setFont(textFont);
firstName.setHorizontalAlignment(JTextField.CENTER);
firstName.setBorder(null);
firstName.setVisible(true);
add(firstName);
JTextField lastName = new JTextField("Woodhouse", 20);
// lastName.setMinimumSize(lastName.getPreferredSize());
lastName.setBackground(black.darker());
lastName.setForeground(stone);
lastName.setFont(textFont);
lastName.setHorizontalAlignment(JTextField.CENTER);
lastName.setBorder(null);
lastName.setVisible(true);
add(lastName);
JLabel displayName = new JLabel("TYPE A NAME & PRESS 'ENTER'");
displayName.setForeground(stone);
displayName.setFont(textFont);
displayName.setVisible(true);
add(displayName);
}
}
}
Related
I'm trying to make a JList. For some reason the JLabel of some random elements have a top padding like this:
As you can see in the picture, the first, fourth, and seventh row all have a top padding in their first JLabel. One way I managed to eliminate this effect is by making the text in the JLabel only one line, but that doesn't solve the problem because I need it to contain multiple lines.
Code:
package maya.page;
import maya.Main;
import maya.object.Module;
import maya.object.Occurrence;
import javax.swing.*;
import javax.swing.border.BevelBorder;
import javax.swing.border.EmptyBorder;
import java.awt.*;
public class StudentModulePanel extends JPanel {
public StudentModulePanel(){
DefaultListModel<Occurrence> listModel = new DefaultListModel<>();
listModel.addAll(Main.modules.get("WIX1002").getOccurrences());
JList<Occurrence> list = new JList<>(listModel);
list.setCellRenderer(new OccurrenceRenderer());
JScrollPane scrollPane = new JScrollPane(list);
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
add(scrollPane, c);
setBackground(new Color(180, 230, 230));
}
}
class OccurrenceRenderer extends OccurrencePanel implements ListCellRenderer<Occurrence>{
#Override
public Component getListCellRendererComponent(JList<? extends Occurrence> list, Occurrence value, int index, boolean isSelected, boolean cellHasFocus) {
setOccurrence(value);
if(isSelected){
setBorder(new BevelBorder(BevelBorder.LOWERED));
} else {
setBorder(new BevelBorder(BevelBorder.RAISED));
}
return this;
}
}
class OccurrencePanel extends JPanel{
Occurrence occurrence;
JLabel moduleTitleLabel = new JLabel();
JLabel occurrenceNumberLabel = new JLabel();
JLabel activityLabel = new JLabel();
JLabel timeLabel = new JLabel();
JLabel tutorLabel = new JLabel();
JLabel targetLabel = new JLabel();
JLabel actualLabel = new JLabel();
void setOccurrence(Occurrence occurrence){
this.occurrence = occurrence;
Module module = Main.modules.get(occurrence.getCode());
moduleTitleLabel.setText(String.format("<html>%s - %s %s</html>", module.getCode(), module.getCode(), module.getTitle()));
occurrenceNumberLabel.setText(Integer.toString(occurrence.getOccurrenceNumber()));
activityLabel.setText(occurrence.getActivityString());
timeLabel.setText(occurrence.getTime());
tutorLabel.setText(String.format("<html>%s</html>", "TUTOR")); //occurrence.getTutor()));
targetLabel.setText(Integer.toString(occurrence.getTargetStudents()));
actualLabel.setText(Integer.toString(occurrence.getActualStudents()));
}
OccurrencePanel(){
int height = 75;
Dimension bigLabelSize = new Dimension(200, height);
Dimension smallLabelSize = new Dimension(40, height);
Dimension activityLabelSize = new Dimension(75, height);
int padding = 10;
EmptyBorder paddingBorder = new EmptyBorder(0, padding, 0, padding);
moduleTitleLabel.setPreferredSize(bigLabelSize);
moduleTitleLabel.setBorder(paddingBorder);
occurrenceNumberLabel.setPreferredSize(smallLabelSize);
occurrenceNumberLabel.setBorder(paddingBorder);
activityLabel.setPreferredSize(activityLabelSize);
activityLabel.setBorder(paddingBorder);
timeLabel.setPreferredSize(bigLabelSize);
timeLabel.setBorder(paddingBorder);
tutorLabel.setPreferredSize(bigLabelSize);
tutorLabel.setBorder(paddingBorder);
targetLabel.setPreferredSize(smallLabelSize);
targetLabel.setBorder(paddingBorder);
targetLabel.setPreferredSize(smallLabelSize);
actualLabel.setBorder(paddingBorder);
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
add(moduleTitleLabel, c);
c.gridx = 1;
add(occurrenceNumberLabel, c);
c.gridx = 2;
add(activityLabel, c);
c.gridx = 3;
add(timeLabel, c);
c.gridx = 4;
add(tutorLabel, c);
c.gridx = 5;
add(targetLabel, c);
c.gridx = 6;
add(actualLabel, c);
}
}
I'm learning java swing and this is very confused to me. The quit button doesn't display. However, if I move the code part of textArea after the two parts of buttons, it will display correctly. So why?
package exercise1;
import javax.swing.*;
import java.awt.*;
public class ChatClient {
private JTextArea textArea;
private JTextField textField;
private JButton btnSend;
private JButton btnQuit;
private JFrame frame;
private JPanel panel;
private JScrollPane scrollPane;
private void launchFrame() {
panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
textArea = new JTextArea(10, 50);
scrollPane = new JScrollPane(textArea);
c.gridx = 0;
c.gridy = 0;
c.gridheight = 3;
panel.add(scrollPane, c);
btnSend = new JButton("Send");
c.gridx = 1;
c.gridy = 0;
c.anchor = GridBagConstraints.NORTH;
panel.add(btnSend, c);
btnQuit = new JButton("Quit");
c.gridx = 1;
c.gridy = 1;
c.anchor = GridBagConstraints.NORTH;
panel.add(btnQuit, c);
}
protected ChatClient() {
frame = new JFrame("Chat Room");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
launchFrame();
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
ChatClient client = new ChatClient();
}
}
Simple: You forgot to reset c.gridheight = 1; after adding the JScrollPane. Without doing this, the send button will overlie the quit button.
private void launchFrame() {
panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL; // ** This is also worthwhile **
textArea = new JTextArea(10, 50);
scrollPane = new JScrollPane(textArea);
c.gridx = 0;
c.gridy = 0;
c.gridheight = 3;
panel.add(scrollPane, c);
btnSend = new JButton("Send");
c.gridx = 1;
c.gridy = 0;
c.gridheight = 1; // ********* ADD THIS *********
c.anchor = GridBagConstraints.NORTH;
panel.add(btnSend, c);
btnQuit = new JButton("Quit");
c.gridx = 1;
c.gridy = 1;
c.anchor = GridBagConstraints.NORTH;
panel.add(btnQuit, c);
}
I want to make my components on my JFrame resize with the resizing of the window.
How can this be done.
Currently when I make the JFrame smaller the components remain in the same place and then disappear as the frame gets smaller.
Ideally I would like to set a minimum JFrame size which would not allow the form to go any smaller but when I make it larger that components should move with the JFrame.
Add: What if I want to use an absolute layout manager. This is the layout manager that my boss prefers.
Code:
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import java.awt.Font;
public class TestFrame extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JButton btnNewButton;
private JButton btnNewButton_1;
private JButton btnNewButton_2;
private JCheckBox chckbxNewCheckBox;
private JCheckBox chckbxNewCheckBox_1;
private JCheckBox chckbxNewCheckBox_2;
public static void main(String[] args) {
TestFrame frame = new TestFrame();
frame.setVisible(true);
}
public TestFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 629, 458);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(15, 15, 15, 15));
setContentPane(contentPane);
contentPane.setLayout(null);
btnNewButton = new JButton("Save");
btnNewButton.setFont(new Font("Sylfaen", Font.BOLD, 14));
btnNewButton.setBounds(514, 386, 89, 23);
contentPane.add(btnNewButton);
btnNewButton_1 = new JButton("Open");
btnNewButton_1.setBounds(514, 352, 89, 23);
contentPane.add(btnNewButton_1);
btnNewButton_2 = new JButton("Close");
btnNewButton_2.setBounds(514, 318, 89, 23);
contentPane.add(btnNewButton_2);
btnNewButton_2.setBackground(Color.YELLOW);
btnNewButton_2.setEnabled(false);
chckbxNewCheckBox = new JCheckBox("Employeed");
chckbxNewCheckBox.setBounds(506, 7, 97, 23);
contentPane.add(chckbxNewCheckBox);
chckbxNewCheckBox_1 = new JCheckBox("Not Employeed");
chckbxNewCheckBox_1.setBounds(506, 33, 97, 23);
contentPane.add(chckbxNewCheckBox_1);
chckbxNewCheckBox_2 = new JCheckBox("Self Employeed");
chckbxNewCheckBox_2.setBounds(506, 59, 97, 23);
contentPane.add(chckbxNewCheckBox_2);
}
}
An Example of GridBagLayout:
This is an an example of the GridBagLayout. The components resize correctly but I do not see a grow property. It must do that automatically.
import java.awt.*;
import javax.swing.JButton;
import javax.swing.JFrame;
public class GridBagLayoutDemo {
final static boolean shouldFill = true;
final static boolean shouldWeightX = true;
final static boolean RIGHT_TO_LEFT = false;
public static void addComponentsToPane(Container pane) {
JButton button;
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
button = new JButton("Button 1");
if (shouldWeightX) {
c.weightx = 0.5;
}
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
pane.add(button, c);
button = new JButton("Button 2");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 0;
pane.add(button, c);
button = new JButton("Button 3");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 2;
c.gridy = 0;
pane.add(button, c);
button = new JButton("Long-Named Button 4");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 40; //make this component tall
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 1;
pane.add(button, c);
button = new JButton("5");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 0; //reset to default
c.weighty = 1.0; //request any extra vertical space
c.anchor = GridBagConstraints.PAGE_END; //bottom of space
c.insets = new Insets(10,0,0,0); //top padding
c.gridx = 1; //aligned with button 2
c.gridwidth = 2; //2 columns wide
c.gridy = 2; //third row
pane.add(button, c);
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("GridBagLayout");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentsToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
createAndShowGUI();
}
}
Use a layout manager, like GridBagLayout, its have a property call "grow" for a column.
I am trying to make an application in java, to start with i have problems in the GUI.
I have put Jpanel inside Jframe ,but i am gettingf problems when i use setMaximumSize and moreover i want to fix the size of the Jpanel, so that even if user tries to change the size of the window , jpanel remains at center.
i have tried this a solution at StackOverflow
How can I properly center a JPanel ( FIXED SIZE ) inside a JFrame?
Please guide me through.This is my first post, i dont have enough reputations to post an image .Thanks
import javax.swing.*;
import java.awt.*;
class App_Demo4 extends JFrame {
public App_Demo4()
{
JFrame frm=new JFrame("Application");
JPanel pane=new JPanel();
frm.setVisible(true);
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.setLayout(new BorderLayout());
frm.add(pane,BorderLayout.CENTER);
pane.setSize(400,400);
Dimension dim=new Dimension(400,400);
pane.setMinimumSize(dim);
Box box = new Box(BoxLayout.Y_AXIS);
box.add(Box.createVerticalGlue());
box.add(pane);
box.add(Box.createVerticalGlue());
frm.add(box);
frm.setSize(500,500);
JButton dbtn1=new JButton("Download File");
pane.add(dbtn1);
JTextField txt1=new JTextField(20);
pane.add(txt1);
JButton bbtn1=new JButton("Browse");
pane.add(bbtn1);
JButton dbtn2=new JButton("Download Mail");
pane.add(dbtn2);
JTextField txt2=new JTextField(20);
pane.add(txt2);
JButton bbtn2=new JButton("Browse");
pane.add(bbtn2);
JButton cbtn1=new JButton("Compile File");
pane.add(cbtn1);
JTextField txt3=new JTextField(20);
pane.add(txt3);
JButton bbtn3=new JButton("Browse");
pane.add(bbtn3);
JButton cbtn2=new JButton("Cancel");
pane.add(cbtn2,BorderLayout.EAST);
}
public static void main(String[] args)
{
new App_Demo4();
}
}
Don't try to set the size of a panel. That is the job of the layout manager.
You need to change the layout manager of your panel. By default a JPanel uses a FlowLayout. In this case the components just flow to a new line depending on the width of the frame.
You might want to look at a GridBagLayout. See the section from the Swing tutorial on How to Use a GridBagLayout for more information and examples.
Also, make sure you add all the components to the frame BEFORE packing the frame and making the frame visible. So you basic code should be:
frame.add(....)l
frame.pack();
frame.setVisible(true);
Thats how you do it :
Hope it helps...
import javax.swing.*;
import java.awt.*;
public class App_Demo4 extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
public App_Demo4()
{
super("Application");
JPanel pane=new JPanel();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setPreferredSize(new Dimension(500,500));
GridBagLayout gl = new GridBagLayout();
pane.setLayout(gl);
GridBagConstraints c = new GridBagConstraints();
c.weightx = 1;
c.weighty = 1;
c.anchor = GridBagConstraints.CENTER;
c.fill = GridBagConstraints.NONE;
c.insets = new Insets(6, 6, 6, 6);
c.gridx = 0;
c.gridy = 0;
JButton dbtn1=new JButton("Download File");
pane.add(dbtn1, c);
JTextField txt1=new JTextField(20);
c.gridx = 1;
pane.add(txt1, c);
JButton bbtn1=new JButton("Browse");
c.gridx = 2;
pane.add(bbtn1, c);
JButton dbtn2=new JButton("Download Mail");
c.gridy = 1;
c.gridx = 0;
pane.add(dbtn2, c);
JTextField txt2=new JTextField(20);
c.gridx = 1;
pane.add(txt2, c);
JButton bbtn2=new JButton("Browse");
c.gridx = 2;
pane.add(bbtn2, c);
c.gridy = 2;
c.gridx = 0;
JButton cbtn1=new JButton("Compile File");
pane.add(cbtn1, c);
JTextField txt3=new JTextField(20);
c.gridx = 1;
pane.add(txt3, c);
JButton bbtn3=new JButton("Browse");
c.gridx = 2;
pane.add(bbtn3, c);
c.gridy = 3;
c.gridx = 2;
JButton cbtn2=new JButton("Cancel");
pane.add(cbtn2,c);
this.add(pane);
this.pack();
this.setVisible(true);
}
public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new App_Demo4();
}
});
}
}
I have the following simple code and I don't know how to modify it so as to have 3 separate panels to switch to, one for each button:
package TouristLocations;
import javax.swing.*;
import java.awt.*;
public class buildApp extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
public static void main(String[] args){
JFrame frame = new JFrame("Test");
frame.setSize(400,500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JLabel title = new JLabel("Locations");
title.setFont(new Font("Serif", Font.BOLD, 40));
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 3;
frame.add(title, c);
JButton b1 = new JButton("View Locations");
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 1;
frame.add(b1, c);
JButton b2 = new JButton("Insert Locations");
c.gridx = 1;
c.gridy = 1;
frame.add(b2, c);
JButton b3 = new JButton("Help");
c.gridx = 2;
c.gridy = 1;
frame.add(b3, c);
TextArea text1 = new TextArea(15,40);
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 3;
frame.add(text1, c);
frame.pack();
}
}
thank you
Sounds like you should consider using JTabbedPane.
In addition to How to Use Tabbed Panes, you may want to look at CardLayout, mentioned here and here.
You should create a main container:
JPanel mainContainer = new JPanel();
//creation of each child
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
... so each button must add those panels into the main container and resize new panel size, something like this:
//for button1:
mainContainer.add(panel1);
panel1.setSize(mainContainer.getSize());
... for button2 action, you must follow the same way above.