Page 2 of 2 FirstFirst 12
Results 21 to 29 of 29
  1. #21
    esgol is offline Member
    Join Date
    Dec 2010
    Posts
    29
    Rep Power
    0

  2. #22
    esgol is offline Member
    Join Date
    Dec 2010
    Posts
    29
    Rep Power
    0

    Default

    I'VE MADE IT! :D

    Simply add Action Listener to a
    >> Button BtGotoCard2;
    which will be added to card1 by
    >> card1.add(BtGotoCard2);
    then into the action listener simply

    >>if (e.getSource()==BtGotoCard2)
    >> {
    >> System.out.println("pathitike to Card2");
    >> CardLayout cl = (CardLayout)(cards.getLayout());
    >> cl.show(cards,"Card 2" );
    >> }


    :D :D

    However i wasn able to put aciton listeners in those sort of STRING buttons
    What are they? Just for the decoration?

    Java Code:
    /*
     * CardLayoutDemo.java (modified)
     *
     */
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class CardLayoutDemo implements ActionListener {
        JPanel cards; //a panel that uses CardLayout
        Button BtGotoCard2;
        Button BtGotoCard1;
        //final static String BUTTONPANEL = "Card with JButtons";
        //final static String TEXTPANEL = "Card with JTextField";
    
        public void addComponentToPane(Container pane) {
            //Put the JComboBox in a JPanel to get a nicer look.
            JPanel comboBoxPane = new JPanel(); //use FlowLayout
            //String comboBoxItems[] = { BUTTONPANEL, TEXTPANEL };
            //JComboBox cb = new JComboBox(comboBoxItems);
            //cb.setEditable(false);
            //cb.addItemListener(this);
            //comboBoxPane.add(cb);
    
            //Create the "cards".
            JPanel card1 = new JPanel();
            card1.add(new JButton("Button 1"));
            card1.add(new JButton("Button 2"));
            card1.add(new JButton("Button 3"));
            BtGotoCard2 = new Button("Go to Card2");       //! edo. Prota ap ola to ftiaxneis.
                                           //Prin kaneis kati allo
            card1.add(BtGotoCard2);
            //BtOK.setBounds(0,10,40,20);
            BtGotoCard2.addActionListener(this);
    
            BtGotoCard1 = new Button("Go to Card1");       //! edo. Prota ap ola to ftiaxneis.
                                           //Prin kaneis kati allo
            card1.add(BtGotoCard1);
            //BtOK.setBounds(0,10,40,20);
            BtGotoCard1.addActionListener(this);
    
    
            JPanel card2 = new JPanel();
            card2.add(new JTextField("TextField", 20));
            card2.add(BtGotoCard1);
    
            //Create the panel that contains the "cards".
            cards = new JPanel(new CardLayout());
            cards.add(card1, "Card 1");
            cards.add(card2, "Card 2");
    
            pane.add(comboBoxPane, BorderLayout.PAGE_START);
            pane.add(cards, BorderLayout.CENTER);
    
    
        }
        /*
        public void itemStateChanged(ItemEvent evt) {
            CardLayout cl = (CardLayout)(cards.getLayout());
            cl.show(cards, (String)evt.getItem());
            cl.show(cards, "Button1");
        }*/
    
         public void actionPerformed(ActionEvent e) {
            if (e.getSource()==BtGotoCard2)
            {
                System.out.println("pathitike to Card2");
                CardLayout cl = (CardLayout)(cards.getLayout());
                cl.show(cards,"Card 2" );
            }
            if (e.getSource()==BtGotoCard1)
            {
                System.out.println("pathitike to Card1");
                CardLayout cl = (CardLayout)(cards.getLayout());
                cl.show(cards,"Card 1" );
            }
        }
    
    
        public void windowOpened(WindowEvent e) {
            //throw new UnsupportedOperationException("Not supported yet.");
        }
    
        public void windowClosing(WindowEvent e) {
           
            //throw new UnsupportedOperationException("Not supported yet.");
        }
    
        public void windowClosed(WindowEvent e) {
            //throw new UnsupportedOperationException("Not supported yet.");
        }
    
        public void windowIconified(WindowEvent e) {
            //throw new UnsupportedOperationException("Not supported yet.");
        }
    
        public void windowDeiconified(WindowEvent e) {
            //throw new UnsupportedOperationException("Not supported yet.");
        }
    
        public void windowActivated(WindowEvent e) {
            //throw new UnsupportedOperationException("Not supported yet.");
        }
    
        public void windowDeactivated(WindowEvent e) {
            //throw new UnsupportedOperationException("Not supported yet.");
        }
    
        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 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();
                }
            });
        }
    }
    Last edited by esgol; 12-28-2010 at 01:13 PM.

  3. #23
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Yes, that's the general idea, though on an unrelated note, I would advise against mixing AWT components (i.e., Button) with Swing components (i.e., JButtons and everything else).

  4. #24
    esgol is offline Member
    Join Date
    Dec 2010
    Posts
    29
    Rep Power
    0

    Default

    JButons seem more flexible i woulf prefere them but how do i put them to the listener since they are Strings?

    Also. How can i specific the side of the panel here?
    I tried
    frame.setBounds(450,250,380,300);
    demo.setBounds(450,250,380,300);
    and generaly everywhere but nothing, only the position 450,250 was heard as command

    how do i tell "him" that i want a 380X300 size window? :S

    All else like background color and foreground seems to work here too :SS

  5. #25
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by esgol View Post
    JButons seem more flexible i woulf prefere them but how do i put them to the listener since they are Strings?
    ???
    I don't understand what you're saying here.


    Also. How can i specific the side of the panel here?
    I tried
    frame.setBounds(450,250,380,300);
    demo.setBounds(450,250,380,300);
    and generaly everywhere but nothing, only the position 450,250 was heard as command
    Have you tried setPreferredSize(new Dimension(380, 300))?

    In general you should avoid setting bounds and instead use the layout managers.

  6. #26
    esgol is offline Member
    Join Date
    Dec 2010
    Posts
    29
    Rep Power
    0

    Default

    >>if (e.getSource()==BtGotoCard2)
    >> {
    >> System.out.println("pathitike to Card2");
    >> CardLayout cl = (CardLayout)(cards.getLayout());
    >> cl.show(cards,"Card 2" );
    >> }

    If the button was a JButton
    >>card1.add(new JButton("Button 1"));
    i wouldnte be able to set to the listener
    >>if (e.getSource()=="Button 1")

    thats what i was saing. How do u put JButtons into the listener. ( omg im such a noob :S )

    edit: and also i cant specify bounds for the buttons neither JButtons nor awt buttons. :S
    It has something like flowLayout by default :S
    setLayout(null);
    Destroys everything ( Canimagine why hahaha im in a CardLayout loool )
    P.S.
    OMG setPreferredSize(new Dimension(380, 300)) WORKS FINE!! :D
    and set ounds works fine for the position :D so im done with the window's position and size :D
    Last edited by esgol; 12-28-2010 at 04:21 PM.

  7. #27
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by esgol View Post
    thats what i was saing. How do u put JButtons into the listener. ( omg im such a noob :S )
    I often use anonymous inner classes for my button listeners or private inner classes. e.g.,

    Java Code:
    import java.awt.event.*;
    import javax.swing.*;
    
    public class SwingFu extends JPanel {
       
       public SwingFu() {
          JButton myButton = new JButton("My Button");
          myButton.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                myButtonActionPerformed(e);
             }
          });
          
          add(myButton);
       }
    
       private void myButtonActionPerformed(ActionEvent e) {
          System.out.println("My Button has been pushed");
          System.out.println("the ActionEvent's actionCommand is: " + e.getActionCommand());
       }
    
       private static void createAndShowUI() {
          JFrame frame = new JFrame("SwingFu");
          frame.getContentPane().add(new SwingFu());
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                createAndShowUI();
             }
          });
       }
    }

    edit: and also i cant specify bounds for the buttons neither JButtons nor awt buttons. :S
    It has something like flowLayout by default :S
    setLayout(null);
    Destroys everything ( Canimagine why hahaha im in a CardLayout loool )
    P.S.
    OMG setPreferredSize(new Dimension(380, 300)) WORKS FINE!! :D
    and set ounds works fine for the position :D so im done with the window's position and size :D
    There are many other layout managers to use. We often nest JPanels each one using a different layout manager.

  8. #28
    esgol is offline Member
    Join Date
    Dec 2010
    Posts
    29
    Rep Power
    0

    Default

    hmm thank you very much and hope it works.

    And if i want to change the position of a button when i have card layout?

    It seems like if they are stuck with a centered FlowLayout

  9. #29
    esgol is offline Member
    Join Date
    Dec 2010
    Posts
    29
    Rep Power
    0

    Default

    Come oon.. this is the last thing I need to know :(

    Also Fubarable I've managed to recognize the JButton form Action listener eith the ... if getsource= .... style like this

    JButton myButton = new JButton("My Button"); // OUTSIDE the constructor as a parametre of the class like the awt buttons

    inside the constructor
    myButton.addActionListener(this);

    and then its recognisable to the action listener outside
    Last edited by esgol; 12-31-2010 at 10:53 AM.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Replies: 3
    Last Post: 03-20-2010, 04:03 AM
  2. Replies: 3
    Last Post: 10-12-2009, 02:03 PM
  3. Not-resizable (J)dialog showing Maximize Menu !!!
    By playwin2 in forum AWT / Swing
    Replies: 7
    Last Post: 09-08-2008, 12:51 AM
  4. Replies: 7
    Last Post: 05-09-2008, 07:54 AM
  5. main window & print dialog
    By roo7 in forum Advanced Java
    Replies: 3
    Last Post: 01-02-2008, 06:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •