2

I am creating a button and label in JPanel. This panel i am adding to a JScrollPane which is eventually added to a frame. Here is my code.

public static void main(String[] args) {

    final JFrame frame = new JFrame();
    JPanel Jpanel =new JPanel(new GridLayout(0, 1));
    JScrollPane pane = new JScrollPane(Jpanel,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

    frame.setSize(500, 500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    JLabel n = new JLabel("hifadfad");
    n.setBounds(90, 20, 100, 100);
    Jpanel.add(n);

    JButton b = new JButton("hi");
    b.setBounds(10, 40, 60, 60);
    Jpanel.add(b);

    frame.add(pane);
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            frame.setVisible(true);
        }
    });
}

After i run, i get something like this enter image description here

How should i set the size of this button ?

Thanks for the help. Appreciate :)

Vrushank Doshi
  • 2,428
  • 5
  • 20
  • 34
  • possible duplicate of [How can I set size of a button?](http://stackoverflow.com/questions/2536873/how-can-i-set-size-of-a-button) – Maddy Aug 13 '15 at 00:01
  • I am still not able to figure out how to solve this problem. I tried different layout managers but didn't work out or else i may be doing something wrong. Could some one please solve it for me ? – Vrushank Doshi Aug 13 '15 at 01:11
  • Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Aug 13 '15 at 03:24
  • For making a button bigger, use a large font or a large icon. We can add more space around the text or icon by calling `setMargin(Insets)`. – Andrew Thompson Aug 13 '15 at 03:28

3 Answers3

1

Start by having a look at Laying Out Components Within a Container, which will provide with more information about how Swing handles component placement and sizing.

Wlecome

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());

            JLabel lbl = new JLabel("Welcome");
            JButton btn = new JButton("Hi");

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;

            add(lbl, gbc);
            add(btn, gbc);
        }

    }

}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • There will be many buttons and labels on the frame, so i also need a scroll bar in the frame. – Vrushank Doshi Aug 13 '15 at 00:23
  • No, you don't, you need to wrap the component/panel in a `JScrollPane`, see [How to Use Scroll Panes](http://docs.oracle.com/javase/tutorial/uiswing/components/scrollpane.html) for more details – MadProgrammer Aug 13 '15 at 00:25
  • I am sorry, i need a scroll pane to wrap up the panel into it. That's what i did right ? the concern is setting the size of button. – Vrushank Doshi Aug 13 '15 at 00:31
  • Yeah, that looks right, but you'll to use a different layout manager other the `GridLayout` (for the `Jpanel`). You might also consider a `JTable` and `JList`, but I don't have the full context to provide a certain suggestion – MadProgrammer Aug 13 '15 at 00:35
  • *"There will be many buttons and labels on the frame,.."* Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height. – Andrew Thompson Aug 13 '15 at 03:29
0

This is problem is rather complicated to solve, but there are several options:

  1. use a GridBagLayout. This will allow you (in a limited way) to specify size and position of the single elements
  2. generate your own layoutmanager matching your constraints.
  3. This isn't exactly a nice solution: remove the layoutmanager from the panel and set the position and size of all elements manually. Note that this requires you aswell to specify the preferred size (in some cases even the minimum size) of the panel, since otherwise the panel won't be shown in the scrollpane (size = (0 , 0)).

Just experimentate a bit with the single options. Note that solution 3 is rather mentioned for completeness than because you should use it.

-2

Put in your code

Jpanel.setLayout(null);
Mihai8
  • 3,113
  • 1
  • 21
  • 31
  • 1
    Avoid using `null` layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify – MadProgrammer Aug 13 '15 at 00:26