Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-05-2007, 07:37 PM
Member
 
Join Date: Jul 2007
Posts: 13
uncopywritable is on a distinguished road
Using previous with CardLayout
I'm still having trouble with CardLayout. I want to make a basic wizard GUI, but I can't make actionListeners deliver any of the special CardLayout methods ie previous, first, next, show. Here's the code. Is there someone patient out there can tell me where I'm going wrong?

Code:
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class CardLayoutDemo { JPanel cards; //a panel that uses CardLayout final static String BUTTONPANEL = "Card with JButtons"; final static String TEXTPANEL = "Card with JTextField"; public void addComponentToPane(Container pane) { //Create the "cards". JPanel card1 = new JPanel(); JButton Back = new JButton("Back"); JButton Next = new JButton("Next"); JButton Cancel = new JButton("Cancel"); card1.add(Back); card1.add(Next); card1.add(Cancel); JPanel card2 = new JPanel(); card2.add(new JTextField("TextField", 20)); //Create the panel that contains the "cards". cards = new JPanel(new CardLayout()); cards.add(card1, BUTTONPANEL); cards.add(card2, TEXTPANEL); pane.add(cards, BorderLayout.CENTER); } /** * Create the GUI and show it. For thread safety, * this method should be invoked from the * event dispatch thread. */ private static void createAndShowGUI() { //Create and set up the window. JFrame frame = new JFrame("CardLayoutDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Create and set up the content pane. CardLayoutDemo demo = new CardLayoutDemo(); demo.addComponentToPane(frame.getContentPane()); //Display the window. frame.pack(); frame.setVisible(true); } public void actionPerformed(ActionEvent e){ if (e.getActionCommand().equals("Back")){ previous(cards); } if (e.getActionCommand().equals("Next")){ //action code } if (e.getActionCommand().equals("Cancel")){ //action code } } public static void main(String[] args) { /* Use an appropriate Look and Feel */ try { //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel"); } catch (UnsupportedLookAndFeelException ex) { ex.printStackTrace(); } catch (IllegalAccessException ex) { ex.printStackTrace(); } catch (InstantiationException ex) { ex.printStackTrace(); } catch (ClassNotFoundException ex) { ex.printStackTrace(); } /* Turn off metal's use of bold fonts */ UIManager.put("swing.boldMetal", Boolean.FALSE); //Schedule a job for the event dispatch thread: //creating and showing this application's GUI. javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-05-2007, 10:33 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,015
hardwired is on a distinguished road
Code:
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class CLD implements ActionListener { JPanel cards; //a panel that uses CardLayout final static String BUTTONPANEL = "Card with JButtons"; final static String TEXTPANEL = "Card with JTextField"; public void addComponentToPane(Container pane) { // Create control panel. JButton Back = new JButton("Back"); Back.setActionCommand("Back"); JButton Next = new JButton("Next"); Next.setActionCommand("Next"); JButton Cancel = new JButton("Cancel"); Cancel.setActionCommand("Cancel"); // Add listeners. Back.addActionListener(this); Next.addActionListener(this); Cancel.addActionListener(this); JPanel north = new JPanel(); north.add(Back); north.add(Next); north.add(Cancel); // Create the "cards". JPanel card1 = new JPanel(new BorderLayout()); card1.setBackground(Color.red); card1.add(new JLabel("Card 1", JLabel.CENTER)); JPanel card2 = new JPanel(); card2.add(new JTextField("TextField", 20)); JPanel card3 = new JPanel(new BorderLayout()); card3.setBackground(Color.yellow); card3.add(new JLabel("Card 3", JLabel.CENTER)); // Instantiate the panel that contains the "cards". cards = new JPanel(new CardLayout()); cards.add("Card 1", card1); cards.add(TEXTPANEL, card2); cards.add("yellow", card3); // Layout components in frame. pane.add(north, BorderLayout.PAGE_START); pane.add(cards, BorderLayout.CENTER); } /** * Create the GUI and show it. For thread safety, * this method should be invoked from the * event dispatch thread. */ private static void createAndShowGUI() { //Create and set up the window. JFrame frame = new JFrame("CardLayoutDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Create and set up the content pane. CLD demo = new CLD(); demo.addComponentToPane(frame.getContentPane()); //Display the window. frame.pack(); frame.setVisible(true); } public void actionPerformed(ActionEvent e){ // Get a reference to the CardLayout. CardLayout cardLayout = (CardLayout)cards.getLayout(); if (e.getActionCommand().equals("Back")){ cardLayout.previous(cards); } if (e.getActionCommand().equals("Next")){ cardLayout.next(cards); } if (e.getActionCommand().equals("Cancel")){ //action code } } public static void main(String[] args) { /* Use an appropriate Look and Feel */ try { //UIManager.setLookAndFeel( // "com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel"); } catch (UnsupportedLookAndFeelException ex) { ex.printStackTrace(); } catch (IllegalAccessException ex) { ex.printStackTrace(); } catch (InstantiationException ex) { ex.printStackTrace(); } catch (ClassNotFoundException ex) { ex.printStackTrace(); } /* Turn off metal's use of bold fonts */ UIManager.put("swing.boldMetal", Boolean.FALSE); //Schedule a job for the event dispatch thread: //creating and showing this application's GUI. javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-05-2007, 10:43 PM
Member
 
Join Date: Jul 2007
Posts: 13
uncopywritable is on a distinguished road
That's brilliant! thanks
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Preventing inserted text from becoming colored from previous style jkhoa AWT / Swing 2 08-10-2007 01:36 AM


All times are GMT +3. The time now is 03:15 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org