Results 1 to 12 of 12
- 04-20-2011, 11:34 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 5
- Rep Power
- 0
JFrame and CardLayout -- Wrong Parent
So here is my code. It is three parts, 2 are shown, the last is not relevant:
Java Code://First file import javax.swing.*; import java.awt.event.*; import java.lang.String; import java.lang.Object; import java.util.Formatter; import java.awt.*; public class ToDoItBetterAgain extends JFrame { //Attributes private CardLayout myCard; private MainPanel mainPanel; private AddPanel addPanel; public static void main(String[] args) { new ToDoItBetterAgain(); } ToDoItBetterAgain() { this.setSize(600,400); setVisible(true); //Card Layout myCard = new CardLayout(); setLayout(myCard); //Cards mainPanel = new MainPanel(myCard, this); addPanel = new AddPanel(myCard, this); add(mainPanel, "Main"); add(addPanel, "Add"); } }
Java Code://First Panel import javax.swing.*; import java.awt.*; import java.applet.*; import java.awt.event.*; import java.lang.String; import java.lang.Object; import java.util.Formatter; class MainPanel extends JPanel { private JButton add, apply; private JLabel error, topLabel, bigLabel; //bigLabel - JTextArea? private CardLayout myCard; private JFrame original; public MainPanel(CardLayout myCard, JFrame original) { this.myCard = myCard; this.original = original; //North - topLabel JPanel north = new JPanel(); LabelFormat big = new LabelFormat("Name","Date","Time","Remove","Delete"); topLabel = new JLabel(big.getActualString()); topLabel.setFont(new Font("Times New Roman", Font.PLAIN, 18)); topLabel.setForeground(Color.BLUE); north.add(topLabel); //Center - bigLabel bigLabel = new JLabel(""); bigLabel.setVerticalAlignment(SwingConstants.TOP); bigLabel.setHorizontalAlignment(SwingConstants.LEFT); bigLabel.setFont(new Font("Times New Roman", Font.PLAIN, 16)); bigLabel.setText("No Events"); //East - Check Boxes //South - Error label, Add/Apply buttons JPanel south = new JPanel(); //Buttons JPanel subsouth1 = new JPanel(); add = new JButton("ADD"); apply = new JButton("APPLY"); subsouth1.setLayout(new FlowLayout(FlowLayout.RIGHT)); subsouth1.add(add); subsouth1.add(apply); //Label JPanel subsouth2 = new JPanel(); error = new JLabel(""); error.setForeground(Color.RED); subsouth2.setLayout(new FlowLayout(FlowLayout.LEFT)); subsouth2.add(error); south.setLayout(new GridLayout(1,2)); south.add(subsouth2); south.add(subsouth1); //Put it all together setLayout(new BorderLayout()); add(north, BorderLayout.NORTH); add(bigLabel, BorderLayout.CENTER); add(south, BorderLayout.SOUTH); //Button Listeners ButtonListener listener = new ButtonListener(); apply.addActionListener(listener); add.addActionListener(listener); } private class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent event) { if(event.getSource() == add) { myCard.last(original); } else if(event.getSource() == apply) { } else { error.setText("Error"); } } } }
The files compile correctly. The file opens and MainPanel loads properly. However, when I hit the "Add" button I get this error:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: wrong parent for CardLayout
at java.awt.CardLayout.checkLayout(CardLayout.java:38 4)
at java.awt.CardLayout.last(CardLayout.java:479)
at MainPanel$ButtonListener.actionPerformed(MainPanel .java:82)
at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.jav a:6289)
at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
at java.awt.Component.processEvent(Component.java:605 4)
at java.awt.Container.processEvent(Container.java:204 1)
at java.awt.Component.dispatchEventImpl(Component.jav a:4652)
at java.awt.Container.dispatchEventImpl(Container.jav a:2099)
at java.awt.Component.dispatchEvent(Component.java:44 82)
at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4577)
at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4168)
at java.awt.Container.dispatchEventImpl(Container.jav a:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2478 )
at java.awt.Component.dispatchEvent(Component.java:44 82)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:644)
at java.awt.EventQueue.access$000(EventQueue.java:85)
at java.awt.EventQueue$1.run(EventQueue.java:603)
at java.awt.EventQueue$1.run(EventQueue.java:601)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$2.run(EventQueue.java:617)
at java.awt.EventQueue$2.run(EventQueue.java:615)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 614)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)
So I'm guessing I am calling the wrong parent, but I don't know who is the real parent is. I'm guessing this is just a rookie mistake, and hoping.
ThanksLast edited by Fubarable; 04-21-2011 at 05:13 AM. Reason: Mod Edit: code tags added
- 04-20-2011, 11:49 PM #2
You pass the CardLayout to the MainPanel class but you do not use it.
I'm no Swing expert but I'm not sure you can use the same layout manager for every panel and frame you have.
- 04-21-2011, 03:37 AM #3
Member
- Join Date
- Apr 2011
- Posts
- 5
- Rep Power
- 0
@Junky,
Thanks for the quick reply. The CardLayout gets passed to MainPanel via the Constructor: public MainPanel(CardLayout myCard, JFrame original). Also, I have nearly identical code for a JApplet version and it works there... No wrong parent issue.
- 04-21-2011, 03:41 AM #4
- 04-21-2011, 04:04 AM #5
The exception is being thrown at this line inside ButtonListener:
What's weird is that you properly call setLayout(myCard) in the ToDoItBetterAgain class. So it is supposed to be the correct parent......Java Code:myCard.last(original);
EDIT: @Junky, yes he does use it: inside ButtonListener
EDIT2: @AutoRobin, please use [ code ] code goes here [ /code ] tags next time (without the spaces) so it would be easier to read your code.Last edited by ra4king; 04-21-2011 at 04:09 AM.
- 04-21-2011, 04:10 AM #6
- 04-21-2011, 04:23 AM #7
- 04-21-2011, 04:33 AM #8
So? It doesn't change the fact that the JPanel's layout manager IS NOT the CardLayout.
- 04-21-2011, 04:37 AM #9
If you look closer at that 1 line:
"original" is referencing the JFrame that has a layout of the instance referenced by "myCard".Java Code:myCard.last(original);
-
Moderator Edit: Code tags added to the post above.
To the OP, in the future, to do this yourself, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.
Another way to do this is to manually place the tags into your code by placing the tag [code] above your pasted code and the tag [/code] below your pasted code like so:
Best of luckJava Code:[code] // your code goes here // notice how the top and bottom tags are different [/code]
________________________________________________
Regarding your problem, I think that the issue is that the layout is not added to the JFrame but rather to its contentPane. Myself, I'd give the main class that has the CardLayout a public method, say
And I'd have my sub panels call this method passing in the appropriate String. I'd make the cardName Strings public constants in the main class, so there's less risk of a spelling or capitalization mistake, and I'd have my sub panel classes use these constants when calling this method.Java Code:public void swapCard(String cardName) { myCard.show(getContentPane(), cardName); }
Best of luck
Best of luck
- 04-21-2011, 12:23 PM #11
@Fubarable
Good point! He should probably call "myCard.last(original.getContentPane())"!
- 04-21-2011, 12:39 PM #12
Member
- Join Date
- Apr 2011
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
Question about cardlayout
By scheffetz in forum New To JavaReplies: 1Last Post: 04-02-2011, 08:05 PM -
CardLayout manager
By abetemari in forum New To JavaReplies: 4Last Post: 03-27-2011, 01:46 AM -
Using a FlowLayout on top of a CardLayout
By snieuw in forum New To JavaReplies: 1Last Post: 11-08-2010, 06:03 PM -
Help with CardLayout
By Kyle227 in forum New To JavaReplies: 4Last Post: 05-28-2010, 01:03 AM -
Regarding CardLayout
By adeeb in forum AWT / SwingReplies: 1Last Post: 06-07-2008, 07:52 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks