0

Hi I have been using netbeans and the swing functions for a while now. I recently have added a JPanel to my J frame by just dragging and dropping. I have then also added a button. I wrote the following code trying to add text to the Panel when the button is clicked. Below is the code I have used.

public void addTextTry(){
        JLabel l1 = new JLabel("The add method appends an element to an array.");
        JLabel l2 = new JLabel("This inturn increases the arrays size.");
        jPanel1.add(l1);
        jPanel1.add(l2);
    }

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        addTextTry();
    }  

I am not getting an error message and If i add anything else to the code like adding text to a textbox, this works. I have checked for silly mistakes such as having the names of the panel and button wrong but this is all correct. Can anyone see or suggest why the text is not appearing in the panel when the button is clicked?

Tom
  • 51
  • 2
  • 10
  • 1
    "I recently have added a JPanel to my JFrame by just dragging and dropping. " There's your problem. Until you completely understand how [Swing](http://docs.oracle.com/javase/tutorial/uiswing/) and [Swing layouts](http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html) work, you won't understand what you're dragging and dropping. You should code your first 100 - 250 Swing applications by hand until you do understand. – Gilbert Le Blanc Jan 26 '16 at 15:05
  • What `Layout` does your `JPanel` instance have? – TT. Jan 26 '16 at 15:41

2 Answers2

1

I think you would need to call jPanel1.revalidate() after adding the labels to the panel so that the layout is reapplied.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;


public class DisplayMessageTrial extends JFrame {

private JPanel jPanel1;
private JButton button = new JButton("Click Me!");

public DisplayMessageTrial() {
    super("DisplayMessageTrial");
    jPanel1 = new JPanel();
    jPanel1.add(button);
    setContentPane(jPanel1);
    button.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            jButton1ActionPerformed(e);
        }
    });
}

public void addTextTry(){
    JLabel l1 = new JLabel("The add method appends an element to an array.");
    JLabel l2 = new JLabel("This inturn increases the arrays size.");
    jPanel1.add(l1);
    jPanel1.add(l2);
    jPanel1.revalidate();
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    addTextTry();
}  


public static void main(String[] args) {
    DisplayMessageTrial trial = new DisplayMessageTrial();
    trial.setBounds(100, 100, 300, 300);
    trial.setVisible(true);
}

}
Simon White
  • 166
  • 1
  • 3
  • Thanks for your help, I have tried putting this at the end of the function but this still didn't work. I understand what jPanel1.revalidate() is doing though, just wondering where in the code i need to put it... – Tom Jan 26 '16 at 14:52
  • I've added some sample code. The text is added to the panel with the call to revalidate() in place, but does not work without it. – Simon White Jan 26 '16 at 15:09
  • this worked brilliantly, however how do i mange to add the button to the original jFrame so that when i add the clear function to the button it dosnt clear the button – Tom Jan 26 '16 at 15:46
  • You can have three panels in the contentPane of the JFrame - one master panel that contains two sub-panels. Then one sub-panel contains the button and the other sub-panel contains everything else. You apply the removeAll() method to the panel that contains the element to be cleared. – Simon White Jan 26 '16 at 23:39
  • Alternatively, you just have one JPanel as a container and instead of calling removeAll(), you call getComponents() on the panel first to get an array of components and you iterate through the array and remove each component in turn using JPanel.remove(Component c) but only if the component is not the button that you want to keep. – Simon White Jan 26 '16 at 23:51
1

Before adding the labels to the panel, you may want to clear it first with removeAll (well, unless you want to leave existing children that you may have added beforehand).

After adding the labels to the panel, you have to notify the panel of its new content with revalidate, so that it recomputes its layout, and repaints the components.

Finally, you should tell the panel to repaint itself.

Putting it all together, your method would look like :

public void addTextTry(){
        JLabel l1 = new JLabel("The add method appends an element to an array.");
        JLabel l2 = new JLabel("This inturn increases the arrays size.");
        jPanel1.removeAll();
        jPanel1.add(l1);
        jPanel1.add(l2);
        jPanel1.revalidate();
        jPanel1.repaint();
    }

You may find additional information in this topic :

Java Swing revalidate() vs repaint()

Community
  • 1
  • 1
Arnaud
  • 17,229
  • 3
  • 31
  • 44