need help with jbutton and new jframe

Reply

Join Date: Jun 2007
Posts: 8
Reputation: dajiebuda is an unknown quantity at this point 
Solved Threads: 0
dajiebuda dajiebuda is offline Offline
Newbie Poster

need help with jbutton and new jframe

 
0
  #1
Jun 16th, 2007
I'm learning java.

I have one Jframe with 5 jbuttons. I want to click button1 to have a new Jframe come up. How should I define the 2nd Jframe? class? public class?
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 59
Reputation: Chaster is an unknown quantity at this point 
Solved Threads: 3
Chaster Chaster is offline Offline
Junior Poster in Training

Re: need help with jbutton and new jframe

 
0
  #2
Jun 16th, 2007
Hi!
You could define a method, let's say popUp():
  1. private void popUp()
  2. {
  3. JFrame frame = new JFrame("aName");
  4. frame.setSize(500, 500);
  5. frame.setVisible(true);
  6. }
Now, you should have an actionPerformed method:
  1. public void actionPerformed(ActionEvent event)
  2. {
  3. if (event.getSource() == button1)
  4. {
  5. popUp();
  6. }
  7. }
Assuming, you have defined the following line:
  1. button1.addActionListener(this);
Oh, and one more thing, your class has to implement the ActionListener interface.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 8
Reputation: dajiebuda is an unknown quantity at this point 
Solved Threads: 0
dajiebuda dajiebuda is offline Offline
Newbie Poster

Re: need help with jbutton and new jframe

 
0
  #3
Jun 16th, 2007
Thanks a lot!
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 83
Reputation: ProgrammersTalk is an unknown quantity at this point 
Solved Threads: 7
ProgrammersTalk's Avatar
ProgrammersTalk ProgrammersTalk is offline Offline
Junior Poster in Training

Re: need help with jbutton and new jframe

 
0
  #4
Jun 17th, 2007
Originally Posted by dajiebuda View Post
I'm learning java.

I have one Jframe with 5 jbuttons. I want to click button1 to have a new Jframe come up. How should I define the 2nd Jframe? class? public class?
you just need to add Action Litener to your button, and use JPanel there, if that's ok for you... i guess that's the easiest way. Here's link if you want to know how to add action listener..

<URL snipped>

you can always use http://java.sun.com/ to search for the tutorial and APIs
Last edited by happygeek; Jun 18th, 2007 at 6:05 am. Reason: Keep it on the site please rather than referring users to other forums for an answer
The ProgrammersTalk Community | Programming & Marketing | Buying & Selling Script
Hang out place of novice and intermediate programmers
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 6
Reputation: hrlygrl923 is an unknown quantity at this point 
Solved Threads: 0
hrlygrl923 hrlygrl923 is offline Offline
Newbie Poster

Re: need help with jbutton and new jframe

 
0
  #5
Mar 12th, 2008
It compliles and runs but does not do anything when i click on the button




import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import java.text.DecimalFormat;
import java.io.*;
import javax.swing.JOptionPane;
import java.lang.*;
import java.math.*;

public class last_try implements ActionListener
{

public static void main(String[] args)
{

JFrame f = new JFrame("What would you like to do");
f.setSize(400, 150);
Container content = f.getContentPane();
content.setBackground(Color.white);
content.setLayout(new FlowLayout());


JButton button1 = new JButton("Button 1");
JButton button2 = new JButton("Button 2");
JButton button3 = new JButton("Button 3");

Container contentPane = f.getContentPane();
contentPane.setLayout(new FlowLayout());
contentPane.add(button1);
contentPane.add(button2);
contentPane.add(button3);


f.addWindowListener(new ExitListener());
f.setVisible(true);
}

public void popUp()
{
JFrame frame = new JFrame("aName");
frame.setSize(500, 500);
frame.setVisible(true);

}


public void actionPerformed(ActionEvent event)
{

if (event.getSource() == "Button 1")
{
JOptionPane.showMessageDialog(null, "Your entry was not in the proper format.");
}

}
}
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,803
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: need help with jbutton and new jframe

 
0
  #6
Mar 13th, 2008
Originally Posted by hrlygrl923 View Post
It compliles and runs but does not do anything when i click on the button




import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import java.text.DecimalFormat;
import java.io.*;
import javax.swing.JOptionPane;
import java.lang.*;
import java.math.*;

