answersLogoWhite

0


Best Answer

You don't actually add items to a LayoutManager object. You should set the layout of the Container you want to add your Components to and the GridLayout will put them where they need to be.

Container myContainer = new Container();

int numRows = 3;

int numCols = 2;

GridLayout myLayoutManager = new GridLayout(numRows, numCols);

myContainer.setLayout(myLayoutManager);

// Add some JButtons to the container...

myContainer.add(new JButtons ("1"));

myContainer.add(new JButtons ("2"));

myContainer.add(new JButtons ("3"));

myContainer.add(new JButtons ("4"));

myContainer.add(new JButtons ("5"));

myContainer.add(new JButtons ("6"));

Your final component will look something like the following:

| 1 | 2 |

| 3 | 4 |

| 5 | 6 |

User Avatar

Wiki User

14y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you add items in GridLayout in Java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is GridLayout?

GridLayout organizes a JPanel like a 2-dimensional array (think battleship board game board) so you can easier add items to it in an organized manner


How do you set button size in GridLayout java?

You don't. GridLayout is one of the layout managers that completely ignores all .setSize and .setPreferredSize method calls. If you want to set the size of buttons in a GridLayout, you should add each button to a JPanel, and add the JPanels to the Container with a GridLayout. This way you can use, for example, a FlowLayout on the JPanel and use a button.setPreferredSize method call to try to keep the buttons a particular size.


What are different types of layout manager available in java awt?

A layout manager is an object that is used to organize components in a container.And it is an interface in the java class libraries that describes how a container and a layout manager communicate. There are 5 main Layouts in java :------ 1. FlowLayout 2. BorderLayout 3. CardLayout 4. GridLayout 5. GridbagLayout


How can add java code to java server page?

Scriplet.


Can we add website link when we write java program?

yes ,i can add the website link in java program when we write.


Should you remove java from your computer?

Java is required to download a lot of items from the internet. If you never download anything from the internet, you can remove Java.


What is better Adobe Flash Player or java spript for YouTube?

Java because it lets you do items abode can't do


Can you add smiley on your java notepad?

yes


Is there any way to add items in history in java-script?

If you're in HTML 5 then the history.pushState() method can be used to add an item into the history. Not all the browsers support the method (yet.) You can find out more over at Mozilla (see related link.)


What is the way to get rid of Java lang no class deffound error?

To get rid of the Java lang no class deffound error, one must add the class or .jar file which contains this class into the Java classpath. When a Java class is run from the command line, one must add a dot (.)


What is HashMap Java used for?

HashMap Java is used as a definition in Java software programming language. This version of Java is used to tag objects using hash numbers and therefore differentiate different items in different databases.


How do you save a notepad file into a java file?

just add an extension .java when you save your notepad text file