java boxlayout space after adding jpanel - java

I have container jpanel in which i used a boxlayout manager and what i do is i add another panels inside in which the added panel contains a label and textfield using flowlayout manager. everytime i add a panel inside it creates an annoying big space after another added panel. I want to reduce the spacing of the panels i have tried using setsize and setpreferredsize method to adjust it. Here is my code:
JPanel global = new JPanel();
global.setLayout(new BoxLayout(global, BoxLayout.Y_AXIS));
global.setPreferredSize(new Dimension(500,312));
global.setSize(500,312);
global.setBounds(8, 5, 500, 312);
global.setBorder(BorderFactory.createLineBorder(Color.black));
global.setBackground(Color.white);
//Elements of global
JLabel label1 = new JLabel("Global Settings");
label1.setAlignmentX(Component.CENTER_ALIGNMENT);
label1.setFont(new Font("tahoma", Font.BOLD, 17));
global.add(label1);
global.add(new JSeparator());
//Name Field
JPanel c = new JPanel();
c.setSize(100, 1);
c.setPreferredSize(new Dimension(100,1));
c.setLayout(new FlowLayout());
JLabel label = new JLabel("Display Name");
JTextField text = new JTextField(20);
text.setPreferredSize(new Dimension(20,25));
c.add(label);
c.add(text);
global.add(c);
//Hostname Field
JPanel c1 = new JPanel();
c1.setSize(100, 1);
c1.setPreferredSize(new Dimension(100,1));
c1.setLayout(new FlowLayout());
JLabel label2 = new JLabel("Host Name");
JTextField text1 = new JTextField(20);
text1.setPreferredSize(new Dimension(20,25));
c1.add(label2);
c1.add(text1);
global.add(c1);

BoxLayout is a pretty aggressive LayoutManager and doesn't always honour the preferred size of components within it. Instead, we must set the maximum size of BoxLayout components to prevent them from being stretched. Additionally, we need to add a Box via Box.createVerticalGlue() - this is special component that gets stretched (rather than the other components).
Here is the rewritten code:
JPanel global = new JPanel();
global.setLayout(new BoxLayout(global, BoxLayout.Y_AXIS));
global.setPreferredSize(new Dimension(500, 312));
global.setSize(500, 312);
global.setBounds(8, 5, 500, 312);
global.setBorder(BorderFactory.createLineBorder(Color.black));
global.setBackground(Color.white);
// Elements of global
JLabel label1 = new JLabel("Global Settings");
label1.setAlignmentX(Component.CENTER_ALIGNMENT);
label1.setFont(new Font("tahoma", Font.BOLD, 17));
global.add(label1);
JSeparator sep = new JSeparator();
sep.setMaximumSize(new Dimension((int) sep.getMaximumSize().getWidth(), 50));
global.add(sep);
// Name Field
JPanel c = new JPanel();
c.setMaximumSize(new Dimension((int) c.getMaximumSize().getWidth(), 50));
JLabel label = new JLabel("Display Name");
JTextField text = new JTextField(20);
text.setPreferredSize(new Dimension(20, 25));
c.add(label);
c.add(text);
global.add(c);
// Hostname Field
JPanel c1 = new JPanel();
c1.setMaximumSize(new Dimension((int) c1.getMaximumSize().getWidth(), 50));
JLabel label2 = new JLabel("Host Name");
JTextField text1 = new JTextField(20);
text1.setPreferredSize(new Dimension(20, 25));
c1.add(label2);
c1.add(text1);
global.add(c1);
global.add(Box.createVerticalGlue());

Related

Padding JTextFields on a JPanel [duplicate]

