Adding a JButton to a panel at runtime

R

Richard

Hello all

I am having a problem adding a component to a JPanel at runtime. My
code adds a new JTextField. However it does not appear when I click the
add button. The component count of the JPanel does rise by one however
everytime I click the add button so the new JTextfield is being added.
I have noticed that when I click the border of the JFrame the new
JTextfield appears then. I have tried to 'repaint' the frame and the
JPanel but I still cannot see the new added JTextField. I can only see
it when I click on the edge of the frame. It seems to force a refresh.
Can anyone help?

Thanks in advance

Richard

--
 
M

mrandywarner

Try calling :

panel.revalidate();
panel.repaint();

The panel needs to be revalidated to relayout the child components.
 
Joined
Oct 2, 2008
Messages
1
Reaction score
0
How ca I add any graphic component to GUI at runtime?

Hi all,

you have written here that you added the button to GUI at runtime. Can I ask you how you did it? I tried this code, but it does not run :-(

My code:

public void actionPerformed(ActionEvent e) {
JToggleButton tb1 = new JToggleButton("Ahoj");
tb1.setName("tb1");
tb1.setMargin(new Insets(2, 2, 2, 2));
tb1.setFont(new Font("tahoma", 0, 11));
bottomBar.getLayout().addLayoutComponent("tb1", tb1);
// bottomBar.add(tb1);
bottomBar.revalidate();
bottomBar.repaint();
}

bottomBar is JPanel with GroupLayout...

Can you help me please?

Richard said:
Hello all

I am having a problem adding a component to a JPanel at runtime. My
code adds a new JTextField. However it does not appear when I click the
add button. The component count of the JPanel does rise by one however
everytime I click the add button so the new JTextfield is being added.
I have noticed that when I click the border of the JFrame the new
JTextfield appears then. I have tried to 'repaint' the frame and the
JPanel but I still cannot see the new added JTextField. I can only see
it when I click on the edge of the frame. It seems to force a refresh.
Can anyone help?

Thanks in advance

Richard

--
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top