Results 1 to 6 of 6
  1. #1
    raffs03 is offline Member
    Join Date
    Sep 2012
    Posts
    41
    Rep Power
    0

    Unhappy Help with this JButton

    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);
    and this is my problem:
    Java Code:
    Main1.add(Btn2);
    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 :D

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default 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

  3. #3
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    335
    Rep Power
    4

    Default 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.

  4. #4
    raffs03 is offline Member
    Join Date
    Sep 2012
    Posts
    41
    Rep Power
    0

    Default Re: Help with this JButton

    Now i know thanks for answering my question I am really a newbie at programming :D

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

    Default Re: Help with this JButton

    Quote Originally Posted by Ronin View Post
    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.
    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!

  6. #6
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    335
    Rep Power
    4

    Default Re: Help with this JButton

    Quote Originally Posted by KevinWorkman View Post
    I'd be more worried about the unnecessary extending of JFrame, plus the nonstandard naming conventions.
    Which will change with experience... or until someone points it out.

    Cheers for the additonal info.

    Regards.

Similar Threads

  1. Replies: 1
    Last Post: 01-15-2013, 06:44 PM
  2. ActionListener for JButton after changing Button to JButton
    By ravi.joshi53 in forum Java Applets
    Replies: 2
    Last Post: 10-07-2011, 07:35 AM
  3. jbutton
    By Patea2000 in forum Advanced Java
    Replies: 1
    Last Post: 03-16-2011, 08:59 AM
  4. JButton help
    By ThrashingBoy in forum New To Java
    Replies: 8
    Last Post: 03-01-2011, 01:48 AM
  5. Regarding JButton
    By sneha_v27 in forum AWT / Swing
    Replies: 4
    Last Post: 02-18-2011, 02:17 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
  •