Results 1 to 7 of 7
Thread: JButton to open new Page
- 02-27-2012, 05:06 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 49
- Rep Power
- 0
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();} } }
- 02-27-2012, 05:12 PM #2
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!
- 02-27-2012, 05:17 PM #3
Member
- Join Date
- Nov 2011
- Posts
- 49
- Rep Power
- 0
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.)
- 02-27-2012, 05:32 PM #4
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.How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 02-27-2012, 06:55 PM #5
Member
- Join Date
- Nov 2011
- Posts
- 49
- Rep Power
- 0
Re: JButton to open new Page
I did as you said and it won't let run the program. Is my actionperformed method correct?
- 02-27-2012, 06:59 PM #6
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!
- 02-27-2012, 07:03 PM #7
Member
- Join Date
- Nov 2011
- Posts
- 49
- Rep Power
- 0
Similar Threads
-
Help with using a jbutton actionlistener to open a text file
By BlackBalloon in forum New To JavaReplies: 2Last Post: 09-05-2011, 12:38 AM -
Open JPanel with JButton
By web_dev in forum New To JavaReplies: 7Last Post: 06-13-2011, 10:25 PM -
open text document using JButton
By coopc in forum New To JavaReplies: 3Last Post: 05-04-2011, 12:02 AM -
How To Open a JFrame Form from a jButton
By Lyricid in forum AWT / SwingReplies: 12Last Post: 03-28-2010, 06:53 AM -
how to click a jbutton and open an url
By katie in forum AWT / SwingReplies: 1Last Post: 08-06-2007, 10:44 PM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks