• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exception in thread "main"

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm creating an application with 8 cards, using card layout manager and panels.
Here is the code:

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

public class Cineplex extends JFrame
{
public CardLayout cardManager;
//switch from one card to another
JButton button1, button2, button3, button4, button5, button6, button7, button8, button9, button10;
JLabel label1, label2, label3, label4, label5, label6, label7, label8, label9, label10, label11;
JTextPane textPane1, textPane2;
JPasswordField passwordField;
JTextField textFieldTxt;

public Cineplex()
{
super("Blackrock_Cineplex");
setSize(700,500);
Container c = this.getContentPane();

//use cardlayout
cardManager = new CardLayout(20,10);
setLayout(cardManager);

//set up the ActionListener
ButtonListener myListener = new ButtonListener();

//the first card
Panel card1 = new Panel();
card1.setBackground(Color.white);

label1 = new JLabel("Please select one of the follwing options:");
card1.add(label1);

button1 = new JButton("Administrator");
card1.add(button1);
button1.addActionListener(myListener);

button2 = new JButton("User");
card1.add(button2);
button2.addActionListener(myListener);

add("Card1", card1);

//the second card
Panel card2 = new Panel();
card2.setBackground(Color.white);

label2 = new JLabel("Please select a movie from thelist below:");
card2.add(label2);

String[] movieStrings = {"Brokeback Mountain", "Chicken Little", "Anchor Man", "Kill Bill"};
JComboBox movieList = new JComboBox(movieStrings);
movieList.setSelectedIndex(1);
movieList.addActionListener(myListener);
card2.add(movieList);

label3 = new JLabel("Number of tickets:");
card2.add(label3);

textPane1 = new JTextPane();
textPane1.setText("");
card2.add(textPane1);

button3 = new JButton("Proceed");
card2.add(button3);
button3.addActionListener(myListener);

button4 = new JButton("Cancel");
card2.add(button4);
button4.addActionListener(myListener);

add("Card2", card2);

//the third card
Panel card3 = new Panel();
card3.setBackground(Color.white);

label4 = new JLabel("Please select your preferred method of payment:");
card3.add(label4);

button5 = new JButton("Credit Card");
card3.add(button5);
button5.addActionListener(myListener);

button6 = new JButton("Cash");
card3.add(button6);
button6.addActionListener(myListener);

add("Card3", card3);

//the fourth card
Panel card4 = new Panel();
card4.setBackground(Color.white);

label5 = new JLabel("Please insert your credit card and press the OK button:");
card4.add(label5);

button7 = new JButton("OK");
card4.add(button7);
button7.addActionListener(myListener);

add("Card4", card4);

//the fifth card
Panel card5 = new Panel();
card5.setBackground(Color.white);

label6 = new JLabel("Please insert your money into the slot provided, and press OK:");
card5.add(label6);

button8 = new JButton("OK");
card5.add(button8);
button8.addActionListener(myListener);

add("Card5", card5);

//the sixth card
Panel card6 = new Panel();
card6.setBackground(Color.white);

label7 = new JLabel("Thank you!");
card6.add(label7);

label8 = new JLabel("You have just bought a ticket for the movies.Enjoy!");
card6.add(label8);

add("Card6", card6);

//the seventh card
Panel card7 = new Panel();
card7.setBackground(Color.white);

passwordField = new JPasswordField(5);
passwordField.setEchoChar('#');
label9 = new JLabel("Enter password: ");
label9.setLabelFor(passwordField);
card7.add(label9);

button9 = new JButton("LogIn");
card7.add(button9);
button9.addActionListener(myListener);

add("Card7", card7);

//the eighth card
Panel card8 = new Panel();
card8.setBackground(Color.white);

button10 = new JButton("Save Details");
card8.add(button10);
button10.addActionListener(myListener);

label10 = new JLabel("Enter name of new movie:");
card8.add(label10);

label11 = new JLabel("Movie Title");
card8.add(label11);
textFieldTxt = new JTextField("20");
card8.add(textFieldTxt);

add("Card8", card8);

//tell the CardLayout to go to the first card of "this", i.e., the frame
cardManager.first(this);

setVisible(true);
}

public static void main(String args[])
{
Cineplex app = new Cineplex();

}

protected class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
Object src = evt.getSource();
if(src == button1)
cardManager.next(Cineplex.this);
//cardManager.show(Cineplex.this, "Card7");
else if(src == button2)
cardManager.show(Cineplex.this, "Card2");
else if(src == button3)
cardManager.show(Cineplex.this, "Card3");
else if(src == button4)
cardManager.show(Cineplex.this, "Card1");
else if(src == button5)
cardManager.show(Cineplex.this, "Card4");
else if(src == button6)
cardManager.show(Cineplex.this, "Card5");
else if(src == button7)
cardManager.show(Cineplex.this, "Card6");
else if(src == button8)
cardManager.show(Cineplex.this, "Card6");
else if(src == button9)
cardManager.show(Cineplex.this, "Card8");
}//end actionPerformed
}
}//end class


My problem is that when i run this code it throws up the following error and i really don't know how to fix it:

Exception in thread �main� java.lang.IllegalArgumentException: wrong parent for CardLayout
at java.awt.CardLayout.checkLayout(Unknown Source)
at java.awt.CardLayout.first(Unknown Source)
at Cineplex.<init>(Cineplex.java:170)
at Cineplex.main(Cineplex.java:177)

Any help would be great.
Thanks



 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JFrame.setLayout() is just shorthand for JFrame.getContentPane().setLayout(), so I believe you need to say

cardManager.first(getContentPane())

rather than

cardManager.first(this)

Even though the error message has the word "thread' in it, this has nothing to do with Threads -- it's a Swing question, so I'm going to move it to Swing/AWT/SWT/JFace for further discussion.
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, you need to set your Container(c)'s layout to be CardLayout.

And add your cards to c

instead of

Use CardLayout's show method and pass the Container as the first arg, the name of the card you want to show as the second.

Good Luck!
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ben Zung:
Also, you need to set your Container(c)'s layout to be CardLayout.



No, actually. In Tiger, JFrame.add() delegates to getContentPane().add(), and JFrame.setLayout() delegates to getContentPane().setLayout(), so this is just fine in that regard. If she's not using Tiger, then this will complain about the add() calls long before the NPE is thrown.
 
Let's get him boys! We'll make him read this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic