Results 1 to 2 of 2
  1. #1
    knightwriter is offline Member
    Join Date
    Nov 2011
    Rep Power

    Default What is a good design to click button and go to new page?

    I have created two jframes and have buttons that are actionlisteners. Each button should go to a different page of the application. However, I realized that the way that I'm creating my event handling, every time a user would press the button to go to the new screen they can create a new instantiation of the new screen object. Obviously I only want a single instantiation of each page, but I'm not sure how to go about that. Here is the code that I have for my actionPerformed method.

    Java Code:
        public void actionPerformed(ActionEvent e){
            Object source =  e.getSource();
            if(source == item1){
                QuotesList quote1 = new QuotesList();
    The user would press this button to go to the quotes list page from the mainscreen. Other buttons on the same jframe will be added to this method by the way.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power

    Default Re: What is a good design to click button and go to new page?

    Consider getting your control code out of your GUI code making the control code more "centralized". Give the GUI's public methods that would allow the control class(es) to change its state. For instance if you're using CardLayout to swap JPanels, consider giving each JPanel a name String (via setName) and using that String when adding the JPanel to the CardLayout-using container. Then give the main GUI a public method, say swapView(String name), that accepts a String and that the control can call to tell the CardLayout to swap views to a different JPanel.

Similar Threads

  1. return value on a button click
    By wotupduck in forum New To Java
    Replies: 1
    Last Post: 03-24-2011, 01:09 PM
  2. Button click GUI question
    By ZambonieDrivor in forum New To Java
    Replies: 2
    Last Post: 11-29-2010, 07:48 AM
  3. SaveAs button click
    By in forum AWT / Swing
    Replies: 2
    Last Post: 07-06-2010, 08:35 AM
  4. Warning: Page has Expired on click of back button
    By bhupendra in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 08-27-2008, 07:42 PM
  5. Replies: 21
    Last Post: 08-01-2008, 11:13 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