This question already has answers here:
Providing white space in a Swing GUI
(6 answers)
Closed 7 years ago.
With the code below I am making a JPanel with JTextFields and JLabels and adding that panel to another JPanel. How can I adjust the spacing between the JTextFields on infoPanel?
I have tried GridBagLayout and GridLayout with different and undesired results. The way it is right now at least gets them aligned vertically, but I can't seem to add space above and below them. Any gurus on this subject able to help out?
public DrawPanelMain() {
JPanel btnPanel = new JPanel(); //Creates a new Panel for the buttons
JPanel infoPanel = new JPanel();
JPanel fields = new JPanel();
//Text boxes for infoPanel
JTextField textField1 = new JTextField(20);
JTextField textField2 = new JTextField(20);
JTextField textField3 = new JTextField(20);
JTextField textField4 = new JTextField(20);
JTextField textField5 = new JTextField(20);
JTextField textField6 = new JTextField(20);
//JLabels for infoPanel
JLabel jLabel1 = new JLabel("Serial Number: ");
JLabel jLabel2 = new JLabel("Information: ");
JLabel jLabel3 = new JLabel("Information: ");
JLabel jLabel4 = new JLabel("Information: ");
JLabel jLabel5 = new JLabel("Information: ");
JLabel jLabel6 = new JLabel("Information: ");
//These are the buttons that will be added to the btnPanel
btnPanel.add(new JButton(new AddSwitchAction("Add Switch Panel")));
btnPanel.add(new JButton(new PushConfigAction("Push Config")));
btnPanel.add(new JButton(new ActivateAllAction("Activate All")));
btnPanel.add(new JButton(new DeactivateAllAction("Deactivate All")));
//Fields that will be added to infoPanel
fields.add(jLabel1);
fields.add(textField1);
fields.add(jLabel2);
fields.add(textField2);
fields.add(jLabel3);
fields.add(textField3);
fields.add(jLabel4);
fields.add(textField4);
fields.add(jLabel5);
fields.add(textField5);
fields.add(jLabel6);
fields.add(textField6);
//Sets border padding for the infoPanel
fields.setBorder(new EmptyBorder(20, 20, 0, 20));
//Draws border for the infoPanel
infoPanel.setBorder(BorderFactory.createRaisedBevelBorder());
//Sets layout for the fields panel
fields.setLayout(new GridLayout(6, 1));
//Add fields to infoPanel
infoPanel.add(fields);
//Add panels to tabbedPane
setLayout(new BorderLayout());
add(tabbedPane, BorderLayout.CENTER);
add(btnPanel, BorderLayout.PAGE_END);
add(infoPanel, BorderLayout.EAST);
}
create a compound border
field.setBorder(BorderFactory.createCompoundBorder(
field.getBorder(),
BorderFactory.createEmptyBorder(int top, int left, int bottom, int right)));
Or put some insets
field.setMargin(new java.awt.Insets(int top, int left, int bottom, int right));

JAVA Swing GridLayout not working properly

Below is the image that will clearly define my problem while using GridLayout
private void init() {
JFrame frame = new JFrame("Login");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextField loginUNTextField = new JTextField();
JTextField loginPTextField = new JTextField();
JTextField registerUNTextField = new JTextField();
JTextField registerPTextField = new JTextField();
JTextField registerETextField = new JTextField();
JButton login = new JButton("Login");
JButton register = new JButton("Register");
JLabel loginUsername = new JLabel("Username");
JLabel loginPassword = new JLabel("Password");
JLabel registerUsername = new JLabel("Username");
JLabel registerPassword = new JLabel("Password");
JLabel registerEmail = new JLabel("Email");
JPanel loginUNPanel = new JPanel(new GridLayout(3,2));
loginUNPanel.add(loginUsername);
loginUNPanel.add(loginUNTextField);
loginUNPanel.add(loginPassword); // Cause Problem
loginUNPanel.add(loginPTextField);
loginUNPanel.add(new JLabel("")); // Cause Problem
loginUNPanel.add(login);
JPanel registerUNPanel = new JPanel(new GridLayout(4,2));
registerUNPanel.add(registerUsername);
registerUNPanel.add(registerUNTextField);
registerUNPanel.add(registerPassword);
registerUNPanel.add(registerPTextField);
registerUNPanel.add(registerEmail);
registerUNPanel.add(registerETextField);
registerUNPanel.add(new JLabel(""));
registerUNPanel.add(register);
loginUNPanel.add(loginPassword);
JPanel main = new JPanel(new GridLayout(1,2));
main.add(loginUNPanel);
main.add(registerUNPanel);
frame.add(main);
frame.pack();
frame.setVisible(true);
}
Desire Result:
UserName | TextField
Passowrd | TextField
| LoginButon
Please let me know where I am worng using GridLayout.
You're adding a component twice to the container.
JPanel loginUNPanel = new JPanel(new GridLayout(3,2));
loginUNPanel.add(loginUsername);
loginUNPanel.add(loginUNTextField);
loginUNPanel.add(loginPassword); // ***** adding it once *****
loginUNPanel.add(loginPTextField);
loginUNPanel.add(new JLabel(""));
loginUNPanel.add(login);
JPanel registerUNPanel = new JPanel(new GridLayout(4,2));
registerUNPanel.add(registerUsername);
registerUNPanel.add(registerUNTextField);
registerUNPanel.add(registerPassword);
registerUNPanel.add(registerPTextField);
registerUNPanel.add(registerEmail);
registerUNPanel.add(registerETextField);
registerUNPanel.add(new JLabel(""));
registerUNPanel.add(register);
loginUNPanel.add(loginPassword); // ***** adding it again. *****
JPanel main = new JPanel(new GridLayout(1,2));
Get rid of that 2nd addition:
// loginUNPanel.add(loginPassword); // *** this
JPanel main = new JPanel(new GridLayout(1,2));

JTextField and JLabel not appearing

