0

I am new in java applications and I am trying to create a window with 3 buttons in a row at the bottom of the window with extended size. However gridheight doesn't have any affect on my buttons. gridwidth works fine.

Can you give me a feedback on what I am doing wrong? I guess I haven't understand something correctly.

Bellow is my code and the output window.

Thanx in advance!

private JButton button1;
private JButton button2;
private JButton button3;

public SessionTasksPage() 
{
     JPanel p1=new JPanel(new GridBagLayout());
     JPanel p2=new JPanel();

     button1 = new JButton("b1");    
     button2 = new JButton("b2"); 
     button3 = new JButton("b3");

     GridBagConstraints gbc =new GridBagConstraints();

     //gbc.insets = new Insets(1,1,1,1);

     gbc.gridheight = 5;
     gbc.gridwidth = 5;
     gbc.weightx=15;
     gbc.weighty=15;
     gbc.fill = GridBagConstraints.BOTH;

     p1.add(button2,gbc);
     p1.add(button3,gbc);
     p1.add(button1,gbc);

     add(p1,BorderLayout.SOUTH);
     add(p2,BorderLayout.NORTH);

     EventHandler1 event1 = new EventHandler1();
     button1.addActionListener(event1);

     //window terminate setting 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     //window size
     setSize(600,400);
     //pack();
     //make window visible
     setVisible(true);
     //window title
     setTitle("HCI-Projet");
     //voice command
     //new AePlayerWave("bin/sounds/login.wav").start();
}

public class EventHandler1 implements ActionListener
{
    public void actionPerformed(ActionEvent event1) {
        System.exit(0);
    }
}

enter image description here

roeygol
  • 4,908
  • 9
  • 51
  • 88
m_papas
  • 91
  • 1
  • 12
  • How is gridwidth doing anything different? You have it set to the same number as gridHeight, 5, and neither the width nor the height of those buttons looks like 5 pixels. – DaaaahWhoosh Jan 29 '15 at 19:30
  • it affects. the default width of the buttons (if I put gbc.gridwidth = 5; as a comment) is much smaller – m_papas Jan 29 '15 at 19:41
  • @user1866935 If you do find that a particular solution **works** for you, marking it as accepted can really help people who come behind you, and read your question, and have the same issues. If a particular solution **doesn't work** for you, leave some comments or create a discussion so that we can help you solve your problem. I promise I'm not saying this just because I have an answer below; if the other answer helped you, accept that one! – Xynariz Jan 30 '15 at 22:58
  • @DaaaahWhoosh - gridHeight doesn't mean anything in pixels. Rather, it refers to the height as a number of cells in a grid within the GridBagLayout. – Xynariz Jan 30 '15 at 22:59

2 Answers2

1

The problem lies the layout of your main panel. BorderLayout, which you use on that panel, has one large central area (BorderLayout.CENTER), and many smaller areas (such as NORTH and SOUTH). The layout will force anything put anywhere other than CENTER to take up the smallest amount of room possible, and max out the remaining room with what is in the CENTER area (with nothing, in this case). Since you're adding p1 as SOUTH, it keeps the vertical component of p1 as small as it can. If you add p1 as CENTER, it should fix your problem.

As a side note, if all the rows/columns in your grid will have the same height/width, you may find using GridLayout much easier than GridBagLayout.

Xynariz
  • 1,232
  • 1
  • 11
  • 28
0

The problem is the JPanel p1. The space of the panel is created by the standard layout, and if you just use add(p1,BorderLayout.SOUTH);, it won't change.

To make it visible you could use a Border: p1.setBorder(BorderFactory.createLineBorder(Color.black));

Use a different layout or a GUI builder.

Xynariz
  • 1,232
  • 1
  • 11
  • 28
ThoFin
  • 1,467
  • 14
  • 20
  • The problem is not the layout of `p1` (which is `GridBagLayout`). The problem is the main (unnamed) panel. – Xynariz Jan 30 '15 at 19:51
  • The comment is saying what i said, isnt it. – ThoFin Jan 30 '15 at 20:03
  • No, your answer said that "there problem is the JPanel p1". The problem is *not* in p1. Also, you said that it's using the standard layout, which is specifically not the case. – Xynariz Jan 30 '15 at 20:04
  • I didnt say that it is the layout of JPanel p1. – ThoFin Jan 30 '15 at 20:07
  • The main panel is also not using standard layout, it's using `BorderLayout`. This is easily provable: if it was using `FlowLayout`, which is the standard, the buttons would be on top, not on the bottom. – Xynariz Jan 30 '15 at 20:08