What is the use of Box class?
Hello friend,
I need you help to sort-out my query which is related to the java classes. I want to be aware about the 'Box class' of java programming. I don't know anything about the use of Box class. I have referred many java book for Box class, but nothing was helpful. I wonder if you are able to provide me some information about the Box class of java along with suitable example for the same. I am waiting for your reply.
Re: What is the use of Box class?
Box class is a lightweight container that uses a BoxLayout object as its layout manager. Box provides several class methods that are useful for containers using BoxLayout - even non-Box containers. The Box class can create several kinds of invisible components that affect layout: glue, struts, and rigid areas. If all the components your Box contains have a fixed size, you might want to use a glue component (returned by createGlue) to control the components' positions.
Re: What is the use of Box class?
Hi friend,
I suggest you to refer following example of 'Box class', which will give you something idea about the use of this class:
Code:
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JTextField;
public class BoxDM
{
public static void main(String argsw[]) {
JFrame verticalFrame1 = new JFrame("Vertical");
verticalFrame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Box verticalBox1 = Box.createVerticalBox();
verticalBox1.add(new JLabel("Top"));
verticalBox1.add(new JTextField("Middle"));
verticalBox1.add(new JButton("Bottom"));
verticalFrame1.add(verticalBox, BorderLayout.CENTER);
verticalFrame1.setSize(250, 150);
verticalFrame1.setVisible(true);
}
}
Re: What is the use of Box class?
Please review following some of the important methods of 'Box class':
- setLayout(LayoutManager lm)
- getAccessibleContext()
- createVerticalStrut(int height1)
- createRigidArea(Dimension dm)
- createHorizontalStrut(int width1)
- createHorizontalGlue()
Re: What is the use of Box class?
In following example I have used different methods of Box class of java:
Code:
import javax.swing.Box;
import javax.swing.JTextField;
public class SampleBox
{
public static void main(String argvs[])
{
JFrame hf = new JFrame("Horizontal");
hf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Box hb = Box.createHorizontalBox();
hb.add(new JLabel("Left"));
hb.add(new JTextField("Middle"));
hb.add(new JButton("Right"));
hf.add(hb, BorderLayout.CENTER);
hf.setSize(150, 150);
hf.setVisible(true);
}
}
Re: What is the use of Box class?
Hello friend,
If you want to use the properties of Box class, then you need to be aware about the following nested classes:
- Component.BltBufferStrategy
- Box.AccessibleBox
- Container.AccessibleAWTContainer
- Component.FlipBufferStrategy
- JComponent.AccessibleJComponent