My JLabel and JTextField are not appearing overtop the image and I'm not sure why. I've put the image on a JLabel and setOpaque(false) but no luck. Any advice? Thanks for the help in advance.
private JTextField tf;
private JLabel jl2;
private JLabel jl3;
public void window() {
ImageIcon ic = new ImageIcon("hangman.png");
JFrame gameFrame = new JFrame();
JPanel jp = new JPanel();
jp.setOpaque(false); //!!
jp.setBorder(BorderFactory.createTitledBorder(""));
JLabel img = new JLabel(ic, JLabel.CENTER);
img.setOpaque(false);
JLabel jl = new JLabel("Enter a Letter:");
jl.setFont(new Font("Rockwell", Font.PLAIN, 20));
tf = new JTextField(1);
jl2 = new JLabel("Letters Used: ");
jl3 = new JLabel();//blank spaces
tf.setFont(new Font("Rockwell", Font.PLAIN, 20));
jl2.setFont(new Font("Rockwell", Font.PLAIN, 20));
jp.add(jl);
jp.add(tf);
jp.add(jl2);
jp.add(jl3);
gameFrame.add(img);
img.add(jp);
gameFrame.setTitle("Hangman");
gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gameFrame.setIconImage(
new ImageIcon("Hangman-Game-grey.png").getImage());
gameFrame.setResizable(false);
gameFrame.pack();
gameFrame.setLocationRelativeTo(null);
gameFrame.setVisible(true);
A JLabel doesn't use a layout manager by default so components added to the label will not be painted.
Try setting the layout manager. Maybe:
img.setLayout( new BorderLayout() );
or choose a layout that meets your requirements.
You should add image, label and textfield objecs to panel, respectively. Then, you should add that panel to your frame. Also, put gameFrame.add(img); code before calling jp.add() methods. I don't know, if this is what you want but from what you try to do, I can say below code should work:
public void window()
{
ImageIcon ic = new ImageIcon("hangman.png");
JFrame gameFrame = new JFrame();
JPanel jp = new JPanel();
jp.setOpaque(false); // !!
jp.setBorder(BorderFactory.createTitledBorder(""));
JLabel img = new JLabel(ic, JLabel.CENTER);
img.setOpaque(false);
JLabel jl = new JLabel("Enter a Letter:");
jl.setFont(new Font("Rockwell", Font.PLAIN, 20));
tf = new JTextField(1);
jl2 = new JLabel("Letters Used: ");
jl3 = new JLabel();// blank spaces
tf.setFont(new Font("Rockwell", Font.PLAIN, 20));
jl2.setFont(new Font("Rockwell", Font.PLAIN, 20));
jp.add(img);
jp.add(jl);
jp.add(tf);
jp.add(jl2);
jp.add(jl3);
gameFrame.add(jp);
gameFrame.setTitle("Hangman");
gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gameFrame.setIconImage(new ImageIcon("1.jpg").getImage());
gameFrame.setResizable(false);
gameFrame.pack();
gameFrame.setLocationRelativeTo(null);
gameFrame.setVisible(true);
}

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);
}

Setting background image

How can I get rid of that gray box?
This is what I'm talking about:
i would really appreciate if you could help me out
Full code here: http://pastebin.com/nrpCTjvV
public final void initUI() {
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setBorder(new EmptyBorder(new Insets(90, 155, 40, 60)));
JButton NewGame = new JButton ("New Game!");
JButton Highscore = new JButton("Highscore");
JButton Credits = new JButton ("Credits");
JButton Website = new JButton ("Website");
JButton Exit = new JButton ("Exit");
panel.add(NewGame);
panel.add(Box.createRigidArea(new Dimension(0, 5)));
panel.add(Highscore);
panel.add(Box.createRigidArea(new Dimension(0, 5)));
panel.add(Credits);
panel.add(Box.createRigidArea(new Dimension(0, 5)));
panel.add(Website);
panel.add(Box.createRigidArea(new Dimension(0, 5)));
panel.add(Exit);
final ButtonGroup entreeGroup = new ButtonGroup();
JRadioButton radioButton;
panel.add(radioButton = new JRadioButton("Music1"));
radioButton.setActionCommand("Music1");
entreeGroup.add(radioButton);
panel.add(radioButton = new JRadioButton("Music2"));
radioButton.setActionCommand("Music2");
entreeGroup.add(radioButton);
panel.add(radioButton = new JRadioButton("No Music", true));
radioButton.setActionCommand("No Music");
entreeGroup.add(radioButton);
add(panel);
pack();
setTitle("Title");
JLabel background = new JLabel(new ImageIcon("background.png"));
add(background);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
setSize(400, 400);
}
add(panel);
pack();
setTitle("Title");
JLabel background = new JLabel(new ImageIcon("background.png"));
add(background);
The default layout manager for a JFrame is a BorderLayout. When you add a component without specifying a constraint the component is added to the CENTER. You can't add multiple components to a single location.
Instead you need to use a different component as the background. Then you add your panel to this component. Check out Background Panel. Then code would be something like:
Background background = new BackgroundPanel(...);
background.add(panel);
add(background);
setResizable(false);
pack();
...

Categories