Results 1 to 6 of 6
Thread: Help with this JButton
- 02-18-2013, 10:02 AM #1
Member
- Join Date
- Sep 2012
- Posts
- 26
- Rep Power
- 0
Help with this JButton
and this is my problem:Java Code:public class Trypack extends JFrame { public Trypack(){ final JFrame Main1 = new JFrame(); final JFrame Visual3 = new JFrame(); Btn1 = new JButton("Login"); Btn2 = new JButton("Clear"); Btn3 = new JButton("View Students"); Btn4 = new JButton("Add Students"); Btn5 = new JButton("Save"); Main1.add(Lbl1); Main1.add(Txt1); Main1.add(Lbl2); Main1.add(Txt2); Main1.add(Btn1); Main1.add(Btn2); Visual3.add(Lbl3); Visual3.add(Txt3); Visual3.add(Lbl4); Visual3.add(Txt4); Visual3.add(Lbl5); Visual3.add(Txt5); Visual3.add(Lbl8); Visual3.add(Txt7); Visual3.add(Lbl6); Visual3.add(Txt6); Visual3.add(Lbl7); Visual3.add(cmbx); Visual3.add(Btn5); Visual3.add(Btn2);
I want to make Btn2 to appear in Main1 Frame and in Visual3 Frame but when i run the program the Btn2 is disappearing in Main1 frame. please help me with this i don't know how to solve this because im newbie in programming :DJava Code:Main1.add(Btn2); Visual3.add(Btn2);
- 02-18-2013, 12:35 PM #2
Re: Help with this JButton
A JButton can have only one parent, so it's not possible to use it in two containers at the same time. You could add the same action/actionListener to two buttons to have them doing the same.
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 02-18-2013, 12:41 PM #3
Senior Member
- Join Date
- Oct 2010
- Posts
- 316
- Rep Power
- 3
Re: Help with this JButton
Hi raffs03,
Adding the same object to different jFrames goes against the concept of OO (Object Orientated). Instead you will need to create two buttons, both of which can have identical text.
Generally speaking, I have never seen JFrame declared as final and objects are usually added to a JPanel which is then added to a JFrame.
Regards.
- 02-18-2013, 02:11 PM #4
Member
- Join Date
- Sep 2012
- Posts
- 26
- Rep Power
- 0
Re: Help with this JButton
Now i know thanks for answering my question I am really a newbie at programming :D
- 02-18-2013, 04:28 PM #5
Re: Help with this JButton
Having a final JFrame variable is fine, and actually makes a lot of sense. I'd be more worried about the unnecessary extending of JFrame, plus the nonstandard naming conventions.
Also note that the JFrame add() method simply passes the Component to its content pane, which is by default a JPanel. Also note that the default layout of content panes is BorderLayout, so the OP is going to have other problems judging from how he's adding the components now.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-18-2013, 05:29 PM #6
Senior Member
- Join Date
- Oct 2010
- Posts
- 316
- Rep Power
- 3
Similar Threads
-
How set JButton icon Relative to the JButton size???
By farshad in forum AWT / SwingReplies: 1Last Post: 01-15-2013, 06:44 PM -
ActionListener for JButton after changing Button to JButton
By ravi.joshi53 in forum Java AppletsReplies: 2Last Post: 10-07-2011, 07:35 AM -
jbutton
By Patea2000 in forum Advanced JavaReplies: 1Last Post: 03-16-2011, 08:59 AM -
JButton help
By ThrashingBoy in forum New To JavaReplies: 8Last Post: 03-01-2011, 01:48 AM -
Regarding JButton
By sneha_v27 in forum AWT / SwingReplies: 4Last Post: 02-18-2011, 02:17 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks