CardLayout

In CardLayout the first card shown when the JPanel(with Cardlayout)
is made visible is the card that was first added to the layout.
Is it possible to change this to a different card (for example, by calling
the show(Container parent, String name) method - before the JFrame container
is made visible?

rrrayc wrote:
Is it possible to change this to a different card (for example, by calling the show(Container parent, String name) method - before the JFrame container is made visible?I would assume that would work just fine...

Similar Messages

  • Using CardLayout and Null Layout

    I used search on the forums and found one person that asked the exact question I'm needing answered. But, he didn't get any responses either.
    I have a JFrame(myJFrame) which has a JPanel(myJpanel). myJpanel uses a CardLayout. I then have another class that extends JPanel(TestJpanel). TestJpanel uses a null layout. I am not able to get the TestJpanel to show correctly with the show(). method. However, if I change the layout of TestJpanel to anything else, presto it works. Has anyone uses null panels as the panels for a cardlayout panel?

    if you do setLayout(null) the component will leave all layout up to you. You can then use setSize(), setLocation(), and setBounds() on all children to position them however you'd like

  • CardLayout does not work

    I'm building a dynamic UI which has "x" number of labels and "x" no of fields. I'm able build the UI.
    When I make a selection in the ComboBox the UI(labels and fields change). For this I'm using a cardlayout.
    It does not seem to work.
    AttributesPanel builds the UI(Labels, Fields)
    import java.util.*;
    import java.util.List;
    import java.awt.event.*;
    import java.awt.*;
    import javax.swing.*;
    public class AttributesPanel extends JPanel {
        private JTextField[] fields;
        private List attributes;
        public AttributesPanel(String templateName) throws Exception{
            JSeparator sep = new JSeparator();
            JButton okButton = new JButton("OK");
            JButton cancelButton = new JButton("Cancel");
            com.insignia.profileManagement.dataLoader.NewProfileMetaDataLoader loader = new com.insignia.profileManagement.dataLoader.NewProfileMetaDataLoader();
            attributes = loader.BuildAttributes(templateName);
            for(int i = 0; i< attributes.size(); i ++) {
              String attributename = (String)attributes.get(i);
              System.out.println("Names of the attribute: " +attributename);
            fields = new JTextField[attributes.size()];
            JLabel[] labels = new JLabel[attributes.size()];
            for(int i = 0; i< attributes.size(); i++) {
                labels[i] = new JLabel((String)attributes.get(i));
                fields[i] = new JTextField(15);
            GridBagLayout gb1 = new GridBagLayout();
            GridBagConstraints gbc = new GridBagConstraints();
            setLayout(gb1);
            gbc.anchor = GridBagConstraints.NORTHWEST;
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            add(new JLabel("Assign Profile Template"), gbc);
            gbc.anchor = GridBagConstraints.NORTH;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.insets = new Insets(0,0,10,0);
            add(sep, gbc);
            gbc.anchor = GridBagConstraints.WEST;
            gbc.insets = new Insets(0,0,0,0);
            for(int i = 0;i < attributes.size(); i ++){
                gbc.gridwidth = 1;
                add(labels[i] ,gbc);
                add(Box.createHorizontalStrut(10));
                gbc.gridwidth = GridBagConstraints.REMAINDER;
                add(fields,gbc);
    JPanel buttonPanel = new JPanel();
    buttonPanel.add(okButton);
    buttonPanel.add(cancelButton);
    gbc.anchor = GridBagConstraints.SOUTH;
    gbc.insets = new Insets(15,0,0,0);
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridwidth = 7;
    add(buttonPanel,gbc);
    2nd class which calls AttributesPanel
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    import java.lang.*;
    import java.util.*;
    import java.util.List;
    public class ProfileUI extends JPanel implements ActionListener{
    private JPanel templatePanel;
    JComboBox comboBox = new JComboBox();
    private CardLayout cardLayout;
    com.insignia.profileManagement.dataLoader.NewProfileMetaDataLoader loader = new com.insignia.profileManagement.dataLoader.NewProfileMetaDataLoader();
    public ProfileUI() throws Exception {
    templatePanel = new JPanel();
    List templates = loader.BuildTemplates();
    comboBox.addActionListener(this);
    for(int i = 0; i< templates.size(); i ++) {
    comboBox.addItem(templates.get(i));
    //attributes = loader.BuildAttributes((String)templates.get(i));
    templatePanel.add(comboBox);
    List attributes = loader.BuildAttributes("Email Template");
    // //creating no of card layouts.
    cardLayout = new CardLayout();
    JPanel mainCardPanel = new JPanel(cardLayout);
    AttributesPanel[] cardPanels = new AttributesPanel[attributes.size()];
    for (int i = 0; i < attributes.size(); i++) {
    mainCardPanel.add(cardPanels[i], i);
    // createCardPanel(cardPanels[i], (String)comboBox.getItemAt(i));
    cardLayout.show(cardPanels[i], "" + i);
    templatePanel.add(mainCardPanel);
    //updateLabel(0);
    cardLayout.show(cardPanels[0], "" + 0);
    public void actionPerformed(ActionEvent e){
    if(e.getSource() == comboBox) {
    public static void main (String args []) throws Exception {
    JFrame frame = new JFrame("ProfileUI");
    JComponent newContentPane= new ProfileUI();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // use this or a
    newContentPane.setOpaque(true); //content panes must be opaque
    frame.setContentPane(newContentPane);
    frame.setSize(400,390);
    frame.setVisible(true);
    //frame.setResizable(false);
    thnx

    Personally, I've had numerous issues with CardLayout -- I'd suggest you try CardPanel instead:
    http://java.sun.com/products/jfc/tsc/articles/cardpanel/
    - Fromage

  • Problem with JPanel, CardLayout and Applets

    Hello All,
    I have a JPanel with a CardLayout as layout manager. In this JPanel i'm gonna show a few applets.
    My problem is when i'm showing an applet for second time and try to click a button it does nothing... I think the action method is not called again...
    How can i solve this?
    Thanks in advance ; )

    Here is a few parts of my code...
    public class InitAll{
    JPanel cards = new JPanel( new CardLayout() );
    JComboBox selector = new JComboBox();
    Object objetosV[][] ={
         {"Ventas",venta.class},
         {"Compras",compras.class}, //These are applets.
         {"Traspasos",traspasos.class},
         {"Facturas",factura.class},
    public void load( Applet app ){
    cards.removeAll();
    selector.removeAll();
    cards.add( "Entrada", app );
    selector.addItem( "Entrada" );
    for ( int i=0; i < objetosV.length; i++ ){
         selector.addItem( (String)objetosV[0] );
         Applet temp=createPanel( ( Class ) objetosV[i][1] );
         temp.setSize( 800,750 );
         cards.add( ( String ) objetosV[i][0], temp );
    public void itemStateChanged(ItemEvent e) {
    ((CardLayout)cards.getLayout()).show(cards, selector.getSelectedItem().toString() );
    Applet tmp = (Applet)cards.getComponent(selector.getSelectedIndex() );
    tmp.start();
    After i show for second time an Applet and select a button from it. the button does nothing...
    Thanks...

  • Focus issue with CardLayout (Java 2 SDK, Standard Edition 1.4.0_01)

    I am having an issue with focus and CardLayout with Java 2 SDK, Standard Edition 1.4.0_01. I have created a small sample application to illustrate my problem. In general, I am trying to create a "Wizard" that the user will enter information and then press a "Next" button to proceed to the next step.
    When the first card is displayed, the focus is on the first text field as expected.
    When I go to the next card by clicking "Next", the focus is not on the text field that has requested it (through the requestFocusInWindow method). The focus is on the "Cancel" button, which is the next component to receive focus after the "Next" button on that panel. I do notice that if I use my mouse to bring focus to the window the text field will gain focus.
    Similarly, when I proceed to the last card, the focus is not on the "Finish" button until the mouse moves over the window.
    Is there something I am doing wrong or is there a bug with focus and CardLayout?
    One other problem I have noticed is that the buttons no longer respond to the "Enter" key press and instead respond to the space bar. Any suggestions as to why this is the case?
    Thanks,
    S.L.
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class CardWindow extends JFrame implements ActionListener {
    public CardWindow() {       
    setTitle("Focus Problems with CardLayout");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    cards = new JPanel();
    cardLayout = new CardLayout();
    cards.setLayout(cardLayout);
    cards.add(createFirstNamePanel(), "FirstNamePanel");
    cards.add(createLastNamePanel(), "LastNamePanel");
    cards.add(createFullNamePanel(), "FullNamePanel");
    getContentPane().add(cards,BorderLayout.CENTER);
    getContentPane().add(createButtonPanel(), BorderLayout.SOUTH);
    resetButtonPanel();
    pack();
    private JPanel createFirstNamePanel() {
    JPanel panel = new JPanel();
    JLabel lblDescriptionProjectName = new JLabel("Please enter your first name:");
    txtFirstName = new JTextField(20);
    panel.add(lblDescriptionProjectName);
    panel.add(txtFirstName);
    return panel;
    private JPanel createLastNamePanel() {
    JPanel panel = new JPanel();
    JLabel lblDescriptionProjectName = new JLabel("Please enter your last name:");
    txtLastName = new JTextField(20);
    panel.add(lblDescriptionProjectName);
    panel.add(txtLastName);
    return panel;
    private JPanel createFullNamePanel(){
    JPanel panel = new JPanel();
    lblFullName = new JLabel();
    resetTextOnFullNamePanel();
    panel.add(lblFullName);
    return panel;
    private JPanel createButtonPanel() {
    buttonPanel = new JPanel();
    btnPrevious = new JButton("< " + "Back");
    btnPrevious.setMnemonic('B');
    btnPrevious.addActionListener(this);
    btnNext = new JButton("Next" + " >");
    btnNext.setMnemonic('N');
    btnNext.addActionListener(this);
    btnCancel = new JButton("Cancel");
    btnCancel.setMnemonic('C');
    btnCancel.addActionListener(this);
    btnFinish = new JButton("Finish");
    btnFinish.setMnemonic('F');
    btnFinish.addActionListener(this);
    buttonPanel.add(btnPrevious);
    buttonPanel.add(btnNext);
    buttonPanel.add(btnCancel);
    buttonPanel.add(btnFinish);
    return buttonPanel;
    private void resetTextOnFullNamePanel(){
    lblFullName.setText("Your name is: " + getFirstName() + " " + getLastName());
    private void resetButtonPanel(){
    Component c[] = buttonPanel.getComponents();
    for(int i = 0; i < c.length; i++){
    c.setVisible(false);
    switch(iWizardStep){
    case FIRSTNAMEPANEL:
    btnPrevious.setVisible(true);
    btnNext.setVisible(true);
    btnCancel.setVisible(true);
    break;
    case LASTNAMEPANEL:
    btnPrevious.setVisible(true);
    btnNext.setVisible(true);
    btnCancel.setVisible(true);
    break;
    case FULLNAMEPANEL:
    btnFinish.setVisible(true);
    break;
    buttonPanel.validate();
    public void actionPerformed(ActionEvent e) {
    Object object = e.getSource();
    if (object == btnNext) {           
    btnNextPressed();
    } else if (object == btnPrevious) {           
    btnPreviousPressed();
    } else if (object == btnFinish) {
    System.exit(0);
    } else if (object == btnCancel) {
    System.exit(0);
    private void btnNextPressed() {       
    switch (iWizardStep) {
    case FIRSTNAMEPANEL:
    setFirstName(txtFirstName.getText());
    break;
    case LASTNAMEPANEL:
    setLastName(txtLastName.getText());
    resetTextOnFullNamePanel();
    break;
    iWizardStep++;
    resetButtonPanel();
    this.cardLayout.next(this.cards);
    switch (iWizardStep) {             
    case LASTNAMEPANEL:
    txtLastName.requestFocusInWindow();
    break;
    case FULLNAMEPANEL:
    btnFinish.requestFocusInWindow();
    break;
    private void btnPreviousPressed() {
    iWizardStep--;
    resetButtonPanel();
    this.cardLayout.previous(this.cards);
    public void setFirstName(String value) {
    firstName = value;
    public String getFirstName() {
    return firstName;
    public void setLastName(String value) {
    lastName = value;
    public String getLastName() {
    return lastName;
    public static void main (String[] args) {
    CardWindow c = new CardWindow();
    c.show();
    private CardLayout cardLayout;
    private JPanel cards, buttonPanel;
    private JTextField txtLastName, txtFirstName;
    private JLabel lblFullName;
    private JButton btnNext, btnPrevious, btnCancel, btnFinish;
    private String firstName = "";
    private String lastName = "";
    private int iWizardStep = 0;
    private static final int FIRSTNAMEPANEL = 0;
    private static final int LASTNAMEPANEL = 1;
    private static final int FULLNAMEPANEL = 2;

    Manfred,
    Thanks for your reply. I tried requestFocus() and it gives the same results. Also Sun's 1.4.0 API (http://java.sun.com/j2se/1.4/docs/api/) mentions the following with respect to the requestFocus() method in the JComponent class:
    Because the focus behavior of this method is platform-dependent, developers are strongly encouraged to use requestFocusInWindow when possible.
    That is why I used requestFocusInWindow.
    S.L.

  • Why doesn't the Card show up from the CardLayout:used JPanel,JFrame

    hello guys,
    this is a question regarding. As I did not get nice responses i am posting again today.
    http://forum.java.sun.com/thread.jsp?forum=57&thread=235761
    Hi, I would like to re-arrange my question and put it in a simpler way. I have a frame(mainframes.java) with border layout. There is a panel on the west(borderlayout.west) which has a few buttons. The center panel(borderlayout.center) has a cardlayout and needless to mention that it contains a deck of panels. When I click on a button in the left panel, an appropriate panel in the deck should be displayed in the center panel. It's all simple so far, but here comes the real problem. I have designed my application in such a way that I have each of my panels in a separate class(each of which extend JPanel) and I am trying to integrate them all in my frame. When I click on the button on the left panel(p.s.:this panel is also in a separate class), then the appropriate panel should be displayed in the frame(i.e. in the center deck).
    mainframes.java:-
    //This is the local method which takes an argument from the invoking panel and displays the corresponding card in the deck.
    void showpanel(String p1)
    this.cardLayout2.show(CENTERPANEL,p1);
    System.out.println("Inside showpanel "+ p1);
    leftpanel.java:-
    void B_LOGIN_actionPerformed(ActionEvent e) {
    new mainframes().showpanel("lp");
    // I am trying to display the card "lp" when I click on a button in the leftpanel, but I can only get the console output as "Inside showpanel lp". This shows that the method showpanel(String lp) is getting invoked but the required card is not being displayed. i.e. this.cardLayout2.show(CENTERPANEL,p1) seems to be not working.
    Thanks for your time. Mail me, if you want to access the complete code, at [email protected]

    I'd check to make sure 3 things...
    1) You have added your Panels to the CardLayout Panel
    2) You have added the CardLayoutPanel to the center of your BorderLayout JPanel
    3) Try giving calls to validate()/invalidate() on the JPanel containing the CardLayoutPanel
    void showpanel(String p1)
    ... invalidate()
    this.cardLayout2.show(CENTERPANEL,p1);
    System.out.println("Inside showpanel "+ p1);
    ... validate()
    Otherwise, I may just need to check your source code, you can send to [email protected]

  • Java Applet Panel problems with CardLayout

    Hi, I have a problem a I have been stucked for 2 days.
    I created 3 Panels to use with CardLayout. I would switch between those 3 panels depending on user interaction. I have some textfields and text areas on the 2nd and 3rd Panel. On the 1st panel, I would like to use g.drawstring to write some stuff on that panel, but the words would be blocked off if it goes to where the components are on the other panels.
    Thanks.

    import java.applet.Applet;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.font.*;
    public class CardTest extends Applet
        Panel panel;
        CardLayout cards;
        public void init()
        {   cards = new CardLayout();
            panel = new Panel(cards);
            panel.add("one", new GraphicPanel());
            panel.add("two", getPanel("panel two"));
            panel.add("three", getPanel("panel three"));
            setLayout(new BorderLayout());
            add(getNavPanel(), "North");
            add(panel);
        private Panel getNavPanel()
            final Button
                last = new Button("last"),
                next = new Button("next");
            ActionListener l = new ActionListener()
                public void actionPerformed(ActionEvent e)
                    Button button = (Button)e.getSource();
                    if(button == last)
                        cards.previous(panel);
                    if(button == next)
                        cards.next(panel);
            last.addActionListener(l);
            next.addActionListener(l);
            Panel panel = new Panel();
            panel.add(last);
            panel.add(next);
            return panel;
        private Panel getPanel(String s)
            Panel panel = new Panel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.weighty = 1.0;
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            panel.add(new Button("button"), gbc);
            panel.add(new TextArea(s, 8, 16), gbc);
            return panel;
        public static void main(String[] args)
            Applet applet = new CardTest();
            Frame f = new Frame();
            f.addWindowListener(new WindowAdapter()
                public void windowClosing(WindowEvent e)
                    System.exit(0);
            f.add(applet);
            f.setSize(400,400);
            f.setLocation(200,200);
            applet.init();
            f.setVisible(true);
    class GraphicPanel extends Panel
        Font font;
        String text;
        final int PAD = 25;
        public GraphicPanel()
            font = new Font("lucida bright regular", Font.PLAIN, 22);
            text = "Hello World";
        public void paint(Graphics g)
            super.paint(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
            int w = getWidth();
            int h = getHeight();
            int dia = Math.min(w,h)/6;
            g2.setPaint(Color.blue);
            g2.drawLine(PAD, PAD, w - PAD, PAD);
            g2.drawLine(PAD, h - PAD, w - PAD, h - PAD);
            g2.setPaint(Color.green.darker());
            g2.drawOval(w/2 - dia/2, h*2/3, dia, dia);
            g2.setFont(font);
            FontRenderContext frc = g2.getFontRenderContext();
            float width = (float)font.getStringBounds(text, frc).getWidth();
            LineMetrics lm = font.getLineMetrics(text, frc);
            float x = (w - width)/2;
            float y = (h + lm.getHeight())/2 -lm.getDescent();
            g2.setPaint(Color.black);
            g2.drawString(text, x, y);
    }

  • Collecting information from CardLayout / RadioButtons

    Hi there,
    I have a following code,
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    //public class CardLayoutDemo implements ItemListener {
    public class jlistandClayout implements ListSelectionListener {
        JPanel cards; //a panel that uses CardLayout
        //final static String BUTTONPANEL = "JPanel with JButtons";
        //final static String TEXTPANEL = "JPanel with JTextField";
          final static int temp   =    4;
        public void addComponentToPane(Container pane) {
            //Put the JComboBox in a JPanel to get a nicer look.
            JPanel comboBoxPane = new JPanel(); //use FlowLayout
            String[] GUNIT      = new String[temp];
            GUNIT[0] = "2";
            GUNIT[1] = "3";
            GUNIT[2] = "5";
            GUNIT[3] = "4";
              JList cb = new JList(GUNIT); // added
              cb.addListSelectionListener(this); // added
            comboBoxPane.add(cb);
            JPanel[]  GPANEL              = new JPanel[temp];
            JRadioButton[][]   GBUTTON    = new JRadioButton[1][temp];
            for(int i=0; i<temp; i++){
                 GPANEL[i] =    new JPanel();
                 ButtonGroup    bGroup   = new ButtonGroup();
                 GPANEL.setLayout(new GridLayout(1,temp));
         for(int f=0; f < temp; f++){
              GBUTTON[0][0] = new JRadioButton(GUNIT[i] + "A");
              GBUTTON[0][1] = new JRadioButton(GUNIT[i] + "B");
              GBUTTON[0][2] = new JRadioButton(GUNIT[i] + "C");
              GBUTTON[0][3] = new JRadioButton(GUNIT[i] + "D");
              bGroup.add(GBUTTON[0][f]);
              GPANEL[i].add(GBUTTON[0][f]);
    JPanel GoPanel = new JPanel();
    JButton Go = new JButton("Go");
    GoPanel.add(Go);
    //Create the panel that contains the "cards".
    cards = new JPanel(new CardLayout());
    for(int j=0; j < temp; j++){
    cards.add(GPANEL[j], GUNIT[j]);
    pane.add(comboBoxPane, BorderLayout.PAGE_START);
    pane.add(cards, BorderLayout.CENTER);
    pane.add(GoPanel, BorderLayout.PAGE_END);
    public void itemStateChanged(ItemEvent evt) {
    CardLayout cl = (CardLayout)(cards.getLayout());
    cl.show(cards, (String)evt.getItem());
         public void valueChanged(ListSelectionEvent e)
    if (e.getValueIsAdjusting() == false) {
    CardLayout cl = (CardLayout)(cards.getLayout());
         JList list = (JList)e.getSource();
    cl.show(cards, (String)list.getSelectedValue());
    * Create the GUI and show it. For thread safety,
    * this method should be invoked from the
    * event-dispatching thread.
    private static void createAndShowGUI() {
    //Make sure we have nice window decorations.
    JFrame.setDefaultLookAndFeelDecorated(true);
    //Create and set up the window.
    JFrame frame = new JFrame("CardLayoutDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //Create and set up the content pane.
    jlistandClayout demo = new jlistandClayout();
    demo.addComponentToPane(frame.getContentPane());
    //Display the window.
    frame.pack();
    frame.setVisible(true);
    public static void main(String[] args) {
    //Schedule a job for the event-dispatching thread:
    //creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    createAndShowGUI();
    Which creates the list with list items. The centre panel uses carlayout to contain different sub-panels with combination of radio buttons.
    I want to create a hashtable of values, which corresponds to selected radio buttons, after pressing go button. In other words, suppose I select 1B, 2A, 3C, and press Go button, then the hashtable contains the values 1B,2A, and 3C will be created.
    I am bit stuck. This is just a simple version of the problem that I have (the real version is too big, and bit complicated). Once I get the solution for this, then I could use this solution to my real problem. Your advices and ideas would be much appreciated. Thank you.
    Regards,
    Young

    This is what I have so far:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    //public class CardLayoutDemo implements ItemListener {
    public class jlistandClayout implements ListSelectionListener, ActionListener {
        JPanel cards;
          final static int temp   =    4;
          private String AButton; //just added
          private String BButton; //just added
          private String CButton; //just added
          private String DButton; //just added
        public void addComponentToPane(Container pane) {
            JPanel comboBoxPane = new JPanel();
            String[] GUNIT      = new String[temp];
            GUNIT[0] = "2";
            GUNIT[1] = "3";
            GUNIT[2] = "5";
            GUNIT[3] = "4";
              JList cb = new JList(GUNIT);
              cb.addListSelectionListener(this);
            comboBoxPane.add(cb);
            JPanel[]  GPANEL              = new JPanel[temp];
            JRadioButton[][]   GBUTTON    = new JRadioButton[1][temp];
            for(int i=0; i<temp; i++){
                 GPANEL[i] =    new JPanel();
                 ButtonGroup    bGroup   = new ButtonGroup();
                 GPANEL.setLayout(new GridLayout(1,temp));
         for(int f=0; f < temp; f++){
              AButton = GUNIT[i] + "A";//just added
              BButton = GUNIT[i] + "B";//just added
              CButton = GUNIT[i] + "C";//just added
              DButton = GUNIT[i] + "D";//just added
              GBUTTON[0][0] = p_jradiobuttonNew(AButton, GUNIT[i] + "A", false);//modified
              GBUTTON[0][1] = p_jradiobuttonNew(BButton, GUNIT[i] + "B", false);//modified
              GBUTTON[0][2] = p_jradiobuttonNew(CButton, GUNIT[i] + "C", false);//modified
              GBUTTON[0][3] = p_jradiobuttonNew(DButton, GUNIT[i] + "D", false);//modified
              bGroup.add(GBUTTON[0][f]);
              GPANEL[i].add(GBUTTON[0][f]);
    JPanel GoPanel = new JPanel();
    JButton Go = new JButton("Go");
    GoPanel.add(Go);
    //Create the panel that contains the "cards".
    cards = new JPanel(new CardLayout());
    for(int j=0; j < temp; j++){
    cards.add(GPANEL[j], GUNIT[j]);
    pane.add(comboBoxPane, BorderLayout.PAGE_START);
    pane.add(cards, BorderLayout.CENTER);
    pane.add(GoPanel, BorderLayout.PAGE_END);
    private JRadioButton p_jradiobuttonNew( //just added. this is how I usually create and initialize the components
              final String a_sCommand
                   ,final String a_sText
              ,boolean isSelected
                   javax.swing.JRadioButton jradiobutton = new JRadioButton();
                   jradiobutton.setText( a_sText );
                   jradiobutton.setActionCommand( a_sCommand);
              return jradiobutton;      
    public void actionPerformed( //to be completed
    java.awt.event.ActionEvent a_actionevent
    public void itemStateChanged(ItemEvent evt) {
    CardLayout cl = (CardLayout)(cards.getLayout());
    cl.show(cards, (String)evt.getItem());
         public void valueChanged(ListSelectionEvent e)
    if (e.getValueIsAdjusting() == false) {
    CardLayout cl = (CardLayout)(cards.getLayout());
         JList list = (JList)e.getSource();
    cl.show(cards, (String)list.getSelectedValue());
    * Create the GUI and show it. For thread safety,
    * this method should be invoked from the
    * event-dispatching thread.
    private static void createAndShowGUI() {
    //Make sure we have nice window decorations.
    JFrame.setDefaultLookAndFeelDecorated(true);
    //Create and set up the window.
    JFrame frame = new JFrame("CardLayoutDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //Create and set up the content pane.
    jlistandClayout demo = new jlistandClayout();
    demo.addComponentToPane(frame.getContentPane());
    //Display the window.
    frame.pack();
    frame.setVisible(true);
    public static void main(String[] args) {
    //Schedule a job for the event-dispatching thread:
    //creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    createAndShowGUI();
    just added more lines and commented only the parts that are changed...the whole point of showing you this code is to see if it is possible to add the action listeners to all the radiobuttons like this. Please correct me if I am wrong. Thank you
    Young

  • Problem with CardLayout

    I ve got a JFrame object and i want to put in it a JMenuBar and a CardLayout that contains two JPanel.
    1. Is it possible to do that
    3. how to do it.(I have already created my JMenuBar in my JFrame)

    Ok this is my source code.
    my problem is to insert a cardlayout it dosen't seem to work.
    Can you help me please.
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.TitledBorder ;
    public class Frame1 extends javax.swing.JFrame
         Panel_Moteur jPanel1 = new Panel_Moteur();
         javax.swing.JMenuBar jMenuBar1 = new javax.swing.JMenuBar();
         javax.swing.JMenu jMenuFichier = new javax.swing.JMenu();
         javax.swing.JSeparator jMenuFichierSeparator1 = new javax.swing.JSeparator();
         javax.swing.JMenuItem jMenuFichierQuitter = new javax.swing.JMenuItem();
         javax.swing.JMenu jMenuConfiguration = new javax.swing.JMenu();
         javax.swing.JMenuItem jMenuConfigurationMoteur = new javax.swing.JMenuItem();
         javax.swing.JSeparator jMenuConfigurationSeparator1 = new javax.swing.JSeparator();
         javax.swing.JMenuItem jMenuConfigurationHoraire = new javax.swing.JMenuItem();
         javax.swing.JMenu jMenuProgrammes = new javax.swing.JMenu();
         javax.swing.JMenuItem jMenuProgrammesAjouter = new javax.swing.JMenuItem();
         javax.swing.JMenuItem jMenuProgrammesModifier = new javax.swing.JMenuItem();
         javax.swing.JMenuItem jMenuProgrammesSupprimer = new javax.swing.JMenuItem();
         javax.swing.JSeparator jMenuProgrammesSeparator1 = new javax.swing.JSeparator();
         javax.swing.JMenuItem jMenuProgrammesVisualiser = new javax.swing.JMenuItem();
         javax.swing.JMenu jMenuAcquisition = new javax.swing.JMenu();
         javax.swing.JMenuItem jMenuAcquisitionCapture = new javax.swing.JMenuItem();
         javax.swing.JMenu jMenuReconstitution = new javax.swing.JMenu();
         javax.swing.JMenuItem jMenuReconstitutionImageGlobale = new javax.swing.JMenuItem();
         JPanel cards = new JPanel();
         CardLayout cardlayout = new CardLayout() ;
         public Frame1() {
         public void initComponents() throws Exception
              cards.setSize(new java.awt.Dimension(930, 830));
              cards.setLocation(new java.awt.Point(0, 40));
    cards.setLayout(cardlayout);
              cards.add(jPanel1,"t");
              jMenuBar1.setVisible(true);
              jMenuFichier.setVisible(true);
              jMenuFichier.setText("Fichier");
              jMenuFichierSeparator1.setVisible(true);
              jMenuFichierQuitter.setVisible(true);
              jMenuFichierQuitter.setText("Quitter");
              jMenuConfiguration.setVisible(true);
              jMenuConfiguration.setText("Configuration");
              jMenuConfigurationMoteur.setVisible(true);
              jMenuConfigurationMoteur.setText("Moteur");
              jMenuConfigurationSeparator1.setVisible(true);
              jMenuConfigurationHoraire.setVisible(true);
              jMenuConfigurationHoraire.setText("Horaire");
              jMenuProgrammes.setVisible(true);
              jMenuProgrammes.setText("Programmes");
              jMenuProgrammesAjouter.setVisible(true);
              jMenuProgrammesAjouter.setText("Ajouter");
              jMenuProgrammesModifier.setVisible(true);
              jMenuProgrammesModifier.setText("Modifier");
              jMenuProgrammesSupprimer.setVisible(true);
              jMenuProgrammesSupprimer.setText("Supprimer");
              jMenuProgrammesSeparator1.setVisible(true);
              jMenuProgrammesVisualiser.setVisible(true);
              jMenuProgrammesVisualiser.setText("Visualiser");
              jMenuAcquisition.setVisible(true);
              jMenuAcquisition.setText("Acquisition");
              jMenuAcquisitionCapture.setVisible(true);
              jMenuAcquisitionCapture.setText("Capture");
              jMenuReconstitution.setVisible(true);
              jMenuReconstitution.setText("Reconstitution");
              jMenuReconstitutionImageGlobale.setVisible(true);
              jMenuReconstitutionImageGlobale.setText("Image Globale");
              setLocation(new java.awt.Point(150, 70));
              setResizable(false);
              setJMenuBar(jMenuBar1);
              getContentPane().setLayout(null);
              setTitle("ProjetAutoroot.Frame1");
              jMenuBar1.add(jMenuFichier);
              jMenuBar1.add(jMenuConfiguration);
              jMenuBar1.add(jMenuProgrammes);
              jMenuBar1.add(jMenuAcquisition);
              jMenuBar1.add(jMenuReconstitution);
              jMenuFichier.add(jMenuFichierSeparator1);
              jMenuFichier.add(jMenuFichierQuitter);
              jMenuConfiguration.add(jMenuConfigurationMoteur);
              jMenuConfiguration.add(jMenuConfigurationSeparator1);
              jMenuConfiguration.add(jMenuConfigurationHoraire);
              jMenuProgrammes.add(jMenuProgrammesAjouter);
              jMenuProgrammes.add(jMenuProgrammesModifier);
              jMenuProgrammes.add(jMenuProgrammesSupprimer);
              jMenuProgrammes.add(jMenuProgrammesSeparator1);
              jMenuProgrammes.add(jMenuProgrammesVisualiser);
              jMenuAcquisition.add(jMenuAcquisitionCapture);
              jMenuReconstitution.add(jMenuReconstitutionImageGlobale);
              getContentPane().add(cards);
              setSize(new java.awt.Dimension(941, 923));
              jMenuFichierQuitter.addActionListener(new java.awt.event.ActionListener() {
                   public void actionPerformed(java.awt.event.ActionEvent e) {
                        jMenuFichierQuitterActionPerformed(e);
              jMenuConfigurationMoteur.addActionListener(new java.awt.event.ActionListener() {
                   public void actionPerformed(java.awt.event.ActionEvent e) {
                        jMenuConfigurarionMoteurActionPerformed(e);
              addWindowListener(new java.awt.event.WindowAdapter() {
                   public void windowClosing(java.awt.event.WindowEvent e) {
                        thisWindowClosing(e);
         private boolean mShown = false;
         public void addNotify() {
              super.addNotify();
              if (mShown)
                   return;
              // resize frame to account for menubar
              JMenuBar jMenuBar = getJMenuBar();
              if (jMenuBar != null) {
                   int jMenuBarHeight = jMenuBar.getPreferredSize().height;
                   Dimension dimension = getSize();
                   dimension.height += jMenuBarHeight;
                   setSize(dimension);
              mShown = true;
         // Close the window when the close box is clicked
         void thisWindowClosing(java.awt.event.WindowEvent e) {
              setVisible(false);
              dispose();
              System.exit(0);
         public void jMenuFichierQuitterActionPerformed(java.awt.event.ActionEvent e) {
              setVisible(false);
              dispose();
              System.exit(0);
         public void jMenuConfigurarionMoteurActionPerformed (java.awt.event.ActionEvent e)
              System.out.println("bonjour") ;
              CardLayout cl = (CardLayout)(cards.getLayout());
              cl.show(cards,"t");
         public void jRadioButton1StateChanged(javax.swing.event.ChangeEvent e) {
         public void jRadioButton2StateChanged(javax.swing.event.ChangeEvent e) {
         public void jRadioButton9StateChanged(javax.swing.event.ChangeEvent e) {
    }

  • I Can't switch my CardLayout @.@

    There is a card 3 but i havn't add ,so the "Enterbtn" is to trigger card 3.
    So what is wrong with my code that it can't switch? I changed the cardPanel from 2 to 1 and 1 to 2 but it only appear card 2.
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;
    public class HPhoneInv extends JFrame {
    private int currentCard = 1;
    private JPanel cardPanel;
    private CardLayout cl;
    public HPhoneInv() {
    setTitle("Handphone Inventory System");
    setSize(500, 160);
    cardPanel = new JPanel();
    cl = new CardLayout();
    cardPanel.setLayout(cl);
    JPanel jp1 = new JPanel();
    jp1.setLayout(new GridLayout(1,1) );
    JLabel jl1 = new JLabel("Select Option:");
    jp1.add(jl1);
    cardPanel.add(jp1, "1");
    JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new GridLayout(1,2) );
    JButton AcqBtn=new JButton("Used Phone Acquisition");
    JButton TranBtn=new JButton("Sale Transaction");
    buttonPanel.add(AcqBtn);
    buttonPanel.add(TranBtn);
    getContentPane().add(jp1, BorderLayout.NORTH);
    getContentPane().add(buttonPanel, BorderLayout.SOUTH);
    JPanel jp2 = new JPanel();
    jp2.setLayout(new GridLayout(5,2) );
    JLabel lbl1 = new JLabel("Cust IC No:");
    JLabel lbl2= new JLabel("Brand:");
    JLabel lbl3 =new JLabel("Model:");
    JLabel lbl4=new JLabel("Serial No:");
    JLabel lbl5=new JLabel("Cost:");
    JTextField tf1=new JTextField("");
    JTextField tf2=new JTextField("");
    JTextField tf3=new JTextField("");
    JTextField tf4=new JTextField("");
    JTextField tf5=new JTextField("");
    jp2.add(lbl1);
    jp2.add(tf1);
    jp2.add(lbl2);
    jp2.add(tf2);
    jp2.add(lbl3);
    jp2.add(tf3);
    jp2.add(lbl4);
    jp2.add(tf4);
    jp2.add(lbl5);
    jp2.add(tf5);
    cardPanel.add(jp2, "2");
    JPanel buttonPanel2 = new JPanel();
    buttonPanel2.setLayout(new GridLayout(1,2) );
    JButton EnterBtn=new JButton("Enter");
    JButton CancelBtn=new JButton("Cancel");
    buttonPanel2.add(EnterBtn);
    buttonPanel2.add(CancelBtn);
    getContentPane().add(jp2, BorderLayout.NORTH);
    getContentPane().add(buttonPanel2, BorderLayout.SOUTH);
    AcqBtn.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
    if (currentCard < 2) {
    currentCard +=1;
    cl.show(cardPanel, "" + (currentCard));
    TranBtn.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
    if (currentCard < 2) {
    currentCard += 1;
    cl.show(cardPanel, "" + (currentCard));
    EnterBtn.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
    if (currentCard > 1) {
    currentCard = 1;
    CancelBtn.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
    if (currentCard > 1) {
    currentCard = 1;
    cl.show(cardPanel, "" + (currentCard));
    public static void main(String[] args) {
    HPhoneInv cl = new HPhoneInv();
    cl.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    cl.setVisible(true);
    Message was edited by:
    RockmanEXE
    Message was edited by:
    RockmanEXE

    The order of the components, added into the cards and into the JFrame are wrong, so i just rearranged the order and i added one more JPanel.
    so based on that, u code accordinglyimport java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;
    public class HPhoneInv extends JFrame {
        private int currentCard = 1;
        private JPanel cardPanel;
        private CardLayout cl;
        JPanel card1 = null;
        JPanel card2 = null;
        public HPhoneInv() {
            setTitle("Handphone Inventory System");
            setSize(500, 160);
            cardPanel = new JPanel(new CardLayout());
            JPanel jp1 = new JPanel();
            jp1.setLayout(new GridLayout(1,1) );
            JLabel jl1 = new JLabel("Select Option:");
            jp1.add(jl1);
            //cardPanel.add(jp1, "1");
            JPanel buttonPanel = new JPanel();
            buttonPanel.setLayout(new GridLayout(1,2) );
            JButton AcqBtn=new JButton("Used Phone Acquisition");
            JButton TranBtn=new JButton("Sale Transaction");
            buttonPanel.add(AcqBtn);
            buttonPanel.add(TranBtn);
            card1 = new JPanel(new BorderLayout());
            //getContentPane().add(jp1, BorderLayout.NORTH);
            //getContentPane().add(buttonPanel, BorderLayout.SOUTH);
            card1.add(jp1, BorderLayout.NORTH);
            card1.add(buttonPanel, BorderLayout.SOUTH);
            card2 = new JPanel(new BorderLayout());
            JPanel jp2 = new JPanel();
            jp2.setLayout(new GridLayout(5,2) );
            JLabel lbl1 = new JLabel("Cust IC No:");
            JLabel lbl2= new JLabel("Brand:");
            JLabel lbl3 =new JLabel("Model:");
            JLabel lbl4=new JLabel("Serial No:");
            JLabel lbl5=new JLabel("Cost:");
            JTextField tf1=new JTextField("");
            JTextField tf2=new JTextField("");
            JTextField tf3=new JTextField("");
            JTextField tf4=new JTextField("");
            JTextField tf5=new JTextField("");
            jp2.add(lbl1);
            jp2.add(tf1);
            jp2.add(lbl2);
            jp2.add(tf2);
            jp2.add(lbl3);
            jp2.add(tf3);
            jp2.add(lbl4);
            jp2.add(tf4);
            jp2.add(lbl5);
            jp2.add(tf5);
            //cardPanel.add(jp2, "2");
            JPanel buttonPanel2 = new JPanel();
            buttonPanel2.setLayout(new GridLayout(1,2) );
            JButton EnterBtn=new JButton("Enter");
            JButton CancelBtn=new JButton("Cancel");
            buttonPanel2.add(EnterBtn);
            buttonPanel2.add(CancelBtn);
            //getContentPane().add(jp2, BorderLayout.NORTH);
            //getContentPane().add(buttonPanel2, BorderLayout.SOUTH);
            card2.add(jp2, BorderLayout.NORTH);
            card2.add(buttonPanel2, BorderLayout.SOUTH);
            cardPanel.add(card2,"two");
            cardPanel.add(card1,"one");
            this.getContentPane().add(cardPanel);
             AcqBtn.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                        CardLayout cardLayout = (CardLayout)(cardPanel.getLayout());
                        cardLayout.show(cardPanel,"two");
                    /*if (currentCard < 2) {
                        currentCard +=1;
                        cl.show(cardPanel, "" + (currentCard));
               TranBtn.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                        CardLayout cardLayout = (CardLayout)(cardPanel.getLayout());
                        cardLayout.show(cardPanel,"two");
                    /*if (currentCard < 2) {
                        currentCard += 1;
                      cl.show(cardPanel, "" + (currentCard));
                EnterBtn.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                        //System.out.println("Enter Button got clicked");
                        CardLayout cardLayout = (CardLayout)(cardPanel.getLayout());
                        cardLayout.show(cardPanel,"one");
             CancelBtn.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                        System.exit(0);
                    if (currentCard > 1) {
                        currentCard = 1;
                      cl.show(cardPanel, "" + (currentCard));
        public static void main(String[] args) {
            HPhoneInv cl = new HPhoneInv();
            cl.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            cl.setVisible(true);
    }

  • Can't change the CardLayout.

    Sorry, can someone help me with this code. I can't change the cardlayout when i try to seperate the JButton and the cardLayout. However, when i try it in one class, there is no problem. Can help me with this and give me some advise? I am learning how to seperate to classes so that the code will not look so messy. Is this the correct way of doing?
    /**********This is the main class*************/
    import javax.swing.*;
    import java.awt.*;
    public class PlayCard extends JFrame
         NorthConsole nc = new NorthConsole();
         ChangingCard cc = new ChangingCard();
         JFrame f1;
         JPanel mainPanel = new JPanel();
         public PlayCard()
              f1 = new JFrame("Card Panel");
              f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              f1.setSize(400,200);
              mainPanel.setLayout(new BorderLayout());
              mainPanel.add(nc.northCard(),BorderLayout.NORTH);
              mainPanel.add(cc.cardPanel(),BorderLayout.SOUTH);
              f1.getContentPane().add(mainPanel);
              f1.setVisible(true);
         public static void main(String[] args)
              PlayCard pc = new PlayCard();
    /*********This is the Panel that contain the button that does the changing*****/
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    public class NorthConsole implements ActionListener
         JButton b3 = new JButton("Display card 1");     
         JButton b4 = new JButton("Display card 2");     
         ChangingCard ch = new ChangingCard();
         JPanel northCard()
              JPanel nc1 = new JPanel();
              b3.addActionListener(this);
              b4.addActionListener(this);     
              nc1.add(b3);
              nc1.add(b4);
              return nc1;
         public void actionPerformed(ActionEvent f)
              ch.getCard();
              Object source = f.getSource();
              if(source==b3)
                   ch.displayShow1();
              }//end of b1 action
              if(source==b4)
                   ch.displayShow2();     
              }//end of b2 action
    }//end of class
    /************This is the CardLayout Class******************/
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class ChangingCard
         String card1But = "Panel 1 with Button 1";
         String card2But = "Panel 2 with Button 2";
         JButton b1 = new JButton("Card 1");
         JButton b2 = new JButton("Card 2");
         JPanel cMain = new JPanel(new CardLayout());
         JPanel c1 = new JPanel();
         JPanel c2 = new JPanel();
         CardLayout cl;
         JPanel cardPanel()
              c1.add(b1);
              c2.add(b2);
              cMain.add(c1,card1But);     
              cMain.add(c2,card2But);
              return cMain;
         public CardLayout getCard()
              cl = (CardLayout)(cMain.getLayout());
              return cl;
         public void displayShow1()
              getCard().show(cMain,card1But);
              System.out.println("Hello");     
         public void displayShow2()
              getCard().show(cMain,card2But);
              System.out.println("Hello2");
    }

    try it this way
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    class PlayCard extends JFrame
      ChangingCard cc = new ChangingCard();
      NorthConsole nc = new NorthConsole(cc);
      public PlayCard()
        setTitle("Card Panel");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(400,200);
        JPanel mainPanel = new JPanel(new BorderLayout());
        mainPanel.add(nc,BorderLayout.NORTH);
        mainPanel.add(cc,BorderLayout.SOUTH);
        getContentPane().add(mainPanel);
      public static void main(String[] args){new PlayCard().setVisible(true);}
    class NorthConsole extends JPanel
      ChangingCard cc;
      public NorthConsole(ChangingCard c)
        cc = c;
        final JButton b3 = new JButton("Display card 1");
        b3.addActionListener(new ActionListener(){
          public void actionPerformed(ActionEvent f){
            cc.displayShow1();}});
        JButton b4 = new JButton("Display card 2");
        b4.addActionListener(new ActionListener(){
          public void actionPerformed(ActionEvent f){
            cc.displayShow2();}});
        add(b3);
        add(b4);
    class ChangingCard extends JPanel
      String card1But = "Panel 1 with Button 1";
      String card2But = "Panel 2 with Button 2";
      CardLayout cl;
      public ChangingCard()
        cl = new CardLayout();
        setLayout(cl);
        JPanel c1 = new JPanel();
        c1.add(new JButton("Card 1"));
        JPanel c2 = new JPanel();
        c2.add(new JButton("Card 2"));
        add(card1But,c1);
        add(card2But,c2);
      public void displayShow1(){cl.show(this,card1But);}
      public void displayShow2(){cl.show(this,card2But);}
    }

  • Problem moving through CardLayout

    Hi,
    I have been unable to find any solid examples of CardLayout, so I have been using some code I found in this forum. I'm getting hung up on how to move forward or backwards through the cards. I know it is myCard.next but for some reason I'm getting hung up on which card I should be referencing and how to associate that card with the code. Perhaps someone could spot the error...
    Thanks,
    Steve
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    class CardLayoutQuestionsv2 {
        public static void main(String[] arguments) {
            JFrame frame = new Interface();
            frame.show();
    class Interface extends JFrame {
         private dataPanel screenvar;
    //     private questionPaneOne abcdef;
         private JTextArea msgout;
         JPanel cardPane;
         Interface () {
              super("This is a JFrame");
            setSize(800, 400);  // width, height
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              // set-up card layout
              cardPane = new JPanel();     // for CardLayout
              CardLayout questions = new CardLayout();
              cardPane.setLayout(questions);
              cardPane.setBorder(BorderFactory.createCompoundBorder(
                                              BorderFactory.createMatteBorder(
                                                              1,1,2,2,Color.blue),
                                    BorderFactory.createEmptyBorder(5,5,5,5)));
              cardTestOnePane     cardOne = new cardTestOnePane();
              cardTestTwoPane     cardTwo = new cardTestTwoPane();
              cardPane.add("questionOne", cardOne);
              cardPane.add("questionTwo", cardTwo);
              // end set-up card layout
              // set-up main pane
              // declare components
              msgout = new JTextArea( 8, 40 );
              buttonPanel commandButtons = new buttonPanel(screenvar, msgout);
              JPanel pane = new JPanel();
              pane.setLayout(new GridLayout(2, 4, 5, 15));   // row, col, hgap, vgap
              pane.setBorder(BorderFactory.createEmptyBorder(30, 20, 10, 30)); //top, left, bottom, right
              pane.add(cardPane);
                 pane.add( new JScrollPane(msgout));
                 pane.add(commandButtons);
              msgout.append("Successful");
              setContentPane(pane);
              setVisible(true);
    class updateDataFields implements ActionListener {     // 400
         private dataPanel abc;
         private JTextArea msg;
         int count = 0;
         public updateDataFields(dataPanel xyz, JTextArea msgout) {     // 100
              abc = xyz;
              msg = msgout;
         }     // 100
         public void actionPerformed(ActionEvent evt) {     // 200
              String command = evt.getActionCommand();
                   if (command.equals("TestMe")){     // 300
                        msg.append("\nSuccessful");
                        btnNextPressed();
                   }     // 300
         }     // 200
         private void btnNextPressed() {     // 500
    // problem here
              cardPane.next(cardTwo);
              }     // 500
    }     // 400
    class buttonPanel extends JPanel {     // 200   Similar to ButtonPanel in Duke's Bakery
         public buttonPanel(dataPanel xyz, JTextArea msgout) {     // 100
              GridLayout actionGrid = new GridLayout(1, 1, 5, 15); // row, col, hgap, vgap
              setLayout(actionGrid);                    // different than panex.setLayout(xgrid);
              JButton buttonTest = new JButton("TestMe");
              buttonTest.setMnemonic('T');
              buttonTest.addActionListener( new updateDataFields( xyz, msgout ));
              add(buttonTest);
         }     // 100
    }     // 200
    class dataPanel extends JPanel {     // Similar to DataPanel in Duke's Bakery
         JLabel left1, left2, left3, right1, right2, right3;
         JTextField left01, left02, left03, right01, right02, right03;
        public dataPanel () {     // 1
              GridLayout grid = new GridLayout(3, 2, 5, 15); // row, col, hgap, vgap
              setLayout(grid);                    // different than panex.setLayout(xgrid);
              left1 = new JLabel("Left1");
              add(left1);
              left01 = new JTextField(0);
              add(left01);
              right1 = new JLabel("Right1");
              add(right1);
              right01 = new JTextField(0);
              add(right01);
    class cardTestOnePane extends JPanel {
         public cardTestOnePane() {
              GridLayout actionGrid1 = new GridLayout(2, 1, 5, 15); // row, col, hgap, vgap
              setLayout(actionGrid1);
              JLabel cardNumberOne = new JLabel("Card Number One");
              JLabel cardNumberTwo = new JLabel("Card Number Two");
              add(cardNumberOne);
              add(cardNumberTwo);
    class cardTestTwoPane extends JPanel {
         public cardTestTwoPane() {
              GridLayout actionGrid1 = new GridLayout(2, 1, 5, 15); // row, col, hgap, vgap
              setLayout(actionGrid1);
              JLabel cardNumberThree = new JLabel("Card Number Three");
              JLabel cardNumberFour = new JLabel("Card Number Four");
              add(cardNumberThree);
              add(cardNumberFour);
    }

    When importing files Lightroom states:
    The following files were not imported because they could not be read.
    IMG_5551.CR2
    When deleting files in Lightroom it states:
    The file named 'IMG_6191.jpg' could not be moved to the Trash folder
    When moving files Lightroom shows the following error:
    File could not be moved to the selected destination.
    EDIT: it is only happening in a single folder/location (with a lot of foto's and subfolders). The rest seems to be working correctly. Strange and irritating.

  • URGENT HELP in Panel with CardLayout

    I have a frame class in which I have added one panel and inside that 3 frames.I want to see the frames depending upon each request,the panle is has CardLayout.When i create an instance of the this class in my main and try to show one of the cards it gives wrong parent for CardLayout error???

    I have a frame class in which I have added one panel and inside that 3 frames.I want to see the frames depending upon each request,the panle is has CardLayout.When i create an instance of the this class in my main and try to show one of the cards it gives wrong parent for CardLayout error???

  • Change cards in CardLayout

    how can i change cards in cardlayout by a method in the same class which calling from different class.
    the way i call the method from the outer class is this :
    p1=new play1(); //instanciate the card layout class call play1()
    p1.fire(); //method that supose to swap cards
    the play1 class fire method is like this :
    public void fire() {
    c1.show(base,"two"); // c1 is the variable that asing to cardlayout & "base" is the base of
    } //the card layout then "two" is the alis uses to refer next panel of the card layout.

    HI.
    For all the panels that have no defined layout (the N S E W panels) try setting them to flow layout.
    Java is suppose to do this in default, but it doesnt always seem to....
    G

  • Automatic do background work in panelX when you cardLayout.show(panelX)?

    Let me first explain what I try to accomplish
    +Panel 1: A form that allow people to enter username and password to connect to a database
    +Panel 2: Use the connection that just created in panel1 to access database and load data into a JcomboBox
    +ContentPane that hold these 2 panel (Layout: cardlayout)
    +Top container is a JApplet
    So when the program load, I will cardlayout.show(Panel1), then if the user press a "submit" button, I will cardlayout.show(Panel2). Simple enough. Panel2' s job is to silently access a database using the connection that is created in panel1 a load some information onto a JComboBox. My question is how do I design so that when the user press that "submit" button, it detects that now panel2 is in focus so invoke my loadData() that access the database and load data onto the JcomboBox?
    Right now i have it so that when panel2 show, the user have to click the "load" button to load information. However, I wish to change that. So please give me some advice.
    Thank you very much and happy thanksgiving

    JohnBrand wrote:
    A clarifying question - should the uploading and downloading be done on a SwingWorker background thread or should it be on a non-SwingWorker thread? You can do it either way. It's fielder's choice here.
    Don't the SwingWorker threads end up on the Event Dispatch Thread?Some methods of SwingWorker are always called in the EDT including the done method and the process method, but the main method, doInBackground, in fact the only method that is required in a SwingWorker-extended class is done off of the EDT. Having methods that reliably work on and off of the EDT is what makes using a SwingWorker object an easier way to create background threads in Swing vs. creating a roll-your-own background thread.

  • Please, Help With CardLayout

    This is my first time to actually attempt to use CardLayout. I have an applet that I am working on. Following is a copy of this applet. It compiles correctly as it is, but when I try to view it in the appletviewer, a message at the bottom of the window says that the applet is not initialized, and the compiler brings a ClassCastException. Any help would be appreciated.
    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    public class PayeeApplet extends Applet implements ActionListener
         //Declare constants
         final     String     header = "Tritsch Innovative Enterprise";
         //Declare variables
         String     firstName,
              lastName,
              position;
         int     empNum = 0,
              cont = 0;
         float     rate = 0.0f,
              hours = 0.0f;
         boolean     done = false;
         PayRec[] empList;
         //Declare global components
         TextField     txtFirstName,
                   txtLastName,
                   txtPosition,
                   txtRate,
                   txtHours;
         Label          showFirstName,
                   showLastName,
                   showPosition,
                   showRate,
                   showHours,
                   showWeekPay;
         Button          btnPrev,
                   btnNext,
                   btnEnter,
                   btnExit,
                   btnViewRecord,
                   btnEditRecord,
                   btnAddRecord;
         Font font1 = new Font("dialog", Font.BOLD, 14);
         //Define layouts
         Panel p = new Panel();
          GridLayout layout = new GridLayout(3,1);
         Panel deck = new Panel();
          CardLayout cardLayout = new CardLayout();
           Panel card1Panel = new Panel();
            GridLayout card1Grid = new GridLayout(4,3);
           Panel card2Panel = new Panel();
            GridLayout card2Grid = new GridLayout(2,3);
         Panel p3 = new Panel();
          GridLayout p3Grid = new GridLayout(4,2);
         public void init()
          //Construct components
          Label lblHeader = new Label(header, 1);
           lblHeader.setFont(font1);
          Label lblFirstName = new Label("First Name:");
           txtFirstName = new TextField(10);
          Label lblLastName = new Label("Last Name:");
           txtLastName = new TextField(10);
          Label lblPosition = new Label("Position:");
           txtPosition = new TextField(10);
          Label lblRate = new Label("Rate:");
           txtRate = new TextField(5);
          Label lblHours = new Label("Hours:");
           txtHours = new TextField(5);
          btnPrev = new Button("Previous");
          btnNext = new Button("Next");
          btnEnter = new Button("Enter");
          btnExit = new Button("Exit");
          btnViewRecord = new Button("View");
          btnEditRecord = new Button("Edit");
          btnAddRecord = new Button("Add");
          //Layouts
          p.setLayout(layout);
          deck.setLayout(cardLayout);
          card1Panel.setLayout(card1Grid);
          card2Panel.setLayout(card2Grid);
          p3.setLayout(p3Grid);
          CardLayout cardLayout = (CardLayout)p.getLayout();
          //Add components
          add(p);
           //First grid
           p.add(lblHeader);
           //Second grid
           p.add(deck);
            deck.add("one",card1Panel);
             card1Panel.add(lblFirstName);
             card1Panel.add(txtFirstName);
             card1Panel.add(lblLastName);
             card1Panel.add(txtLastName);
             card1Panel.add(lblPosition);
             card1Panel.add(txtPosition);
             card1Panel.add(lblRate);
             card1Panel.add(txtRate);
             card1Panel.add(lblHours);
             card1Panel.add(txtHours);
            deck.add("two",card2Panel);
             card2Panel.add(showFirstName);
             card2Panel.add(showLastName);
             card2Panel.add(showPosition);
             card2Panel.add(showRate);
             card2Panel.add(showHours);
             card2Panel.add(showWeekPay);
           //Third grid
           p.add(p3);
            p3.add(btnPrev);
             btnPrev.addActionListener(this);
            p3.add(btnNext);
             btnNext.addActionListener(this);
            p3.add(btnEnter);
             btnEnter.addActionListener(this);
            p3.add(btnExit);
             btnExit.addActionListener(this);
            p3.add(btnViewRecord);
             btnViewRecord.addActionListener(this);
            p3.add(btnEditRecord);
             btnEditRecord.addActionListener(this);
            p3.add(btnAddRecord);
             btnAddRecord.addActionListener(this);
         public void actionPerformed(ActionEvent e)
         {cardLayout.first(p);
    }

    I've looked at your code:
    // Here you're setting a GridLayout
    p.setLayout(layout);
    deck.setLayout(cardLayout);
    card1Panel.setLayout(card1Grid);
    card2Panel.setLayout(card2Grid);
    p3.setLayout(p3Grid);
    // Here you retrieve the GridLayout and cast it to a CardLayout
    // this in turn generates a ClassCastException - fair enough !
    CardLayout cardLayout = (CardLayout)p.getLayout();When you have fixed this you'll receive a NullPointerException because you're adding showFirstName etc., which is null.

Maybe you are looking for

  • Error while trying to index a bfile

    hi, I have encountered the following error will trying to index a bfile . Following are the error messages, the listener.ora and creation script. I'm using a AIX Ver 4.3.3 J50 machine and my oracle version is 8.1.7 Thanks Creation Script SQL> create

  • Connecting the iPod Hi-Fi to Airport Express

    OK, a little help would be great here. I purchased the iPod Hi-Fi about 6 months ago and incorrectly assumed that the cabling would be included in order to connect to an Airport Express. For me, that's the whole reason to buy a Hi-Fi vs. another spea

  • Why is the trial watermark still visible?

    Hi, I have recently purchased and recieved my premiere elements 8 software. I downloaded the software trial so i could use it straight away (whilst waiting for the hardcopy) and i was assured that when i recieved my serial code that the watermark fro

  • How to create a spool job for adobe form without dialog

    Hi All, I have a requirement like below. I need to generate a spool for Adobe form output and that sholud be without print parameters dialog popup. What are the parameters i need to pass to the FP_JOB_OPEN function module. Solutions will be appriciat

  • Ace with servers in VMware

    Hi; I have a customer who has a test site  with one ACE doing load balancing for a small farm ( 8 servers). Recently the customer moved his servers to to VMware. The customer claims that since that change, the ACE is causing for large delays. His cla