Java Swings: Adding JButton to JFrame

A

arunsivaprakash

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) {
}
}
}
 
T

Thomas Hawtin

Bart said:
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
 
B

Bart Cremers

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top