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

    Unhappy How to create a menu dialog into a single window?

    Good evenings :)

    I couldnt find this answer no matter how much i searched in google in several combinations.

    I want to create a window which contains a menu with the options found in an ATM machine.

    Ok i put the awt buttons from swing but the action from each button has to erase the curent menu from the window and paint a new menu ( the next menu ) e.g you press "transaction" ie has to sent u to the menu with the available amounts to subtract from the account ( 20, 40 100 200.. )

    how can i do this?
    My friend managed to make it by opening a new window into the action with each press of button. But like this he had a new window each time for each sub-menu

    A "wizard" function i found in a java book wasnt what i need it.

    How can i make it in a single window?

    Can someone tell me what i have to use ori even give a sample code of a window with 2 buttons where u press the 1st and shows u a new combination of awt elements [IN THE SAME WINDOW]
    This simple is all i need :S i will understand then

    ( I tried to make it by calling a new window class in main after the 1st but here is not C++ ahaha XD )

    THANK YOU FOR THE TIME! :D

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

    Default

    Merd, I dont think I've asked much :((

    It must be from the simples thing in JAva for experienced people here. :(

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

    Default

    You likely want to create different JPanels each with their own JButtons, and then swap them in the GUI using a CardLayout. One Container, usually a JPanel is the card "holder" and will have its layout set to be CardLayout. The other JPanels will be the Cards, and will be added to the card holder JPanel using the add method along with a String that will indicate which card is which. There's a decent tutorial on how to do this at the Oracle Swing tutorials here: How to use CardLayout

    Luck!

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

    Default

    Hmm yes this lights much the darc

    But i dont want a CardLayout. I want each combination of buttons to be in a different window

    Do i need a different Layout to do this? Something like WindowLayout?

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

    Default

    maybe its more simply achievable.

    I could have each button to be a command to open a new window (its special window) with the same name and close the curent one

    How can this be done?

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,450
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by esgol View Post
    maybe its more simply achievable.

    I could have each button to be a command to open a new window (its special window) with the same name and close the curent one

    How can this be done?
    But in your original post you wrote that you want to do it in a single window; a CardLayout is ideal for that purpose.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    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 JosAH View Post
    But in your original post you wrote that you want to do it in a single window; a CardLayout is ideal for that purpose.
    Yep, agree 100%. How many ATM's do you see where windows close, open and swap? They all have one single window open and swap views in that single window, and in Swing the equivalent of this is swapping JPanels inside of a top level container which is best achieved via the CardLayout.

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

    Default

    oh so i can make it that the cards on the top are not visible but backgound?

    P.S.
    While waitng for response i've hald made it this way

    insite the listener for the button "Take Money" i call a new class which is the window with
    choices of amount ( 20 40 100 150 200 )
    which into his own listener for each button calls another class which puts a new window "Transaction Complete"

    Java Code:
    public void actionPerformed(ActionEvent e) {
            if (e.getSource()==BtAnalipsi)
            {
                System.out.println("pathitike to Analipsi");
                MyWin3Analipsi Window2 = new MyWin3Analipsi();
                dispose();
            }
    * Analipsi=Substraction in Greek XD

    And now in Analipsi Class's Listener for the window
    Java Code:
    public void actionPerformed(ActionEvent e)
        {
            if (e.getSource()==Bt20)
            {
                System.out.println("Lavate 20 evro"); //Lavate=You took
                MyWin4Done Window2 = new MyWin4Done();
                dispose();
            }
    
            if (e.getSource()==Bt40)
            {
                System.out.println("Lavate 40 evro");
                MyWin4Done Window2 = new MyWin4Done();
                dispose();
            }
        }
    Java Code:
    MyWin4Done()
        {
    
            setBounds(300,300,300,300);
            setTitle("H Sinalagh Pragmatopoihtike");
            setVisible(true);
            addWindowListener(this);      //! To thelei gia na kleisei to parathiro
    
        }
    is it easier with a card panel? but there is a way to hide the cards themselfs?
    Last edited by esgol; 12-24-2010 at 06:39 PM. Reason: forgot elemten to write

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,450
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by esgol View Post
    is it easier with a card panel? but there is a way to hide the cards themselfs?
    That's the sole purpose of a CardLayout. You stick a bunch of components in it (normally they're JPanels) and if displays only one of them. Each of your JPanels contains its own buttons, textfields etc. and only the JPanel displayed in the CardLayout displays its own buttons and whatever.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default

    OH I CANTE BELEIVE IT I THOUGH I HAD POSTED IT YESTERDAY NIGHT. FORGOT TO CLICK "POST REPLY!!

    """""""""'''
    ahh... got it

    ok but the tabs musnt be seenable from the user. They have to exist only as an internal mechanism of the class
    """"""""""

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,197
    Rep Power
    19

    Default

    I see you still haven't gone through the link posted by Fubarable at #2. I can only assume that any further help will be similarly ignored.

    db

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,450
    Blog Entries
    7
    Rep Power
    20

    Default

    [QUOTE=esgol;165823]ok but the tabs musnt be seenable from the user. They have to exist only as an internal mechanism of the class

    Which tabs? Nobody mentioned any tabs ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default

    Here its where each tabs is added in the example
    Java Code:
            tabbedPane.addTab(BUTTONPANEL, card1);
            tabbedPane.addTab(TEXTPANEL, card2);
    i want the 2 JPanels to exist but not to be seen as Tabs
    so i will manage to have an "ATM like" interface

    However if i delete the lines the entire panels arent add. How is it done? Some kind of setvisible=false specified to the tabs? ( omg i feel i just sed an idiotness :SS )


    Java Code:
    //package layout;
    
    /*
     /*
     * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
     */
    
    // * TabDemo.java
    
    import java.awt.*;
    import javax.swing.*;
    
    public class SimpleFrame {
        final static String BUTTONPANEL = "Tab with JButtons";
        final static String TEXTPANEL = "Tab with JTextField";
        final static int extraWindowWidth = 100;
    
        public void addComponentToPane(Container pane) {
            JTabbedPane tabbedPane = new JTabbedPane();
    
            //Create the "cards".
            JPanel card1 = new JPanel() {
                //Make the panel wider than it really needs, so
                //the window's wide enough for the tabs to stay
                //in one row.
                public Dimension getPreferredSize() {
                    Dimension size = super.getPreferredSize();
                    size.width += extraWindowWidth;
                    return size;
                }
            };
            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));
    
            tabbedPane.addTab(BUTTONPANEL, card1);
            tabbedPane.addTab(TEXTPANEL, card2);
    
            pane.add(tabbedPane, BorderLayout.CENTER);
        }
    
        /**
         * Create the GUI and show it.  For thread safety,
         * this method should be invoked from the
         * event dispatch thread.
         */
        private static void createAndShowGUI() {
            //Create and set up the window.
            JFrame frame = new JFrame("TabDemo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            //Create and set up the content pane.
            SimpleFrame demo = new SimpleFrame();
            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-25-2010 at 10:54 AM. Reason: Deleted copyright info from oracle

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,450
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by esgol View Post
    Here its where each tabs is added in the example
    Java Code:
            tabbedPane.addTab(BUTTONPANEL, card1);
            tabbedPane.addTab(TEXTPANEL, card2);
    i want the 2 JPanels to exist but not to be seen as Tabs
    so i will manage to have an "ATM like" interface

    However if i delete the lines the entire panels arent add. How is it done? Some kind of setvisible=false specified to the tabs? ( omg i feel i just sed an idiotness :SS )
    Nobody mentioned a JTabbedPane, we all suggested a CardLayout; do as Fubarable suggested: create a JPanel with a CardLayout and add all your other JPanels (containing the buttons etc.) to it. Add the 'master' JPanel to the content pane of your JFrame; there will be no tabs at all.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default

    My lord, but this one's a bit on the stubborn side. OP, why are you not using CardLayout as has been recommended all throughout this thread?
    Last edited by Fubarable; 12-25-2010 at 04:26 PM.

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

    Default

    I managed to do it in a more complex way.

    When a button is pressed into the listener another java class is called which contains the code for the creation of a new window.

    Also after that call i use the command dispose(); to close the curent ( last ) window.

    However this method has the weakness that YOU MUSTN MOVE YOUR WINDOW FROM THE ORIGINL POSITION cause all windows are setbounds in that specific position and so if it is in the center and u move it in the wright the next window will appear in the center too.

    Messy you are wright Fubarable but just because simply i havent undrestood how is this done with card layout and-since noone gave an example piece of code of how to make it so.

    Java Code:
     public void actionPerformed(ActionEvent e) 
    {
     if (e.getSource()==BtAnalipsi)
            {
                System.out.println("pathitike to Katathesi");
                AnalipsiWindow Window2 = new AnalipsiWindow();
                dispose();
            }
    }

    Down here I put as an example the code for the 1st window and the class that will be called to proceed to the next window.

    However since this is a semestral project for a course of Interaction in
    "Athens Technological Institute - Informatics Department" in Greece
    I'm gonna post it by edit after the deadline submisson date after January

    My contribution to the forum, whereas the minimal ( and full of Prepossession ) support i recived like if my username had a tag on it (This one is a lazy dog who searches a ready exercise for copy-paste )


    THE MAIN CLASS
    (Enter PIN window )
    Java Code:
    //Will be pasted here after January when project is delivered
    WinEidos.java
    (Window for type of account )
    Java Code:
    //Will be pasted here after January when project is delivered
    So Just for curiosity, can someone copy-paste an example of how could I do it with CardLayout?
    by having e.g window with OK and when i press it to change the Card to the AccountTypeChoose which has 2 buttons
    It will also add to the thread's usefullness

    P.S.
    Tags for future Google-forum searches since i couldn find a similar thread

    MAKE CREATE WINDOW MENU DIALOG SINGLE WINDOW IN JUST ONE WINDOW SAME WINDOW
    Last edited by esgol; 12-27-2010 at 08:38 AM.

  17. #17
    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
    I managed to do it in a more complex way.
    And wrong way.



    When a button is pressed into the listener another java class is called which contains the code for the creation of a new window.

    Also after that call i use the command dispose(); to close the curent ( last ) window.

    However this method has the weakness that YOU MUSTN MOVE YOUR WINDOW FROM THE ORIGINL POSITION cause all windows are setbounds in that specific position and so if it is in the center and u move it in the wright the next window will appear in the center too.
    This is a complete kludge. If you'r smart, you'll get rid of this code.


    Messy you are wright Fubarable but just because simply i havent undrestood how is this done with card layout and-since noone gave an example piece of code of how to make it so.
    There's a great example of using CardLayout in the Swing tutorials, and many examples posted in this forum that you can easily find by searching. Given this abundance, we don't need to re-write the tutorials for you or write your code for you. The better thing for you to do is to study the tutorials and the examples and then try it yourself, and if it doesn't work post your attempt here and any error messages you may be receiving. Then we can help fix your code. Don't just throw your hands up and give up on the correct solution because you don't understand it yet.


    So Just for curiosity, can someone copy-paste an example of how could I do it with CardLayout?
    We'll help you, but first give it a try yourself and post the attempt here as this is really the best way to learn. But don't do it with your huge program to start out with. Instead just create a small example CardLayout program that is similar but much simpler than your big program.

    Luck.

  18. #18
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,450
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by esgol View Post
    Tags for future Google-forum searches since i couldn find a similar thread

    MAKE CREATE WINDOW MENU DIALOG SINGLE WINDOW IN JUST ONE WINDOW SAME WINDOW
    Some more tags: CardLayout CardLayout CardLayout CardLayout CardLayout CardLayout CardLayout CardLayout (but the OP won't listen).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default

    Hmm ok I've took the cardlyout demo example from oracle and put into coment the code that creates the combo box.

    But how can i create a button and put a listener to it so that i change card if its pressed?

    addActionListener(this) doesnt works at all! :SSS

    Java Code:
    /*
     * CardLayoutDemo.java
     *
     */
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class CardLayoutDemo implements ItemListener {
        JPanel cards; //a panel that uses CardLayout
        Button BtOK;
        //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"));
    
            JPanel card2 = new JPanel();
            card2.add(new JTextField("TextField", 20));
            card2.add(new JButton("Button 3"));
    
            //Create the panel that contains the "cards".
            cards = new JPanel(new CardLayout());
            cards.add(card1, "Button 1");
            cards.add(card2, "Button 3");
    
            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());
        }
    
    
    
        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();
                }
            });
        }
    }

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

    Default

    Quote Originally Posted by esgol View Post
    addActionListener(this) doesnt works at all! :SSS
    Of course it doesn't because your class doesn't implement the ActionListener interface; please think before you randomly try to add more random code you don't understand.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Page 1 of 2 12 LastLast

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
  •