Java Swings: Adding JButton to JFrame

Discussion in 'Java' started by arunsivaprakash@gmail.com, Apr 12, 2006.

  1. Guest

    Dear All,

    I have a problem in the following code.....I was unit testing the code
    from main(). I have the FaMain base class for creating the window with
    certain features. So I need all my child windows to inherit FaMain
    class. Similarly, I need FaButton as base class instead of JButton.

    But even after creating two buttons but1 & but2 only the second button
    but2 is getting appeared......Please help me.......


    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    /**
    * <p>Title: </p>
    * <p>Description: </p>
    * <p>Copyright: Copyright (c) 2006</p>
    * <p>Company: </p>
    * @author not attributable
    * @version 1.0
    */

    public class FaButton extends JPanel {

    JButton jButton1;
    FaMain fa;
    static JFrame jf;
    public FaButton() {
    jf = new FaMain().jfFaMain;
    jButton1.setText("Sample");
    jButton1.setBounds(new Rectangle(229, 122, 59, 25));
    jButton1.setOpaque(true);
    jButton1.setVisible(true);
    jf.getContentPane().add(jButton1);
    }
    public FaButton(JFrame jf, String title, char Mnemonic, String
    ToolTipText )throws Exception {
    this.jf = (jf == null)? new JFrame(Console.title(fa)) : jf;
    jButton1 = new JButton();
    jButton1.setText(title);
    jButton1.setMnemonic(Mnemonic);
    jButton1.setToolTipText(ToolTipText);
    jButton1.setBounds(new Rectangle(0, 0, 100, 100));
    jButton1.setOpaque(true);
    jButton1.setEnabled(true);
    add(jButton1);
    jf.getContentPane().add(this);
    }
    public FaButton(JFrame jf, int x, int y, int width, int height, String
    title, char Mnemonic, String ToolTipText )throws Exception {
    this.jf = (jf == null)? new JFrame(Console.title(fa)) : jf;
    jButton1 = new JButton();
    jButton1.setText(title);
    jButton1.setMnemonic(Mnemonic);
    jButton1.setToolTipText(ToolTipText);
    jButton1.setBounds(new Rectangle(x, y, width, height));

    jButton1.setAlignmentX(x);
    jButton1.setAlignmentY(y);

    jButton1.setOpaque(true);
    jButton1.setEnabled(true);
    jButton1.setVisible(true);

    add(jButton1, null);

    jf.getContentPane().add(this);

    jButton1.setVisible(true);

    jButton1.revalidate();
    jButton1.repaint();
    }
    public static void main(String[] args) {
    try {
    FaMain faMain = new FaMain();
    JFrame jj= faMain.jfFaMain;

    FaButton but1 = new FaButton(jj, 500, 500, 100, 100, "My
    Button1", 'M', "This is a button");
    FaButton but2 = new FaButton(jj, 200, 200, 100, 100, "My
    Button2", 'M', "This is a button");

    Component[] comp =
    jf.getRootPane().getContentPane().getComponents();

    System.out.println(jf.getRootPane().getContentPane().getComponentCount());

    for (int i = 0; i < comp.length; i++) {
    System.out.println(comp.getClass().getName());
    }
    }
    catch (Exception ex) {
    }
    }
    }
     
    , Apr 12, 2006
    #1
    1. Advertising

  2. Bart Cremers wrote:
    >
    > You have to take the layout manager into account. The default layout
    > manager of a JFrame is a BorderLayout. When you add the buttons you use
    > the contentPane.add(Component) method which is equals to adding the
    > button to the CENTER of the borderlayout.


    It's worth noting that add and setLayout on JFrame and certain other
    classes don't do what they appear to. They are hacked to forward to the
    content pane (from 1.5), as the code below demonstrates.

    class Lay {
    public static void main(String[] args) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    javax.swing.JFrame frame = new javax.swing.JFrame();
    frame.setLayout(new javax.swing.SpringLayout());
    System.err.println(frame.getLayout().getClass());
    }
    });
    }
    }

    (What it actually prints is dependent upon the PL&F, but usually class
    java.awt.BorderLayout as well.)

    > So you add two components to
    > the same are of a borderlayout, which will result in the borderlayout
    > throwing away the first component added to that position.


    Yes, the BorderLayout will leave the first component alone, not changing
    its bounds at all. So if you show the frame and latter add the second
    component, then the first component will stay where it was when the
    second was added. If you add both components before validating (or
    packing) then the first will have a bounds of 0, 0, 0, 0.

    Shift-Control-F1 is useful to see what is going on.

    Tom Hawtin
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
     
    Thomas Hawtin, Apr 12, 2006
    #2
    1. Advertising

  3. Bart Cremers Guest

    Just to focus on your problem and not to ask the questions I have with
    your code.

    You have to take the layout manager into account. The default layout
    manager of a JFrame is a BorderLayout. When you add the buttons you use
    the contentPane.add(Component) method which is equals to adding the
    button to the CENTER of the borderlayout. So you add two components to
    the same are of a borderlayout, which will result in the borderlayout
    throwing away the first component added to that position.
    Do a setLayout(new FlowLayout()) on your JFrame and you'll see both
    buttons added side by side.

    Regards,

    Bart
     
    Bart Cremers, Apr 12, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sweta

    problem in java swings

    sweta, May 5, 2006, in forum: Java
    Replies:
    0
    Views:
    1,394
    sweta
    May 5, 2006
  2. sweta
    Replies:
    2
    Views:
    726
    Roedy Green
    May 8, 2006
  3. jakester
    Replies:
    3
    Views:
    1,899
    jakester
    Apr 3, 2007
  4. middaycrisis
    Replies:
    0
    Views:
    1,546
    middaycrisis
    Nov 11, 2007
  5. Replies:
    2
    Views:
    439
    markspace
    Aug 27, 2012
Loading...

Share This Page