public class last_try implements ActionListener
{

public static void main(String[] args)
{

JFrame f = new JFrame("What would you like to do");
f.setSize(400, 150);
Container content = f.getContentPane();
content.setBackground(Color.white);
content.setLayout(new FlowLayout());


JButton button1 = new JButton("Button 1");
JButton button2 = new JButton("Button 2");
JButton button3 = new JButton("Button 3");

Container contentPane = f.getContentPane();
contentPane.setLayout(new FlowLayout());
contentPane.add(button1);
contentPane.add(button2);
contentPane.add(button3);


f.addWindowListener(new ExitListener());
f.setVisible(true);
}

public void popUp()
{
JFrame frame = new JFrame("aName");
frame.setSize(500, 500);
frame.setVisible(true);

}


public void actionPerformed(ActionEvent event)
{

if (event.getSource() == "Button 1")
{
JOptionPane.showMessageDialog(null, "Your entry was not in the proper format.");
}

}
}
See post # 2. You never added the action listeners to the buttons. You may have to rearrange/redesign some code and add those action listeners outside of the main function due to the fact that main is static.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 6
Reputation: hrlygrl923 is an unknown quantity at this point 
Solved Threads: 0
hrlygrl923 hrlygrl923 is offline Offline
Newbie Poster

Re: need help with jbutton and new jframe

 
0
  #7
Mar 13th, 2008
Thanks. I tried that I just can not figure it out
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,803
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: need help with jbutton and new jframe

 
0
  #8
Mar 13th, 2008
I had something like this in mind:

  1.  
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5. import javax.swing.text.*;
  6. import java.text.DecimalFormat;
  7. import java.io.*;
  8. import javax.swing.JOptionPane;
  9. import java.math.*;
  10.  
  11. public class last_try implements ActionListener
  12. {
  13. // put variable declarations here (i.e. code below)
  14. JFrame f;
  15. JButton button1;
  16. JButton button2;
  17. // etc. for other variables
  18.  
  19.  
  20. public last_try() // new constructor. No longer have "static" limitiations
  21. {
  22. // code here that uses the variables declared above
  23. // see code below
  24.  
  25.  
  26. f = new JFrame("What would you like to do");
  27. f.setSize(400, 150);
  28. // more code to set up GUI
  29.  
  30. button1 = new JButton("Button 1");
  31. button2 = new JButton("Button 2");
  32. button3 = new JButton("Button 3");
  33.  
  34. button1.addActionListener(this); // add action listeners here
  35. button2.addActionListener(this);
  36.  
  37. // more code
  38.  
  39.  
  40. // need more code to use the line below, so commented out.
  41. // Are you sure you really want it?
  42. // f.addWindowListener(new ExitListener());
  43.  
  44. f.setVisible(true);
  45. }
  46.  
  47. public static void main(String[] args)
  48. {
  49. last_try last = new last_try();
  50. }
  51.  
  52. public void popUp()
  53. {
  54. JFrame frame = new JFrame("aName");
  55. frame.setSize(500, 500);
  56. frame.setVisible(true);
  57. }
  58.  
  59. public void actionPerformed(ActionEvent event)
  60. {
  61. System.out.println("In actionPerformed");
  62.  
  63. if (event.getSource() == button1) // no quotes here
  64. {
  65. JOptionPane.showMessageDialog(null, "Your entry was not in the proper format.");
  66. }
  67. }
  68. }

Note that there are no quotes around "button1" in line 63 and that it matches line 15 and that an action listener was added to button1 in line 34. Main is very small and only calls a constructor. The constructor does not have the "static" limitations that that "main" has, so you will have fewer problems using worlds like "new". Note that the variables are declared OUTSIDE of the constructor. This makes them global and hence usable by the action listener, which they may not have been had variables like "button1" been declared in "main" or in the constructor. Try to add the code where I put the comments and see if it you can get it to work. Post back with more questions if you cannot.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1
Reputation: Rajnish1349 is an unknown quantity at this point 
Solved Threads: 0
Rajnish1349 Rajnish1349 is offline Offline
Newbie Poster

Re: need help with jbutton and new jframe

 
0
  #9
Aug 10th, 2008
my frame is working well but it freezes in the middle. What may be the reason.?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,803
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: need help with jbutton and new jframe

 
0
  #10
Aug 10th, 2008
Originally Posted by Rajnish1349 View Post
my frame is working well but it freezes in the middle. What may be the reason.?

You should start a new thread. Post the code and describe the exact problem and the circumstances under which the problems occurs. Any answer given here would be pure speculation since you haven't posted the code.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC