Buttons and Event Handlers

N

Nessuno

Hi,

I am a newbie in Java. While learning from the Horstmann I have
modified a little example from the book. The problem is that the
buttons it creates are never shown.

Please, would someone explain what I am missing? The code follows.
Thanks in advance to everyone who will reply.

fabio

ButtonTest.java

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

public class ButtonTest
{
public static void main( String[] args )
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
ButtonFrame frame = new ButtonFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}

@SuppressWarnings("serial")
class ButtonFrame extends JFrame
{
public ButtonFrame()
{
setTitle("ButtonTest");
setSize(300, 200);
buttonPanel = new JPanel();
makeButton("Red", Color.RED);
makeButton("Green", Color.GREEN);
makeButton("Blue", Color.BLUE);
}

private void makeButton(String name, final Color backgroundColor)
{
JButton button = new JButton(name);
buttonPanel.add(button);
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
buttonPanel.setBackground(backgroundColor);
}

});

}

private JPanel buttonPanel;
}
 
J

Joshua Cranmer

Please, would someone explain what I am missing? The code follows.
Thanks in advance to everyone who will reply.

buttonPanel = new JPanel();
makeButton("Red", Color.RED);
makeButton("Green", Color.GREEN);
makeButton("Blue", Color.BLUE);

You make a buttonPanel and then... never add it to anything? So all of
your buttons are now on some panel which is never attached to anything else.
 
K

Knute Johnson

Hi,

I am a newbie in Java. While learning from the Horstmann I have
modified a little example from the book. The problem is that the
buttons it creates are never shown.

Please, would someone explain what I am missing? The code follows.
Thanks in advance to everyone who will reply.

fabio

ButtonTest.java

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

public class ButtonTest
{
public static void main( String[] args )
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
ButtonFrame frame = new ButtonFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}

@SuppressWarnings("serial")
class ButtonFrame extends JFrame
{
public ButtonFrame()
{
setTitle("ButtonTest");
setSize(300, 200);
buttonPanel = new JPanel();
makeButton("Red", Color.RED);
makeButton("Green", Color.GREEN);
makeButton("Blue", Color.BLUE);

add(buttonPanel);
}

private void makeButton(String name, final Color backgroundColor)
{
JButton button = new JButton(name);
buttonPanel.add(button);
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
buttonPanel.setBackground(backgroundColor);
}

});

}

private JPanel buttonPanel;
}

You need to add the buttonPanel to the JFrame.
 
M

markspace

Knute said:
public static void main( String[] args )
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
ButtonFrame frame = new ButtonFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.pack();
You need to add the buttonPanel to the JFrame.


The OP should also call pack() on the frame before it is displayed.
 
K

Knute Johnson

Knute said:
public static void main( String[] args )
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
ButtonFrame frame = new ButtonFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);

You need to add the buttonPanel to the JFrame.


The OP should also call pack() on the frame before it is displayed.

He calls setSize() in the JFrame constructor. Not the best method but
it does work in this instance.
 
R

Roedy Green

I am a newbie in Java. While learning from the Horstmann I have
modified a little example from the book. The problem is that the
buttons it creates are never shown.

See http://mindprod.com/jgloss/jbutton.html
and
http://mindprod.com/jgloss/button.html

for some similar sample code that does work.

Hint: Every Component except the Frame/JFrame must be added to some
container to be visible.
--
Roedy Green Canadian Mind Products
http://mindprod.com

When a newbie asks for help tracking a bug in a code snippet, the problem is usually is the code he did not post, hence the value of an SSCCE.
see http://mindprod.com/jgloss/sscce.html
 
M

markspace

Knute said:
He calls setSize() in the JFrame constructor. Not the best method but
it does work in this instance.


Thanks, I'd missed that. I think I might recommend calling pack() after
adding all components, then calling setMinimumSize() instead of
setSize(). That'll prevent the components from clipping if they take
more space, but will enlarge the window if it happens to be smaller.
 
L

Lew

Nessuno said:
public class ButtonTest
{
public static void main( String[] args )
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
ButtonFrame frame = new ButtonFrame();

As others have answered your question, I will only add that you should not
indent Usenet code examples with TAB characters, as they get ridiculously wide
for most newsreaders and gravely harm readability. You want your code to be
readable. Please use spaces, up to a maximum of four per indent level, for
indentation.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top