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 06-07-2008, 07:43 PM
Member
 
Join Date: May 2008
Posts: 29
adeeb is on a distinguished road
Regarding CardLayout
Hi
I know that in card layout we can add different cards and switch between them.
Now i have add buttons as first, last, next and previous. And i am able to do so by using this buttons.Now what i need is to get a card directly on clicking a button say xyz. Imagine you have 15 cards and you need to get the 7th card on clicking the xyz button how to do it. Please some body help me.
The code i used for first and last button is as follows for card layout with 4 cards.


This is for first button

firstb.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
cl.first(panel);
currcard=1;
}
});

This is for last button

firstb.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
cl.last(panel);
currcard=4;
}
});
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 06-07-2008, 08:52 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
Code:
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class CardLayoutTest implements ActionListener, ListSelectionListener { JPanel cardPanel; public void actionPerformed(ActionEvent e) { String ac = e.getActionCommand(); CardLayout cards = (CardLayout)cardPanel.getLayout(); if(ac.equals("FIRST")) cards.first(cardPanel); if(ac.equals("PREVIOUS")) cards.previous(cardPanel); if(ac.equals("NEXT")) cards.next(cardPanel); if(ac.equals("LAST")) cards.last(cardPanel); } public void valueChanged(ListSelectionEvent e) { if(!e.getValueIsAdjusting()) { String id = (String)((JList)e.getSource()).getSelectedValue(); CardLayout cards = (CardLayout)cardPanel.getLayout(); cards.show(cardPanel, id); } } private JPanel getCardContent() { CardLayout cards = new CardLayout(); cardPanel = new JPanel(cards); String[] ids = { "red", "green", "blue", "yellow", "pink" }; Color[] colors = { Color.red, Color.green, Color.blue, Color.yellow, Color.pink }; for(int i = 0; i < ids.length; i++) { cardPanel.add(ids[i], getPanel(ids[i], colors[i])); } return cardPanel; } private JList getCardList() { String[] ids = { "red", "green", "blue", "yellow", "pink" }; JList list = new JList(ids); list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); list.addListSelectionListener(this); return list; } private JPanel getPanel(String id, Color color) { JLabel label = new JLabel(id, JLabel.CENTER); label.setFont(label.getFont().deriveFont(18f)); JPanel panel = new JPanel(new BorderLayout()); panel.add(label); panel.setBackground(color); return panel; } private Box getButtonPanel() { Box box = Box.createHorizontalBox(); box.add(Box.createHorizontalGlue()); String[] ids = { "first", "previous", "next", "last" }; for(int i = 0; i < ids.length; i++) { JButton button = new JButton(ids[i]); button.setActionCommand(ids[i].toUpperCase()); button.addActionListener(this); box.add(button); box.add(Box.createHorizontalGlue()); } return box; } public static void main(String[] args) { CardLayoutTest test = new CardLayoutTest(); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(test.getCardContent()); f.add(test.getCardList(), "Before"); f.add(test.getButtonPanel(), "Last"); f.setSize(400,400); f.setLocation(200,200); f.setVisible(true); } }
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
Using previous with CardLayout uncopywritable New To Java 2 08-05-2007 10:43 PM


All times are GMT +3. The time now is 04:37 AM.


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