convert to a different layout

im only familiar with the border and flow layouts, is there another one i could use for this code?import java.awt.BorderLayout;

import java.awt.Container;

import java.awt.GridLayout;

import javax.swing.BorderFactory;

import javax.swing.JButton;

import javax.swing.JCheckBox;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.border.Border;

publicclass makeup{

publicstaticvoid main(String args[]){

String title = (args.length == 0 ?"CheckBox Sample" : args[0]);

JFrame frame =new JFrame(title);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel panel =new JPanel(new GridLayout(0, 1));

Border border = BorderFactory.createTitledBorder("Sub Toppings");

panel.setBorder(border);

JCheckBox check =new JCheckBox("Anchovies");

panel.add(check);

check =new JCheckBox("Garlic");

panel.add(check);

check =new JCheckBox("Onions");

panel.add(check);

check =new JCheckBox("Pepperoni");

panel.add(check);

check =new JCheckBox("Spinach");

panel.add(check);

JButton button =new JButton("Submit");

Container contentPane = frame.getContentPane();

contentPane.add(panel, BorderLayout.CENTER);

contentPane.add(button, BorderLayout.SOUTH);

frame.setSize(300, 200);

frame.setVisible(true);

}

}

[2531 byte] By [bronze-starDukes] at [2007-11-26 12:13:17]
# 1

> im only familiar with the border and flow layouts, is there another one i could use for this code?

this link will give you a visual idea of how the various managers look,

experiment with the ones similar to how you want your code to appear.

http://java.sun.com/docs/books/tutorial/uiswing/layout/visual.html

platinumsta at 2007-7-7 14:14:22 > top of Java-index,Archived Forums,Socket Programming...
# 2

thanks, i like the card layout, but it wont work -- the applet wont initialize

in fact, none of them will

/*

* CardLayoutDemo.java requires no other files.

*/

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class makeup implements ItemListener {

JPanel cards; //a panel that uses CardLayout

final static String BUTTONPANEL = "JPanel with JButtons";

final static String TEXTPANEL = "JPanel with JTextField";

public void addComponentToPane(Container pane) {

//Put the JComboBox in a JPanel to get a nicer look.

JPanel comboBoxPane = new JPanel(); //use FlowLayout

String comboBoxItems[] = { BUTTONPANEL, TEXTPANEL };

JComboBox cb = new JComboBox(comboBoxItems);

cb.setEditable(false);

cb.addItemListener(this);

comboBoxPane.add(cb);

//Create the "cards".

JPanel card1 = new JPanel();

card1.add(new JButton("Button 1"));

card1.add(new JButton("Button 2"));

card1.add(new JButton("Button 3"));

JPanel card2 = new JPanel();

card2.add(new JTextField("TextField", 20));

//Create the panel that contains the "cards".

cards = new JPanel(new CardLayout());

cards.add(card1, BUTTONPANEL);

cards.add(card2, TEXTPANEL);

pane.add(comboBoxPane, BorderLayout.PAGE_START);

pane.add(cards, BorderLayout.CENTER);

}

public void itemStateChanged(ItemEvent evt) {

CardLayout cl = (CardLayout)(cards.getLayout());

cl.show(cards, (String)evt.getItem());

}

/**

* Create the GUI and show it. For thread safety,

* this method should be invoked from the

* event-dispatching thread.

*/

private static void createAndShowGUI() {

//Create and set up the window.

JFrame frame = new JFrame("CardLayoutDemo");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Create and set up the content pane.

makeup demo = new makeup();

demo.addComponentToPane(frame.getContentPane());

//Display the window.

frame.pack();

frame.setVisible(true);

}

public static void main(String[] args) {

//Schedule a job for the event-dispatching thread:

//creating and showing this application's GUI.

javax.swing.SwingUtilities.invokeLater(new Runnable() {

public void run() {

createAndShowGUI();

}

});

}

}

bronzestar at 2007-7-7 14:14:22 > top of Java-index,Archived Forums,Socket Programming...
# 3
> but it wont work -- the applet wont initialize in fact, none of them willNot sure what you are talking about.The code works fine. Its an application, not an applet. There is a difference.
platinumsta at 2007-7-7 14:14:22 > top of Java-index,Archived Forums,Socket Programming...
# 4
I've found that the best way to get your specified layout is setting the container layout to null, and then setting the bounds for each of your componentssetLayout(null);component.setBounds(x,y,width,height)
bronzestar at 2007-7-7 14:14:22 > top of Java-index,Archived Forums,Socket Programming...
# 5
i was pasting the code into an applet window, im an idiot, thanks i got it to work
bronzestar at 2007-7-7 14:14:22 > top of Java-index,Archived Forums,Socket Programming...
# 6

> I've found that the best way to get your specified layout is setting the container layout to null,

You should always use Layout Managers. Read the tutorial link given above.

The only time you would use a null layout is when you need the ability to drag and position components in the container.

platinumsta at 2007-7-7 14:14:22 > top of Java-index,Archived Forums,Socket Programming...