Java Forum / General / March 2006
Cardlayout GUI
IanH - 12 Mar 2006 23:30 GMT Hi,
I am currently working on a Payroll Interface. I have been experimenting with a cardayout view. First, I want to allow employees to enter their first and last name and to pick how their remuneration is calculated ie, salary, hourly, commission etc.
At the moment I just have a combo-box that allows each employee to pick their remuneration type and based on this selection a card is called.
My question is - is it possible to display a Jtextfield for the first and last name on the interface with the combo-box, and if so how would i go about it.
any help would be appreciated.
Ian
import java.awt.*; import java.awt.event.*; import javax.swing.*;
public class PayrollSystem implements ItemListener { JPanel cards; //a panel that uses CardLayout final static String HOURLYPANEL = "Hourly"; final static String COMMISSIONPANEL = "Commission"; final static String BASEPLUSPANEL = "BasePlusCommission"; final static String SALARYPANEL = "Salary";
public void addComponentToPane(Container pane) { //Put the JComboBox in a JPanel to get a nicer look. JPanel comboBoxPane = new JPanel(); //use FlowLayout String comboBoxItems[] = { HOURLYPANEL, COMMISSIONPANEL, BASEPLUSPANEL, SALARYPANEL }; JComboBox cb = new JComboBox(comboBoxItems); cb.setEditable(false); cb.addItemListener(this); comboBoxPane.add(cb);
//Create the "cards". JPanel card1 = new JPanel(); card1.add(new JLabel("Hours worked")); card1.add(new JTextField(6)); card1.add(new JLabel("Rate of Pay")); card1.add(new JTextField(8)); card1.add(new JButton("Submit"));
JPanel card2 = new JPanel(); card2.add(new JLabel("Total Sales")); card2.add(new JTextField(8)); card2.add(new JLabel("Commission Rate")); card2.add(new JTextField(8)); card2.add(new JButton("Submit"));
JPanel card3 = new JPanel(); card3.add(new JLabel("Basic Pay")); card3.add(new JTextField(8)); card3.add(new JLabel("Total Sales")); card3.add(new JTextField(8)); card3.add(new JLabel("Commision Rate")); card3.add(new JTextField(8)); card3.add(new JButton("Submit"));
JPanel card4 = new JPanel(); card4.add(new JLabel("Salary")); card4.add(new JTextField(10)); card4.add(new JButton("Submit"));
//Create the panel that contains the "cards". cards= new JPanel(new CardLayout()); cards.add(card1, HOURLYPANEL); cards.add(card2, COMMISSIONPANEL); cards.add(card3, BASEPLUSPANEL); cards.add(card4, SALARYPANEL);
pane.add(comboBoxPane, BorderLayout.PAGE_START); pane.add(cards, BorderLayout.CENTER);
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()); }
/** * GUI created * */ private static void createAndShowGUI() { //Make sure we have nice window decorations. JFrame.setDefaultLookAndFeelDecorated(true);
//Create and set up the window. JFrame frame = new JFrame("Payroll Interface System"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane. PayrollSystem demo = new PayrollSystem(); 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(); } }); } }
Rhino - 13 Mar 2006 06:20 GMT > Hi, > [quoted text clipped - 11 lines] > > any help would be appreciated. A very good place to start to learn how to use the different layout managers is the Java Tutorial. You can find it here: http://java.sun.com/docs/books/tutorial/index.html. You want the chapter (they call them "trails") named "Creating a GUI with JFC/Swing". If you click on that chapter, you'll see a section of the chapter entitled "Laying Out Components Within A Container"; click on that. I suggest that you then click on "A Visual Guide To Layout Managers" to get an overview of the different standard layout managers; you may find one that is more appropriate than CardLayout for your requirements. In any case, choose the one that seems best and then click on the 'How to' article for that Layout Manager for a proper description of how to use each one.
In answer to your specific question, yes, you can certainly have one or more text fields on the same card as a combo box. The specifics of how to create text fields can be found by going to this page: http://java.sun.com/docs/books/tutorial/uiswing/components/components.html and then clicking on the "text field" link (I doubt that you want a formatted text field for this problem.) That will show you how to create the text fields; there is a link to an article on creating combo boxes on that page too. Once you've created the components though, you will need to figure out how to lay them out; they usually won't become visible unless they have been layed out correctly. That's where the layout manager pages are going to come in handy :-)
Basically, I think you have the right design in mind: you want the first card to prompt for the name and the pay type; then, based on the value chosen from the combo box, you go to another card that prompts for the remaining pay information that is necessary for that pay type. Now, if you really want to start to look professional, you can do some edits on the information the user gives to show that you won't accept just any old data but that probably won't be expected in a school assignment. Then, you'd likely save the information somewhere; in a professional application, it would likely be to a database. Again, though, a school project probably won't expect you to go that far.
This should get you started. Ask followup questions if you start getting stuck in the details and someone will probably be along to help you.
-- Rhino
> import java.awt.*; > import java.awt.event.*; [quoted text clipped - 96 lines] > } > } Paul Hamaker - 13 Mar 2006 09:17 GMT Ian, this example explains CardLayout : http://javalessons.com/cgi-bin/fun/java-tutorials-main.cgi?ses=ao789&code=cdl&sub=gui You can lookup other layout examples and explanations in the index of the GUI unit : http://javalessons.com/cgi-bin/fun/java-tutorials-main.cgi?sub=gui&ses=ao789 -------------------- Paul Hamaker, SEMM, teaching ICT since 1987 http://javalessons.com
IanH - 13 Mar 2006 23:21 GMT Hi,
I have been working on the above problem. I have tried to insert a JLabel and a JtextField by following examples on the net and in books but to no such luck. I have inserted the correct code(i think) to include these components however, my code is running with no errors nor does it display the components. If anyone has any suggestions on where I'm going wrong I would be glad to hear them.
Ian
import java.awt.*; import java.awt.event.*; import javax.swing.*;
public class PayrollSystem implements ItemListener {
JPanel cards, JTextField, textField1, JLabel, label1; final static String HOURLYPANEL = "Hourly"; final static String COMMISSIONPANEL = "Commission"; final static String BASEPLUSPANEL = "BasePlusCommission"; final static String SALARYPANEL = "Salary";
public void addComponentToPane(Container pane) { //Put the JComboBox in a JPanel to get a nicer look. JPanel comboBoxPane = new JPanel(); //use FlowLayout JPanel textFieldPane = new JPanel(); JPanel labelPane = new JPanel();
// JLabel and JTextField JLabel label1 = new JLabel(); labelPane.add(label1); label1.setText("Enter name");
JTextField textField1 = new JTextField(); textFieldPane.add(textField1);
String comboBoxItems[] = { HOURLYPANEL, COMMISSIONPANEL, BASEPLUSPANEL, SALARYPANEL }; JComboBox cb = new JComboBox(comboBoxItems); cb.setEditable(false); cb.addItemListener(this); comboBoxPane.add(cb);
//Create the "cards". JPanel card1 = new JPanel(); card1.add(new JLabel("Hours worked")); card1.add(new JTextField(6)); card1.add(new JLabel("Rate of Pay")); card1.add(new JTextField(8)); card1.add(new JButton("Submit"));
JPanel card2 = new JPanel(); card2.add(new JLabel("Total Sales")); card2.add(new JTextField(8)); card2.add(new JLabel("Commission Rate")); card2.add(new JTextField(8)); card2.add(new JButton("Submit"));
JPanel card3 = new JPanel(); card3.add(new JLabel("Basic Pay")); card3.add(new JTextField(8)); card3.add(new JLabel("Total Sales")); card3.add(new JTextField(8)); card3.add(new JLabel("Commision Rate")); card3.add(new JTextField(8)); card3.add(new JButton("Submit"));
JPanel card4 = new JPanel(); card4.add(new JLabel("Salary")); card4.add(new JTextField(10)); card4.add(new JButton("Submit"));
//Create the panel that contains the "cards". cards= new JPanel(new CardLayout()); cards.add(card1, HOURLYPANEL); cards.add(card2, COMMISSIONPANEL); cards.add(card3, BASEPLUSPANEL); cards.add(card4, SALARYPANEL);
pane.add(comboBoxPane, BorderLayout.PAGE_START); pane.add(cards, BorderLayout.CENTER);
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()); }
/** * GUI created * */ private static void createAndShowGUI() { //Make sure we have nice window decorations. JFrame.setDefaultLookAndFeelDecorated(true);
//Create and set up the window. JFrame frame = new JFrame("Payroll Interface System"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane. PayrollSystem demo = new PayrollSystem(); 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(); } }); } }
IchBin - 13 Mar 2006 23:29 GMT > Hi, > [quoted text clipped - 6 lines] > > Ian [snip code]
What components do not display.. Or which JLabel and JTextField? Looks like all display for me. I'm running JDK 1.6.0
 Signature Thanks in Advance... IchBin, Pocono Lake, Pa, USA http://weconsultants.servebeer.com/JHackerAppManager __________________________________________________________________________
'If there is one, Knowledge is the "Fountain of Youth"' -William E. Taylor, Regular Guy (1952-)
IanH - 13 Mar 2006 23:46 GMT Label1 and TextField1 are not displaying on the GUI.(Hope I am explaining it properly) I'm fairly new to Java. I'm running JDK 1.5. Ian
IchBin - 13 Mar 2006 23:53 GMT > Label1 and TextField1 are not displaying on the GUI.(Hope I am > explaining it properly) I'm fairly new to Java. I'm running JDK 1.5. > Ian Well you define textFieldPane and add textField1. Then define labelPane and add and add textField1 to that panel. The only problem is you do not add textFieldPane or textField1 to pane.
 Signature Thanks in Advance... IchBin, Pocono Lake, Pa, USA http://weconsultants.servebeer.com/JHackerAppManager __________________________________________________________________________
'If there is one, Knowledge is the "Fountain of Youth"' -William E. Taylor, Regular Guy (1952-)
IanH - 14 Mar 2006 00:15 GMT Thanks for that. I have made the following changes put i seem to be getting an error. Is it ok to add the JTextField by doing the following?
JTextField textField1 = new JTextField(); JTextField.add(textField1);
_____________________________________________________________________________ Changes to code.
public void addComponentToPane(Container pane) { //Put the JComboBox in a JPanel to get a nicer look. JPanel comboBoxPane = new JPanel(); //use FlowLayout
JPanel labelPane = new JPanel();
// JLabel and JTextField JLabel label1 = new JLabel();
label1.setText("Enter name"); labelPane.add(label1);
JTextField textField1 = new JTextField(); JTextField.add(textField1);
String comboBoxItems[] = { HOURLYPANEL, COMMISSIONPANEL, BASEPLUSPANEL, SALARYPANEL }; JComboBox cb = new JComboBox(comboBoxItems); cb.setEditable(false); cb.addItemListener(this); comboBoxPane.add(cb);
Rhino - 14 Mar 2006 00:58 GMT > Thanks for that. I have made the following changes put i seem to be > getting an error. Is it ok to add the JTextField by doing the > following? > > JTextField textField1 = new JTextField(); > JTextField.add(textField1); No. You can't add a text field to itself; you have to add it to a container, like a JPanel. You'll need something like this:
JPanel myPanel = new JPanel(); JTextField myTextField = new JTextField(); myPanel.add(myTextField);
-- Rhino
> _____________________________________________________________________________ > Changes to code. [quoted text clipped - 20 lines] > cb.addItemListener(this); > comboBoxPane.add(cb); IanH - 14 Mar 2006 09:25 GMT Ok, the textfield is still not appearing on the GUI. I have also added the following code. Is this what i use to display the textField on the GUI? Ian
pane.add(myPanel, BorderLayout.PAGE_START); pane.add(cards, BorderLayout.CENTER);
Rhino - 14 Mar 2006 15:06 GMT > Ok, the textfield is still not appearing on the GUI. I have also added > the following code. Is this what i use to display the textField on the [quoted text clipped - 3 lines] > pane.add(myPanel, BorderLayout.PAGE_START); > pane.add(cards, BorderLayout.CENTER); Could you post the complete current version of the program, just to make sure that we are looking at the same code?
Also, did you look at the Java Tutorial sections on creating the different components and using the layout managers? If not, you may find your questions answered there. Two other points: - GUI components are typically (but not always) added to the ContentPane of your application with the getContentPane() method in the constructor for your JFrame - the GUI itself won't normally be visible until you do a setVisible() on the JFrame
This tiny fragment illustrates both points:
================================================================
public class PayrollApplication extends JFrame {
public static void main(String[] args) {
PayrollApplication myApplication = new PayrollApplication(); //creates GUI myApplication.pack(); //packs GUI myApplication.setVisible(true); //makes GUI visible }
public PayrollApplication() {
JPanel myPanel = new JPanel(); //creates panel JTextField nameField = new JTextField(); //creates text field myPanel.add(nameField); //adds text field to panel
getContentPane().add(myPanel); //adds panel to ContentPane }
}
================================================================
Naturally, your GUI is more complicated than this one so you'll need to create more components. But your program will normally follow this model.
-- Rhino
IchBin - 14 Mar 2006 18:19 GMT > Hi, > [quoted text clipped - 6 lines] > > Ian [snip code]
> JPanel textFieldPane = new JPanel(); > JPanel labelPane = new JPanel(); [quoted text clipped - 6 lines] > JTextField textField1 = new JTextField(); > textFieldPane.add(textField1); replace above with this:
JPanel userNameAndPasswordPane = new JPanel();
// User Name JLabel and JTextField userNameAndPasswordPane.add(new JLabel("Enter name")); JTextField textField1 = new JTextField(25); userNameAndPasswordPane.add(textField1);
[snip code]
> pane.add(comboBoxPane, BorderLayout.PAGE_START); > pane.add(cards, BorderLayout.CENTER); replace above with this: pane.add(comboBoxPane, BorderLayout.PAGE_START); pane.add(userNameAndPasswordPane, BorderLayout.CENTER); pane.add(cards, BorderLayout.PAGE_END);
[snip code]
Thanks in Advance... IchBin, Pocono Lake, Pa, USA http://weconsultants.servebeer.com/JHackerAppManager __________________________________________________________________________
'If there is one, Knowledge is the "Fountain of Youth"' -William E. Taylor, Regular Guy (1952-)
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|