I am trying to make a layout where all buttons have image and text. The five buttons are placed in two rows with buttons in second row placed in between gaps of first row. The problem is button3(btn3)/Third menu is out of screen/not visible. I am placing the Grouplayout in center of Borderlayout. Here is the code:
public class UI2 extends JFrame {
UI2() {
setSize(600, 600);
setTitle("Dashboard");
Container c = getContentPane();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel jpmain = new JPanel();
GroupLayout layout = new GroupLayout(jpmain);
jpmain.setLayout(layout);
ImageIcon image = new ImageIcon(new File("path").getPath());
JButton btn1 = new JButton(image);
btn1.setText("First Menu");
btn1.setHorizontalTextPosition(SwingConstants.CENTER);
btn1.setVerticalTextPosition(SwingConstants.BOTTOM);
image = new ImageIcon(new File("path").getPath());
JButton btn2 = new JButton(image);
btn2.setText("Second Menu");
btn2.setHorizontalTextPosition(SwingConstants.CENTER);
btn2.setVerticalTextPosition(SwingConstants.BOTTOM);
image = new ImageIcon(new File("path").getPath());
JButton btn3 = new JButton(image);
btn3.setText("Third Menu");
btn3.setHorizontalTextPosition(SwingConstants.CENTER);
btn3.setVerticalTextPosition(SwingConstants.BOTTOM);
image = new ImageIcon(new File("path").getPath());
JButton btn4 = new JButton(image);
btn4.setText("Fourth Menu");
btn4.setHorizontalTextPosition(SwingConstants.CENTER);
btn4.setVerticalTextPosition(SwingConstants.BOTTOM);
image = new ImageIcon(new File("path").getPath());
JButton btn5 = new JButton(image);
btn5.setText("Fifth Menu");
btn5.setHorizontalTextPosition(SwingConstants.CENTER);
btn5.setVerticalTextPosition(SwingConstants.BOTTOM);
JPanel jp = new JPanel();
layout.setHorizontalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(btn1)
.addComponent(jp))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(jp)
.addComponent(btn4))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(btn2)
.addComponent(jp))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(jp)
.addComponent(btn5))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(btn3)
.addComponent(jp)));
layout.setVerticalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
.addComponent(btn1)
.addComponent(jp)
.addComponent(btn2)
.addComponent(jp)
.addComponent(btn3))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
.addComponent(jp)
.addComponent(btn4)
.addComponent(jp)
.addComponent(btn5)
.addComponent(jp)));
layout.linkSize(btn1,btn2,btn3,btn4,btn5);
c.add(jpmain,BorderLayout.CENTER);
c.add(new JPanel(), BorderLayout.WEST);
c.add(new JPanel(),BorderLayout.EAST);
c.add(new JPanel(), BorderLayout.NORTH);
c.add(new JPanel(),BorderLayout.SOUTH);
setVisible(true);
}
public static void main(String[] args) {
UI2 frame = new UI2();
}
}