Question

hello i am trying to declare a BoxLayout the same way of declaring the BorderLayout

the way of declaring the borderlayout

JPanel textFiled = new JPanel(new BorderLayout());
    textFiled.add(enterText,BorderLayout.CENTER);

but it is not working with BoxLayout because it is for one Component and width and i watched some tutorial some are declaring a a method but i don't wanna to do that so i tried to put it in other JPanel and nothing happens

JPanel t = new JPanel();
        t.add(startTime);
        t.add(endTime);

        JPanel timing = new JPanel(new BoxLayout(t, BoxLayout.PAGE_AXIS));

and what will happen if i set the width as 50 for example and what are my chooses

Was it helpful?

Solution

You can't create the panel and set the BoxLayout in one statement (like you do for the BorderLayout). You need to use two statements since the BoxLayout needs a reference to the panel that you want to use as a BoxLayout.

1) Read the section from the Swing tutorial on How to Use Box Layout. It will show you how to create a panel using a BoxLayout and how to add components to the panel.

2) Or, you can check out the Box class. It is a convenience class that will allow you to create a container using a BoxLayout in a single statement.

Box timing = new Box(...);
timing.add( t );

Take a look at the table of contents for the tutorial as it contains many tutorial on basic Swing functionality.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top