Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Help with opening new classes

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with opening new classes

    Hi,

    I haven't looked at Java for a while. Can someone give me a hand with the following please.

    I have added a part of my program below, I just need to close the class and open a new one when a button is pressed if someone can help with that.


    // Create Stockroom button
    JButton btnStockroom = new JButton("Stockroom");
    // Add event handler for OK button
    btnStockroom.addActionListener(
    new ActionListener(){
    public void actionPerformed(ActionEvent e) {
    JOptionPane.showMessageDialog(frame,
    "You've selected the Stockroom"
    );



    }

    Thanks in advance

    Jono


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,116
    My Mood
    Mellow
    Thanks
    244
    Thanked 755 Times in 741 Posts

    Default Re: Help with opening new classes

    Not sure what you mean by "close the class and open a new one." Can you describe what you're trying to do in simpler terms, like: When the button is pressed I want to display a dialog box that says "You just pushed the "I'm with stupid" button."

    Or, describe what's wrong with the code you posted. What errors are you getting (post them)? Or describe what it does or does not do that is contrary to what you'd like to happen.

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with opening new classes

    Sorry, so I want the application to display a message saying "You've selected the Stockroom" and then when the user clicks OK on that dialog box it closes the original screen that had the buttons on it and opens a new one which is specific to if that button is pressed.

    Thanks

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,116
    My Mood
    Mellow
    Thanks
    244
    Thanked 755 Times in 741 Posts

    Default Re: Help with opening new classes

    To get this clear: You have an interface that begins by showing the user a dialog with buttons that present some number of options. One of the options presented is "Stockroom." When the user selects one of the options, say "Stockroom," another dialog opens that says, "You have chosen the Stockroom." So at this point there are two dialogs open, the first with the buttons to select which option, the second indicates which option was chosen. Correct so far?

    Interlude - Why open a dialog that tells the user what they just did? Is it meant to be a confirmation dialog, ensuring that the user really wanted to select "Stockroom?" If so, I would expect there are two buttons with the second dialog, an OK button and a Cancel button. If the Cancel button is chosen, the second dialog would close, returning to the first dialog so that the user can make the correct choice. Is that right?

    Picking it back up from having the two dialogs open, if the user selects OK on the "You have chosen the Stockroom" dialog, you would like both of those dialogs to close and a third to open, I assume the third dialog has something to do with the Stockroom.

    Do I have it right? If not, please clarify.

  5. #5
    Junior Member
    Join Date
    Sep 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with opening new classes

    Yea that all sounds correct, actually after reading that back it doesn't make sense having the "You have chosen the Stockroom" dialog being displayed. So I will remove that.
    Once a button has been pressed another dialog should replace the existing buttons with more options.

    Thanks

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,116
    My Mood
    Mellow
    Thanks
    244
    Thanked 755 Times in 741 Posts

    Default Re: Help with opening new classes

    Glad to have brought clarity to your thinking, and it sounds that you're now off working a modified design. Come back when you need help with that.

  7. #7
    Junior Member
    Join Date
    Sep 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with opening new classes

    // Create Stockroom button
    JButton btnStockroom = new JButton("Stockroom");
    // Add event handler for Stockroom button
    btnStockroom.addActionListener(
    new ActionListener(){
    public void actionPerformed(ActionEvent e) {
    JOptionPane.showMessageDialog(frame,
    "You've selected the Stockroom"
    );



    }

    Sorry I still seem to have the same issue as when I started this post however. So I have the code above orginally.

    which I will change to:

    // Create Stockroom button
    JButton btnStockroom = new JButton("Stockroom");
    // Add event handler for Stockroom button
    btnStockroom.addActionListener(
    new ActionListener(){
    public void actionPerformed(ActionEvent e) {

    }

    So how do I now get the previous buttons that were on the jframe tobe removed and it to be displayed with some more buttons once the stockroom button is pressed?

  8. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,116
    My Mood
    Mellow
    Thanks
    244
    Thanked 755 Times in 741 Posts

    Default Re: Help with opening new classes

    Adding and/or removing a container's components at runtime can be messy and have unexpected and unpleasant consequences. Why don't you check out CardLayout? It was made for what I think you're trying to do, but then I admit that I'm not sure why you want components coming and going.

  9. #9
    Junior Member
    Join Date
    Sep 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with opening new classes

    That's absolutely perfect for what I need. Thanks a lot

Similar Threads

  1. Opening ports
    By frozen java in forum Java Theory & Questions
    Replies: 0
    Last Post: August 2nd, 2012, 11:16 PM
  2. Error when opening eclipse
    By BlackJavaCoder in forum Java IDEs
    Replies: 4
    Last Post: September 3rd, 2011, 08:57 AM
  3. Help me my IDE is not opening
    By Pms.java in forum Java IDEs
    Replies: 4
    Last Post: August 31st, 2011, 06:11 PM
  4. Opening a new frame on close.
    By Zein in forum Java Theory & Questions
    Replies: 1
    Last Post: August 24th, 2011, 04:25 PM
  5. Opening a File for User
    By aussiemcgr in forum Java Theory & Questions
    Replies: 3
    Last Post: July 29th, 2010, 04:00 AM