0

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();
}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
tarun
  • 1
  • 3
  • 1
    Don't use `setSize(...)`. That is just a guess as to what the size of the frame should be. Instead, AFTER you add the components to the frame and BEFORE you make the frame visible use the `pack()` method. – camickr Nov 25 '21 at 14:44
  • I did do that. The component is still out of screen. – tarun Nov 26 '21 at 05:00
  • This is [how it appears here](https://i.stack.imgur.com/XWCjY.png) using a 10 x 10 black icon. Are you seeing something different? OTOH this is [what it looks like when packed](https://i.stack.imgur.com/ap9At.png). – Andrew Thompson Nov 26 '21 at 08:02
  • Well, I think it's the size of images that's messing up my view. – tarun Nov 26 '21 at 11:55
  • 1
    *"I think it's the size of images"* Can you vague that up for me? It's in danger of conveying useful information. Information like .. what size are the images used in your app? (BTW - a future tip. Post code that hot-links to images on the net, then people trying to help would not need to make guesses about image size etc). – Andrew Thompson Nov 26 '21 at 12:47
  • 1
    @tarun *I think it's the size of images* - If the images are too big, they are not automatically resized, so yes they may be truncated. It worked fine for me with smaller images. – camickr Nov 26 '21 at 14:13
  • I've tried that code again, increasing the image sizes up to 200 x 200 and it works flawlessly, as long as `pack()` is called. So if you cannot get it working there, please post code that shows a call to `pack()` and hot-links to images. I put [a few images here](https://stackoverflow.com/a/19209651/418556) for exactly that use. – Andrew Thompson Nov 26 '21 at 14:24
  • I used `getScaledInstance()` method of ImgaeIcon class and with `pack()`, it's working fine now. Thanks a lot for your help. – tarun Nov 27 '21 at 06:37

0 Answers0