Results 1 to 5 of 5
  1. #1
    esgol is offline Member
    Join Date
    Dec 2010
    Posts
    29
    Rep Power
    0

    Default How to change position of JButtons in a CardLayout?

    Java Code:
    ...
    
        JPanel cards; //a panel that uses CardLayout
        Button BtGotoCard2; //awt button for test BUT DOESNT CHANGES POSITION EITHER
        Button BtGotoCard1;
        JButton myButton = new JButton("JB Go to Card2");
    
    public void addComponentToPane(Container pane) {
    
            
    
            JPanel card1 = new JPanel();
            BtGotoCard2 = new Button("Go to Card2");      
            [COLOR="Red"]BtGotoCard2.setBounds(50,150,80,20);[/COLOR]
            [COLOR="Red"]myButton.setBounds(50,150,80,20);[/COLOR]
    
            card1.add(BtGotoCard2);
            card1.add(new JButton("J Button 1"));
            card1.add(myButton);
            myButton.addActionListener(this);
            BtGotoCard2.addActionListener(this);
    
            BtGotoCard1 = new Button("Go to Card1");       
    
            card1.add(BtGotoCard1);
    
            BtGotoCard1.addActionListener(this);
    
    
            JPanel card2 = new JPanel();
            card2.add(new JTextField("TextField", 20)).setBackground(LightBez);
            card2.add(BtGotoCard1);
    
    
            //Create the panel that contains the "cards".
            cards = new JPanel(new CardLayout());
            cards.add(card1, "Enter PIN");
            cards.add(card2, "Eidos Logariasmou");
    
    
            pane.add(cards);
            
    
        }
    And when the function is called

    Java Code:
    
    private static void createAndShowGUI() {
            //Create and set up the window.
            JFrame frame = new JFrame("CardLayoutDemo");
            frame.setBounds(450,250,380,300);
            frame.setPreferredSize(new Dimension(380, 300));
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            //Create and set up the content pane.
            CardLayoutDemo demo = new CardLayoutDemo();
            //demo.setBounds(450,250,380,300);
            demo.addComponentToPane(frame.getContentPane());
    
            //Display the window.
            frame.pack();
            frame.setVisible(true);
        }
    Set bounds works with the frame ( even if not for the size) but not for the JButtons. What can i do?
    Last edited by esgol; 12-31-2010 at 11:04 AM. Reason: styling

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,515
    Blog Entries
    7
    Rep Power
    20

    Default

    A CardLayout sets each of its components ("cards") to the same size. If those components are, say, JPanels, you can add other components to them using any LayoutManager your want (even none). Those other components can be JButtons or whatevev. b.t.w. don't use Buttons, use JButtons instead.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default

    Even None and it will work?
    I've imagine tha all other elements inheritate the cardlayout's layout

    So i can put other elements by specifing none layout? And i will be freed from the bounds of the cardlayout?

    Like doing

    myButton.setLayout(null);
    myButton.setBounds(50,150,80,20);
    card1.add(myButton);

    I ve done it inside the constructor but its stil bound to the cardlayout

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,515
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by esgol View Post
    Even None and it will work?
    I've imagine tha all other elements inheritate the cardlayout's layout

    So i can put other elements by specifing none layout? And i will be freed from the bounds of the cardlayout?

    Like doing

    myButton.setLayout(null);
    myButton.setBounds(50,150,80,20);
    card1.add(myButton);

    I ve done it inside the constructor but its stil bound to the cardlayout
    If you add your myButton to the JComponent with the CardLayout it will have the size of the entire component. It doesn't matter whether or not you've set the LayoutManager of the JButton to null. Add some JPanels (without a LayoutManager) to the JComponent with the CardLayout (also a JPanel?) and add components to the other JPanels. Use the CardLayout to display one of the JPanels. So:

    Java Code:
    CardLayout cards= new CardLayout();
    JPanel mainPanel= new JPanel(cards); // create the main panel
    
    JPanel panel1= new JPanel(null); // create two cards
    JPanel panel2= new JPanel(null);
    
    mainPanel.add(panel1, "panel1"); // add them to the main panel
    mainPanel.add(panel2, "panel2");
    
    // add other components to panel1 and panel2 here
    // note that the panels don't have a LayoutManager
    ...
    // use cards.show(mainPanel, "panel1") to show panel1
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default

    IT WORKS! :D IT WORKS!!! :D :D :D

    Java Code:
    //CONSTRUCTOR TYPES DECLARATION
    CardLayout cards= new CardLayout(); //a panel that uses CardLayout
        JPanel MainPanel= new JPanel(cards); // create the main panel
    
        
    
    
        Button BtGotoCard2;
        Button BtGotoCard1;
        JTextField TextField1 = new JTextField("TextField1", 20);
    
    public void addComponentToPane(Container pane) 
    {
    ...
            JPanel card1 = new JPanel(null);
            MainPanel.add(card1, "Enter PIN");
    
            BtGotoCard2 = new Button("Go to Card2");  
            BtGotoCard2.addActionListener(this);     
            BtGotoCard2.setBounds(50,50,80,20);
            myButton.setBounds(150,100,80,20);
    
            card1.add(BtGotoCard2);
            card1.add(myButton);
    
    //------------------------------------
    
            JPanel card2 = new JPanel(null);
            MainPanel.add(card2, "Eidos Logariasmou");
    
            BtGotoCard1 = new Button("Go to Card1");      
            BtGotoCard1.setBounds(50,50,80,20);
            TextField1.setBounds(100,100,80,20);
            BtGotoCard1.addActionListener(this);
    
            card2.add(BtGotoCard1);
            card2.add(TextField1);
    
           ...
    
           pane.add(MainPanel);
    }
    And into the Action Listenet Afterwards

    Java Code:
    public void actionPerformed(ActionEvent e) {
            if (e.getSource()==BtGotoCard2)
            {
                System.out.println("pathitike to Card2");
                //CardLayout cl = (MainPanel)(cards.getLayout());
                cards.show(MainPanel, "Eidos Logariasmou");
                //cl.show(cards,"Eidos Logariasmou" );
            }
            
            if (e.getSource()==myButton)
            {
                System.out.println("Anagnoristike pathma myButton apo to if getsource");
                //CardLayout cl = (CardLayout)(cards.getLayout());
                cards.show(MainPanel, "Eidos Logariasmou");
            }
            if (e.getSource()==BtGotoCard1)
            {
                System.out.println("pathitike to GotoCard1");
                //CardLayout cl = (CardLayout)(cards.getLayout());
                cards.show(MainPanel, "Enter PIN");
            }
        }
    NOW I'M GONNA HAVE A SUPER ATM INTERFACE :D :D :D :D

    THANK YOU ALL OF YOU :D

Similar Threads

  1. How to I change the position in arrays
    By LennyKosmos in forum New To Java
    Replies: 3
    Last Post: 09-27-2010, 10:04 PM
  2. Change the Font Size and Text Position
    By stayfierce in forum New To Java
    Replies: 1
    Last Post: 12-03-2009, 05:17 AM
  3. get position in string from caret position
    By helloworld111 in forum AWT / Swing
    Replies: 5
    Last Post: 02-19-2009, 01:36 AM
  4. Regarding CardLayout
    By adeeb in forum AWT / Swing
    Replies: 1
    Last Post: 06-07-2008, 07:52 PM
  5. how to change the appearance of jbuttons
    By katie in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 10:26 PM

Posting Permissions

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