0
getContentPane().setLayout(new CardLayout());
CardLayout x = (CardLayout) getLayout();

I'm using Netbeans and in Design manager I chosed my JFrame contents as CardLayout. However I cannot make it work so far. I set the above code after the initComponenets call and still I have this error:

java.awt.BorderLayout cannot be cast to java.awt.CardLayout

Somehow I have BorderLayout instead of CardLayout. How is it possible? I've just set layout as CardLayout. PS: my class extends javax.swing.JFrame.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Halil
  • 2,076
  • 1
  • 22
  • 30
  • 1) I typically find it easier and cleaner to either add a single `JPanel` to the content pane, or set it ***as*** the content pane. Then anything to do with borders, layout, color can be done to the `JPanel`. 2) *"my class extends `javax.swing.JFrame`"* Don't extend frame unless adding or overriding methods. Just keep a reference to one. – Andrew Thompson Dec 14 '12 at 03:09
  • I'm a netbeans and java newby so I don't really know about borders and stuff. I used netbeans generated code from Swing GUI Forms>Application Sample Form. Then added 2 panels and CardLayout Layout. When I try to switch, I get these errors. I'd appreciate if there is any easier way to get around this. – Halil Dec 14 '12 at 03:11
  • See [this answer](http://stackoverflow.com/questions/5665156/calling-awt-frame-methods-from-subclass/5786005#5786005) for a working `CardLayout`. I think you should put Netbeans to the side for the moment and learn how to code Java. – Andrew Thompson Dec 14 '12 at 03:12
  • I know if I code JPanel and frame by hand, it works. I've just swtiched to Netbeans from Eclipse because it has native GUI manager and spent a lot of hours to switch from SWT to Swing. Unfortunately I don't have time to write write GUI by code since I have to focus on back-end of the project. I just need a quick fix for Netbenas that I can continue coding the backend. – Halil Dec 14 '12 at 03:17
  • *"I just need a quick fix for Netbenas"* Hire someone. Voting to close as 'too localized'. – Andrew Thompson Dec 14 '12 at 03:22
  • @AndrewThompson, no you got me wrong. I'm searching for a fix that I use it in Netbeans. I already search web for hours. This is the last place for me to ask. – Halil Dec 14 '12 at 03:24

1 Answers1

2

You can use:

CardLayout x = (CardLayout) getContentPane().getLayout();

Similarly to switch card panels you can use:

x.show(getContentPane(), "card2");

This is not an issue if you simply use a separate JPanel as the 'card' container plus you get the added benefit of using the JFrame BorderLayout should you wish to add navigation buttons, say in the BorderLayout.SOUTH location.

Essential reading for using CardLayout

Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • Thanks for quick answer! Then I get this error: `java.lang.IllegalArgumentException: wrong parent for CardLayout`. I've just tried that with brand new project. – Halil Dec 14 '12 at 03:08
  • I'm guessing you are using the wrong container for `CardLayout.show` – Reimeus Dec 14 '12 at 03:19
  • I've been tring to do it by "next" and I gave layout's name as the arguement. It seemed like code failed at assignment, not at "next" statement. show with string name worked. BTW: I read this page as well but apparently I didn't paid enough attention. Thanks a lot! – Halil Dec 14 '12 at 03:23