0

In one of my application I want to add some JButtons in a limited space so I created a JPanel of the required size, but when I added the JButtons on that JPanel, they are not visible in the given space.

Here is the code:

import javax.swing.*;
import java.awt.event.*;

class MyTest
{
    public static void main(String... args) {
        JFrame jf=new JFrame();
        JPanel jp=new JPanel();
        jp.setSize(10,10);
        jp.add(new JButton("1"));
        jp.add(new JButton("2"));
        jp.add(new JButton("3"));

        jf.add(jp);

        jf.setSize(500,500);
        jf.setVisible(true);
    }
}
akhil singhal
  • 111
  • 1
  • 7
  • Your `jp` panel will not have the size you specified, since the frame is bigger and it is placed by default on `BorderLayout.CENTER`. This means that the panel will take all the frame's space, except from the frame decorations. Please clarify what you actually want to achieve, also remember that `setSize ()` method should **not** be used, instead, let the layout calculate all component dimensions using `pack ()` method on your frame – Ansharja Oct 31 '17 at 17:34
  • in my code i want to add the Jbuttons in the limited area if the JButton is increased the Button size should decrease Accordingly but buttons should not come out from the specific area. – akhil singhal Oct 31 '17 at 17:39
  • Try a `JComponent.sizeVariant`, for [example](https://stackoverflow.com/a/14599176/230513). – trashgod Oct 31 '17 at 18:23

1 Answers1

0

i want to add the Jbuttons in the limited area if the JButton is increased the Button size should decrease

Use a GridLayout. Space will be given equally to each button and will change as the frame is resized.

Read the section from the Swing tutorial on How to Use GridLayout for more information and working examples.

Of course if the space available is too small, then the text of the buttons will be truncated.

And you would probably want to use the following so the buttons don't take up the entire frame:

//jf.add(jp);
jf.add(jp, BorderLayout.PAGE_START);

Read the section from the tutorial on How to Use BorderLayout to understand what the above code does.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Still The problem exists even if i am specifying the GridLayout with the correct row and Columns it is not following that area.It Should Print all The buttons in the given area specified by a 10*10 rectangle – akhil singhal Oct 31 '17 at 18:31
  • @akhilsinghal, `area specified by a 10*10 rectangle` - makes no sense. That means each button would be 3 pixels. You would see nothing of the button except the border. I think you need to clarify your question. What do you expect to see with one button, two buttons, 3 buttons, 4 buttons etc.? – camickr Oct 31 '17 at 19:44
  • this was for an example the correct scenario is in area of 120*50 pixel rectangle i have to add 7 JCheckBox .The Problem is the Jcheckbox is taking a little more space so i want to confined it in the 120*50 rectangle and it should be resized accordingly to the area – akhil singhal Nov 01 '17 at 06:05
  • This is not how GUI's are created. Every LAF can use different fonts or different borders, so you never know what the exact size of a component should be. For example on my platform the preferred size of a single checkbox is 32x24 so if won't fit in an area you just specified. You can adjust the Font to make the preferred size smaller. But you can only make the Font so small before the text is not readable. When you use layout manager effectively each component will adjust as required. I already gave you a suggestion. – camickr Nov 01 '17 at 14:40
  • i will try by reducing the text size – akhil singhal Nov 02 '17 at 17:12