Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GUI Set Up - Beginner Java Question - FlowLayout?

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hello:

I am getting confused with setting up a GUI with two JPanels one that would be on top and the other middlePanel having a FlowLayout.

I set up two JPanels but obviously have something wrong because my northPanel has "disappeared" when I added a middlePanel. My componets are all over the place and again my northPanel is no longer showing.

Idea of what I'm doing.

1) Enter total number of diners
2)Confirm that diners # is correct.
3)Enter name of Diner
4)Take order - Entree (Pull-Down)
5)Two sides (CheckBox)
6)Display Completed Order of diners.

P.S. I hope my question is not too stupid I am new and has justed started Java Programming. I have tried to look through the Documentation but am getting confused with GUI relating to FlowLayout, GridLayout. etc. I'm just not sure which one I should use to set up my GUI in an organized manner. Am I on the right track or is my code completely screwed. Thanks.

** My Code **


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


public class Menu extends JFrame {
private JTextField partyNumField, dinerName;
private JComboBox orderComboBox;
private int partyNum;
private JButton getParty, continueOrder;
private JLabel party, companyLogo, dinerLabel, entreeOrder;
private String dinnerEntree[] = {"Filet Mignon", "Chicken Cheese Steak", "Tacos", "Ribs"};
private JCheckBox mashed, cole, baked, french;

public Menu() {

super("O'Brien Caterer - Where we make good Eats!");

Container container = getContentPane();


JPanel northPanel = new JPanel();
northPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 80, 5));
companyLogo = new JLabel("Welcome to O'Brien's Caterer's");
northPanel.add(companyLogo);


party = new JLabel("Enter the Total Number in Party Please");
partyNumField = new JTextField(5);
northPanel.add(party);
northPanel.add(partyNumField);


getParty = new JButton("GO - Continue with Order");

getParty.addActionListener(

new ActionListener(){

public void actionPerformed(ActionEvent actionEvent)
{

partyNum = Integer.parseInt(partyNumField.getText());
String ans=JOptionPane.showInputDialog(null, "Total Number is party is: "
+ partyNum + " is this correct?\n\n" + "Enter 1 to continue\n"
+ "Enter 2 to cancel\n");
if (ans.equals("1")) {
System.out.println(ans+"=continue"); // handle continue
} else { // assume to be 2 for cancel
System.out.println(ans+"=cancel"); // handle cancel
}
}
}
); // end Listener

northPanel.add(getParty);

JPanel middlePanel = new JPanel();
middlePanel.setLayout(new FlowLayout(FlowLayout.CENTER));
dinerLabel = new JLabel("Please enter Diner's name");
dinerName = new JTextField(30);
continueOrder = new JButton("continue");

middlePanel.add(dinerLabel);
middlePanel.add(continueOrder);
middlePanel.add(dinerName);

entreeOrder = new JLabel("Please choose an entree");
orderComboBox = new JComboBox(dinnerEntree);
orderComboBox.setMaximumRowCount(4);

//orderComboBox.addItemListener(

// new ItemListener(){
// public void itemsStateChanged(ItemEvent event)
// {
// if (event.getStateChange() == ItemEvent.SELECTED)
// add entree order to Person
// continue ** enable the two sides order
// }
// }
// );

mashed = new JCheckBox("Mashed Potatoes");
middlePanel.add(mashed);
cole = new JCheckBox("Cole Slaw");
middlePanel.add(cole);
baked = new JCheckBox("Baked Beans");
middlePanel.add(baked);
french = new JCheckBox("FrenchFries");
middlePanel.add(french);

// CheckBoxHandler handler = new CheckBoxHandler();
// mashed.addItemListener(handler);
// cole.addItemListener(handler);
// baked.addItemListener(handler);
// french.addItemListener(handler);


middlePanel.add(entreeOrder);
middlePanel.add(orderComboBox);

container.add(northPanel);
container.add(middlePanel);
middlePanel.setEnabled(true);
setSize(500, 500);
show();
}
// private class to handle event of choosing Check BOx Item
// private class CheckBoxHandler implements ItemListener{
// private int count = 0;

// public void itemStateChanged(ItemEvent event){

// if (event.getsource() == mashed)
// if (event.getStateChange() == ItemEvent.SELECTED)
// count++;
// add mashed choice to person's order

// if (event.getsource() == cole)
// if (event.getStateChange() == ItemEvent.SELECTED)
// count++;
//add cole slaw to person's order

// if (event.getsource() == baked)
// if (event.getStateChange() == ItemEvent.SELECTED)
// count++;
//add baked beans to person's order

// if (event.getsource() == french)
// if (event.getStateChange() == ItemEvent.SELECTED)
// count++;
//add french to person's order
// }


public static void main(String args[])
{
Menu application = new Menu();
application.addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent windowEvent)
{
System.exit(0);
}
}
);
}
}
 
The key to laying out components in Swing or AWT is to layer,layer, layer.

Layering allows you to use flow or grid layout in alot of cases for the major components and you only need to use gridbag for a few pieces that need fine grained control over placement.

In JFC, you will learn more by banging your head against the keyboard for a few hours and reading and re-reading the API and java tutorials than by me telling you a quick fix to your problem. 8^)
 
I have found it easiest to use a base panel to which
I add everything else and then do a
getContentPane().add(basePanel);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top