Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Swing event handling for a JButton?  RSS feed

 
Hugh Mclaughlin
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've created a GUI and am trying to put two JTextFields in, I am using the BorderLayout Manager, and I want both of them at the top of the Container, so I (being a GUI noob) put them both NORTH. Except now one overwrites the other, any idea how to get one under the other but both at the top?

Code:

private void buildGUI()
{
JPanel title = new JPanel();
title.setLayout(new BorderLayout());
title.add(new JTextField(10),
BorderLayout.CENTER);
title.setBorder
(new TitledBorder(new EtchedBorder(),
"Title: "));

JPanel author = new JPanel();
author.setLayout(new BorderLayout());
author.add(new JTextField(10),
BorderLayout.CENTER);
author.setBorder
(new TitledBorder(new EtchedBorder(),
"Author: "));
Container pane = getContentPane();

pane.add(title, BorderLayout.NORTH);
pane.add(author, BorderLayout.NORTH);
}
[ November 04, 2005: Message edited by: bert McG ]
 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use another Container (JPanel) which contains your two JTextFields. You then add that Container to the pane.

You'll have to do this quite often to create more complex GUI's: putting Containers (with quite different LayoutManagers possibly) inside other Containers.
 
Hugh Mclaughlin
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I almost understand that could you explain it using the code? Sorry if I seem really simple I am very new to GUIs! Many thanks.
 
Brian Cole
Author
Ranch Hand
Posts: 949
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Rob Spoor meant something like this:
 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exactly. Although you can give northPanel any layout you wish, it doesn't matter much for the overal buildup (only appearance and resizing behaviour).
 
Hugh Mclaughlin
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could anyone help me with an event? I am trying to make my JButton "Cancel" clear the GUI screen of all text entered when it is clicked...if anyone could help I would appreciate it a lot!

Code:

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


class MessageBoard

extends JFrame

{
MessageBoard()
{
super("COMP213 Message Board");

buildGUI();

setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setVisible(true);
}

private void buildGUI()
{
JPanel title = new JPanel();
title.setLayout(new BorderLayout());
title.add(new JTextField(10),
BorderLayout.CENTER);
title.setBorder
(new TitledBorder(new EtchedBorder(),
"Title: "));

JPanel author = new JPanel();
author.setLayout(new BorderLayout());
author.add(new JTextField(10),
BorderLayout.CENTER);
author.setBorder
(new TitledBorder(new EtchedBorder(),
"Author: "));

JPanel cancel = new JPanel();
cancel.setLayout(new BorderLayout());
cancel.add(new JButton("Cancel"),
BorderLayout.CENTER);

JPanel send = new JPanel();
send.setLayout(new BorderLayout());
send.add(new JButton("Send"),
BorderLayout.CENTER);

JPanel box = new JPanel();
box.setLayout(new BorderLayout());
box.add(new JTextArea(20,50),
BorderLayout.CENTER);
box.setBorder
(new TitledBorder(new EtchedBorder(),
"Message"));

JPanel view = new JPanel();
view.setLayout(new BorderLayout());
view.add(new JButton("View Topics"),
BorderLayout.CENTER);

TopicView tv = new TopicView();
tv.setTopics();

Container pane = getContentPane();

JPanel northPanel = new JPanel(new GridLayout(1, 0));
northPanel.add(title);
northPanel.add(author);

JPanel eastPanel = new JPanel(new GridLayout(3,0));
eastPanel.add(cancel);
eastPanel.add(send);
eastPanel.add(view);

pane.add(northPanel, BorderLayout.NORTH);
pane.add(eastPanel, BorderLayout.EAST);
pane.add(box, BorderLayout.CENTER);
pane.add(tv, BorderLayout.CENTER);

}
public static void main(String[] args)
{
MessageBoard m = new MessageBoard();
}
}


[ November 04, 2005: Message edited by: bert McG ]
[ November 04, 2005: Message edited by: bert McG ]
 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, you have to define an action for your button. You can use either an instance of javax.swing.Action (subclassing AbstractAction usually works), or a java.awt.event.ActionListener.
Second, that action or action listener has to have references to the text fields to clear.

Please check the API and/or tutorial for more info.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!