Results 1 to 7 of 7
Like Tree2Likes
  • 2 Post By KevinWorkman

Thread: JButton to open new Page

  1. #1
    knightwriter is offline Member
    Join Date
    Nov 2011
    Posts
    49
    Rep Power
    0

    Default JButton to open new Page

    I've looked through the threads here regarding my problem but can't seem to quite get the information I need. I have a frame with various buttons that, when clicked, need to open up a new and specific page. I got this to work with just one button, however, implementing all the buttons as actionlisteners and then including them in the actionperformed method is giving me troubles. Any help is greatly appreciated. Here is my code:

    Java Code:
    //This is the class that shows the main screen upon starting the program.
    
    package desktop.application;
    
    import java.awt.FlowLayout;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.awt.event.*;
    
    
    
    
    
    //Extend JFrame means that this class is going to inherit the JFrame class attributes.
    public class MainScreen extends JFrame implements ActionListener {
        
     
        
     public  void mainscreen(){
          //This is the attributes for the main form
           setTitle("Main Screen");
           setLayout(new FlowLayout());
           setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           setSize(275,180);
           setVisible(true);
           
           //These are the components that are on the main screen.
            JLabel item1 = new JLabel("# of projects working on now ");
            add(item1);
            JTextField item3 = new JTextField(5);
            add(item3);
            JLabel item2 = new JLabel("# of potential projects on bid now ");
            add(item2);
            JTextField item4 = new JTextField(5);
            add(item4);
            JButton item5 = new JButton("Go to Quotes Screen");
            add(item5);
            JButton item6 = new JButton("Go to Bids Screen");
            add(item6);
            JButton item7 = new JButton("Work In Progress");
            add(item7);
            JButton item8 = new JButton("Exit Program");
            add(item8);
            
            //This should set the listener and action event for the buttons
            
            item5.addActionListener(this);
            item6.addActionListener(this);
            
     }
        
        
       
        public void actionPerformed(ActionEvent e){
            JButton item5 = new JButton();
            
           // Object source = e.getSource();
            if(e.getSource() == item5){
                QuotesScreen quote = new QuotesScreen();
                quote.quotes();}
            
           
            }
        
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,932
    Rep Power
    8

    Default Re: JButton to open new Page

    Why are you declaring a new JButton inside your ActionListener? You should be comparing the source to the JButtons to which you add the ActionListener. Alternatively, you could just add a different ActionListener to each JButton and not worry about the source.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    knightwriter is offline Member
    Join Date
    Nov 2011
    Posts
    49
    Rep Power
    0

    Default Re: JButton to open new Page

    Netbeans gives me an error of cannot find symbol for item5, and suggest making a local variable. I am planning on putting new actionlisteners for each button. (See lines 51 and 52.) Then I was using "if" to call the proper button in the actionperformed method. (Not sure, but I hope that answers your question and helps in answering mine.)

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,932
    Rep Power
    8

    Default Re: JButton to open new Page

    You shouldn't just go with what your IDE suggests. This is actually one of the downsides to using an IDE as a beginner- in this case, your IDE was completely wrong about what to do. I really suggest using a simple editor and the command prompt until you really understand what the compiler errors mean.

    Get rid of the item5 declared inside your ActionListener. The error you'll see then is simply telling you that it can't "see" any variable named item5. It can't see item5 because item5 is declared inside another method.
    Fubarable and DarrylBurke like this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    knightwriter is offline Member
    Join Date
    Nov 2011
    Posts
    49
    Rep Power
    0

    Default Re: JButton to open new Page

    I did as you said and it won't let run the program. Is my actionperformed method correct?

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,932
    Rep Power
    8

    Default Re: JButton to open new Page

    I wouldn't expect it to run the program, as you have a compiler error you have to resolve. I've told you the cause of the error, and I've given you one solution to your problem. Another solution is to make sure you declare your variables in the proper scope- if you need to use a variable outside of a method (rather, inside multiple methods), don't declare the variable inside a single method.

    The basic tutorials, which should be your first stop, go over this: Declaring Member Variables (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    knightwriter is offline Member
    Join Date
    Nov 2011
    Posts
    49
    Rep Power
    0

    Default Re: JButton to open new Page

    Thank you for your help Kevin, I will do all of these things shortly. I appreciate your help very much!

Similar Threads

  1. Replies: 2
    Last Post: 09-05-2011, 12:38 AM
  2. Open JPanel with JButton
    By web_dev in forum New To Java
    Replies: 7
    Last Post: 06-13-2011, 10:25 PM
  3. open text document using JButton
    By coopc in forum New To Java
    Replies: 3
    Last Post: 05-04-2011, 12:02 AM
  4. How To Open a JFrame Form from a jButton
    By Lyricid in forum AWT / Swing
    Replies: 12
    Last Post: 03-28-2010, 06:53 AM
  5. how to click a jbutton and open an url
    By katie in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 10:44 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
  •