user4559332
user4559332

Reputation:

how to add a "console" like window to a GUI?

I have created a GUI. It has several buttons. i would like to add a console like object underneath them in which i would be able to write messages so the user would see them. what element/object/class should i use? i want it to be able to present messages in different lines. here is my code for creating the GUI:

public ssGUI() {
    setLayout(new BorderLayout());

    bRunNoAdj = new JButton("Run no adjustment");
    bRunNoAdj.setVerticalTextPosition(AbstractButton.CENTER);
    bRunNoAdj.setHorizontalTextPosition(AbstractButton.LEADING);
    bRunNoAdj.setMnemonic(KeyEvent.VK_E);
    bRunNoAdj.addActionListener(this);
    bRunNoAdj.setEnabled(false);
    bRunNoAdj.setBackground(Color.white);

    bRunAdj = new JButton("Run adjustment");
    bRunAdj.setVerticalTextPosition(AbstractButton.CENTER);
    bRunAdj.setHorizontalTextPosition(AbstractButton.LEADING);
    bRunAdj.setMnemonic(KeyEvent.VK_E);
    bRunAdj.addActionListener(this);
    bRunAdj.setEnabled(false);
    bRunAdj.setBackground(Color.white);

    bConnect = new JButton("Connect");
    bConnect.setMnemonic(KeyEvent.VK_E);
    bConnect.addActionListener(this);
    bConnect.setEnabled(true);
    bConnect.setBackground(Color.white);

    bDisconnect = new JButton("Disconnect");
    bDisconnect.setMnemonic(KeyEvent.VK_E);
    bDisconnect.addActionListener(this);
    bDisconnect.setEnabled(false);
    bDisconnect.setBackground(Color.white);

    bStationary = new JButton("Show Stationary");
    bStationary.setMnemonic(KeyEvent.VK_E);
    bStationary.addActionListener(this);
    bStationary.setEnabled(false);
    bStationary.setBackground(Color.white);

    bMoving = new JButton("Show Moving");
    bMoving.setMnemonic(KeyEvent.VK_E);
    bMoving.addActionListener(this);
    bMoving.setEnabled(false);
    bMoving.setBackground(Color.white);

    JPanel topPanel = new JPanel();
    topPanel.add(bConnect);
    topPanel.add(bDisconnect);
    add(topPanel, BorderLayout.NORTH);
    JPanel centerPanel = new JPanel();
    centerPanel.add(bRunNoAdj);
    centerPanel.add(bRunAdj);
    add(centerPanel, BorderLayout.CENTER);
    JPanel bottomPanel = new JPanel();
    bottomPanel.add(bStationary);
    bottomPanel.add(bMoving);
    add(bottomPanel, BorderLayout.SOUTH);
}

any help would be appreciated, thank you.

Upvotes: 4

Views: 600

Answers (3)

Roan
Roan

Reputation: 1206

The easiest would probably be to use a JTextArea.

You would ofcourse prevent the user from editing the area, this can be done like this:

JTextArea area = new JTextArea();
area.setEditable(false);

And if you want the user to be able to scroll the area you can add it to a JScrollPane like this:

JTextArea area = new JTextArea();
JScrollPane scrollableArea = new JScrollPane(area);

And lastly you can add a line to the area by doing:

area.setText(area.getText() + "\n" + newLine);

Or preferably:

area.append("\n" + newLine);

I hope this helps :)

Upvotes: 5

Jorgovanka
Jorgovanka

Reputation: 79

I think what you mean is JTextArea or JTextField

Upvotes: 0

AJNeufeld
AJNeufeld

Reputation: 8695

Use a JTextArea. Call text_area.setEditable(false); on it to make it read-only.

Upvotes: 5

Related Questions