943,769 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 5496
  • Java RSS
Jun 16th, 2007
0

need help with jbutton and new jframe

Expand 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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dajiebuda is offline Offline
8 posts
since Jun 2007
Jun 16th, 2007
0

Re: need help with jbutton and new jframe

Hi!
You could define a method, let's say popUp():
Java Syntax (Toggle Plain Text)
  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:
Java Syntax (Toggle Plain Text)
  1. public void actionPerformed(ActionEvent event)
  2. {
  3. if (event.getSource() == button1)
  4. {
  5. popUp();
  6. }
  7. }
Assuming, you have defined the following line:
Java Syntax (Toggle Plain Text)
  1. button1.addActionListener(this);
Oh, and one more thing, your class has to implement the ActionListener interface.
Reputation Points: 12
Solved Threads: 3
Junior Poster in Training
Chaster is offline Offline
68 posts
since Jun 2007
Jun 16th, 2007
0

Re: need help with jbutton and new jframe

Thanks a lot!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dajiebuda is offline Offline
8 posts
since Jun 2007
Jun 17th, 2007
0

Re: need help with jbutton and new jframe

Click to Expand / Collapse  Quote originally posted by dajiebuda ...
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
Reputation Points: 21
Solved Threads: 7
Junior Poster in Training
ProgrammersTalk is offline Offline
83 posts
since Jun 2007
Mar 12th, 2008
0

Re: need help with jbutton and new jframe

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.");
}

}
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hrlygrl923 is offline Offline
6 posts
since Mar 2008
Mar 13th, 2008
0

Re: need help with jbutton and new jframe

Click to Expand / Collapse  Quote originally posted by hrlygrl923 ...
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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is online now Online
5,373 posts
since Jan 2008
Mar 13th, 2008
0

Re: need help with jbutton and new jframe

Thanks. I tried that I just can not figure it out
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hrlygrl923 is offline Offline
6 posts
since Mar 2008
Mar 13th, 2008
0

Re: need help with jbutton and new jframe

I had something like this in mind:

JAVA Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is online now Online
5,373 posts
since Jan 2008
Aug 10th, 2008
0

Re: need help with jbutton and new jframe

my frame is working well but it freezes in the middle. What may be the reason.?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Rajnish1349 is offline Offline
1 posts
since Jul 2008
Aug 10th, 2008
0

Re: need help with jbutton and new jframe

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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is online now Online
5,373 posts
since Jan 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Inter Thread Communication!
Next Thread in Java Forum Timeline: Could somebody help me please with java program?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC