0

So, I am creating a new Canvas (JPanel) class: Canvas canvas = new Canvas(); and then I am calling a method on that class: canvas.addTextBox();

Now, from within this Canvas class, I want to add a new jTextArea to the Canvas. I tried using the code below but it isn't showing up. Not sure what I am doing wrong. Thanks!

class Canvas extends JPanel {

    public Canvas() {
        this.setOpaque(true);
        //this.setBackground(Color.WHITE);
    }

    public void addTextBox() {
        final JTextArea commentTextArea = new JTextArea(10, 10);
        commentTextArea.setLineWrap(true);
        commentTextArea.setLineWrap(true);
        commentTextArea.setWrapStyleWord(true);
        commentTextArea.setVisible(true);
    }

}

Full Code

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

import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class UMLEditor {

    public static void main(String[] args) {
        JFrame frame = new UMLWindow();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBounds(30, 30, 1000, 700);
        frame.getContentPane().setBackground(Color.white);
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}
class UMLWindow extends JFrame {
    Canvas canvas = new Canvas();

    private static final long serialVersionUID = 1L;

    public UMLWindow() {
        addMenus();
    }
    public void addMenus() {

        getContentPane().add(canvas);

        JMenuBar menubar = new JMenuBar();

        JMenuItem newTextBox = new JMenuItem("New Text Box");
        newTextBox.setMnemonic(KeyEvent.VK_E);
        newTextBox.setToolTipText("Exit application");
        newTextBox.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                canvas.addTextBox();
            }
        });

        menubar.add(newTextBox);

        setJMenuBar(menubar);

        setSize(300, 200);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
}

class Canvas extends JPanel {

    public Canvas() {
        this.setOpaque(true);
        //this.setBackground(Color.WHITE);
    }

    public void addTextBox() {
        final JTextArea commentTextArea = new JTextArea(10, 10);
        commentTextArea.setLineWrap(true);
        commentTextArea.setLineWrap(true);
        commentTextArea.setWrapStyleWord(true);
        commentTextArea.setVisible(true);
    }

}
Harry
  • 772
  • 10
  • 32
  • 1
    You have to add it to the panel. What you have now is only creating the JTextArea along with some setters. – Luminous Oct 27 '14 at 17:28

1 Answers1

3

The addTextBox method only creates a JTextArea. It never adds it to the JPanel

You will need to add the following line to addTextBox method:

add( commentTextArea );

In case the JFrame which contains your components is already visible on screen when the addTextBox method is called, you need to invalidate the container as well. Simply add

revalidate();
repaint();
Robin
  • 36,233
  • 5
  • 47
  • 99
  • Do I have to notify anything that a JTextArea has been added? This works but it only shows up when I resize/move the JFrame – Harry Oct 27 '14 at 17:47
  • @Harry: You'll need to `(re-)validate()` the enclosing container and possibly `repaint()`, for [example](http://stackoverflow.com/a/5812981/230513). – trashgod Oct 27 '14 at 17:49
  • Ah, gotchya. Why `invalidate();` in update? Validate works (but I'm not familiar with validate() at all, just tested). Also setting a bounds on the JTextArea worked. – Harry Oct 27 '14 at 17:51
  • @Harry See http://stackoverflow.com/q/9510125/1076463. I basically mark the container as invalid because a component has been added, so the layout must be refreshed. And I then trigger a repaint – Robin Oct 27 '14 at 17:57
  • So does this mean you have to validate(); before repainting? If I use invalide() and then repaint() is does the same as before, only shows up when I resize the window – Harry Oct 27 '14 at 18:54
  • 1
    @Harry Perhaps it was revalidate() and repaint() instead of invalidate() and repaint(). – Robin Oct 27 '14 at 20:56