1

I would like to create a "wizard" on a JDialog using the CardLayout, triggered by user pressing the New button from the menubar. In Netbeans I have created a JDialog through which I have a series of jPanels in CardLayout format. In my "New" menu item I wrote the following code to initiate the jDialog as follows,

 CardLayout cl = (CardLayout) jDialogNew.getLayout();
 cl.preferredLayoutSize(jDialogNew);
 cl.show(jDialogNew, "card1");

However, the compiler comes up with the following error,

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException:
    java.awt.BorderLayout cannot be cast to java.awt.CardLayout

If anyone is out there that can take me through creating a wizard on "Netbeans" I'd be eternally grateful

kleopatra
  • 51,061
  • 28
  • 99
  • 211
Sam
  • 2,702
  • 5
  • 31
  • 45
  • This [example](http://stackoverflow.com/questions/5654926/implementing-back-forward-buttons-in-swing/5655843#5655843) might make a good start on your [sscce](http://sscce.org/). – trashgod Oct 14 '11 at 02:28

3 Answers3

2

Your jDialogNew has a BorderLayout set as its layout and and not a CardLayout, meaning that when you call getLayout() to try to fit it into a variable that cant hold a BorderLayout an exception is thrown. The classes are different so you cannot cast from one to another, causing a ClassCastException.

A possible solution to this is to set your own layout for the jDialogNew. I dont have code infront of me so I cant check myself, but try looking for a method like setLayout(), and pass in a new layout of your choice.

Numeron
  • 8,723
  • 3
  • 21
  • 46
  • Thanks Numeron for your kind answer! You see, I want to create a wizard dialog using a few JPanel under the JDialog. I need the user to be able to browse through these JPanels through a series of next/previous buttons. This operation is vitally important for me at this stage and I need to get it done asap. Your kind assitance in resolving this issue is very much appreciated! – Sam Oct 14 '11 at 04:08
1

you can do with following

create JFrame -> Add "CARD LAYOUT"

add JPanels to project. Design JPanels. Customize init code of JFrame. Insert JPanels with this.add(jpanel name). for all jpanels setVisible(false) - then setVisible true which jpanel you want to start with.

Toon Krijthe
  • 52,876
  • 38
  • 145
  • 202
nick
  • 11
  • 1
0

The way I did it in Netbeans was very easy! All I had to do was to was to introduce a separate JFrame in my resources package (being a part of my overall package) and in that JFrame I created a JPanel with the CardLayout, under which I created all my other JPanels relating to that top JPanel. Now having the JFrame I could set my fixed canvas plus everything else I needed to construct and activate my CardLayout "Wizard" dialogue box! Then I had to call the new JFrame from with my application whenever the event was triggered. It made life a whole lot easier and it works just great!

Sam
  • 2,702
  • 5
  • 31
  • 45