0

I m creating a GUI in java and would like to use a JTextArea, however I am having a lot of trouble adding it to the frame. How would I go about creating a text Area and then using it to read text or display text?

Here is my GUI code so far:

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

public class addMemoUI extends JFrame { 

JFrame frame = new JFrame();

/**
 * Create the application.
 */
public addMemoUI() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {

    frame.getContentPane().setBackground(new Color(255, 255, 255));
    frame.getContentPane().setLayout(null);

    JButton button = new JButton("Create");
    button.setBackground(new Color(100, 149, 237));
    button.setBounds(135, 350, 130, 50);
    frame.getContentPane().add(button);

    JLabel lblMemos = new JLabel("MEMOS");
    lblMemos.setForeground(new Color(100, 149, 237));
    lblMemos.setFont(new Font("Moire", Font.BOLD, 30));
    lblMemos.setBounds(22, 21, 234, 37);
    frame.getContentPane().add(lblMemos);

    JButton button_1 = new JButton("Cancel");
    button_1.setBackground(new Color(100, 149, 237));
    button_1.setBounds(5, 350, 130, 50);
    frame.getContentPane().add(button_1);

    frame.setBounds(100, 100, 270, 400);
    frame.setUndecorated(true); //REMOVES MENU BAR
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JButton btnExit = new JButton("");
    btnExit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            System.exit(0);
        }
    });
}

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                MemoUI window = new MemoUI();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}
}

Thanks very much :)

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user3120023
  • 197
  • 3
  • 6
  • 16
  • Quick note: you are both extending from `JFrame` and using the variable `JFrame frame`. You probably only want one of the two. – DSquare Apr 02 '14 at 18:01
  • Thanks, i'll amend that :) – user3120023 Apr 02 '14 at 18:03
  • 1
    To sort-of-answer your question, you should use a [Layout Manager](http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html). Using a null Layout is a bad idea (code is longer, less reliable, less scalable...). Once you know how to use a layout then you'll know how to add a `JTextArea` to the frame/panel. – DSquare Apr 02 '14 at 18:06
  • @DSquare +1. Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Apr 02 '14 at 23:44

1 Answers1

1

Here is example for how to use JTextArea. You can set, get or append text. You can find the others by google.

public class Example {
private JTextArea jtextbox;

private void initialize() {
    JFrame frm = new JFrame();
      :
    JScrollPane scroll = new JScrollPane();

    jtextbox= new JTextArea();
    scroll.setViewportView(jtextbox); // add scroll panel
    jtextbox.setTabSize(4);
    jtextbox.setLineWrap(true);
    jtextbox.setBackground(SystemColor.window);
}

private void setText(String text)  {

    jtextbox.append(text); // or setText(text)
}

private String getText()  {
    return jtextbox.getText();
}

}
Timothy
  • 19
  • 3
  • Thanks very much for this, how do i actually add the text box to the frame, as currently it is not appearing. – user3120023 Apr 02 '14 at 18:39
  • How would i add it to the frame? – user3120023 Apr 02 '14 at 19:42
  • Use WindowBuilder Plug-in on eclipse. It's easy way. You can add components on JPanel and need to decide Layouts. This is your choice. – Timothy Apr 02 '14 at 19:56
  • I'm designing this in eclipse. – user3120023 Apr 02 '14 at 22:39
  • Did you install WindowBuilder? If not, here is the URL https://www.eclipse.org/windowbuilder/download.php. You just need drag & drop. Then check initialize() function which contains drawing information. Don't forget each layout show them differently. I think the default value is GridLayOut. Good luck – Timothy Apr 03 '14 at 13:54