Results 1 to 6 of 6
Thread: Card Layout with Buttons
- 01-12-2012, 10:40 PM #1
Member
- Join Date
- Dec 2011
- Posts
- 10
- Rep Power
- 0
Card Layout with Buttons
I've been looking at Card Layout, and I was curious about how I could use buttons with it.
The GUI I have currently navigates between the two cards with a "previous" and "next" button. However, I would like to know if I can call the card I want to appear by name, I know my commented code is incorrect. I looked at the API, and the addLayoutComponent(String name, Component comp) method is deprecated, but it's been replaced by another addLayoutComponent method. If this is part of the solution, I don't understand how I will be able to call my cards by name later.
Or, would it be better to just use ItemListener and a combo box?
Here is the code:
Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; public class CardsDemo { public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } private static void createAndShowGUI() { JFrame frame = new JFrame("CardLayoutDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); CardLayoutDemo demo = new CardLayoutDemo(); demo.addComponentToPane(frame.getContentPane()); frame.pack(); frame.setVisible(true); } } class CardLayoutDemo implements ActionListener { JPanel cards; final static String BUTTONPANEL = "Card with JButtons"; final static String TEXTPANEL = "Card with JTextField"; public void addComponentToPane(Container pane) { JPanel selection = new JPanel(); JButton first = new JButton("Previous"); JButton second = new JButton("Next"); first.addActionListener(this); second.addActionListener(this); first.setActionCommand("prev"); second.setActionCommand("next"); selection.add(first); selection.add(second); JPanel card1 = new JPanel(); card1.add(new JButton("Button 1")); card1.add(new JButton("Button 2")); card1.add(new JButton("Button 3")); JPanel card2 = new JPanel(); card2.add(new JTextField("TextField", 20)); cards = new JPanel(new CardLayout()); cards.add(card1, BUTTONPANEL); cards.add(card2, TEXTPANEL); pane.add(selection, BorderLayout.PAGE_START); pane.add(cards, BorderLayout.CENTER); } public void actionPerformed(ActionEvent evt) { CardLayout cl = (CardLayout)(cards.getLayout()); if (evt.getActionCommand().equals("prev")) { cl.previous(cards); } if (evt.getActionCommand().equals("next")) { cl.next(cards); } // //cl.show(cards, evt.getActionCommand()); // } }
- 01-12-2012, 11:12 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Re: Card Layout with Buttons
That is the approach taken in the How to Use Card Layout page of Oracle's Tutorial.However, I would like to know if I can call the card I want to appear by name
- 01-12-2012, 11:14 PM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Re: Card Layout with Buttons
Moved to AWT/Swing forum.
-
Re: Card Layout with Buttons
For example, if you add to your code above:
Java Code:class CardLayoutDemo implements ActionListener { JPanel cards; final static String BUTTONPANEL = "Card with JButtons"; final static String TEXTPANEL = "Card with JTextField"; private static final String[] COMBO_STRINGS = {BUTTONPANEL, TEXTPANEL}; public void addComponentToPane(Container pane) { JPanel selection = new JPanel(); JButton first = new JButton("Previous"); JButton second = new JButton("Next"); first.addActionListener(this); second.addActionListener(this); first.setActionCommand("prev"); second.setActionCommand("next"); selection.add(first); selection.add(second); final JComboBox combo = new JComboBox(COMBO_STRINGS); selection.add(combo); combo.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent evt) { CardLayout cl = (CardLayout) cards.getLayout(); cl.show(cards, combo.getSelectedItem().toString()); } });
- 01-13-2012, 01:51 AM #5
Member
- Join Date
- Dec 2011
- Posts
- 10
- Rep Power
- 0
Re: Card Layout with Buttons
Thank you for the post, I've never seen a combo box implemented like that, so it's good to know.
I guess that trying to use the show method with JButtons is not ideal.
Thanks for the help.
-
Re: Card Layout with Buttons
Similar Threads
-
Image in Card Layout
By poorbrain in forum Java AppletsReplies: 4Last Post: 03-08-2011, 06:15 PM -
Question Card Layout, Card Management
By lrichil in forum AWT / SwingReplies: 1Last Post: 04-22-2010, 10:11 AM -
Edit layout Layout please help me
By manhtungtnk28@gmail.com in forum New To JavaReplies: 4Last Post: 11-23-2009, 08:41 AM -
Card
By hedonist in forum New To JavaReplies: 3Last Post: 08-13-2009, 02:20 PM -
Card Layout
By Gilbee in forum NetBeansReplies: 3Last Post: 03-03-2009, 09:37 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks