Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Buttons and Event Handlers

Reply
Thread Tools

Buttons and Event Handlers

 
 
Nessuno
Guest
Posts: n/a
 
      02-21-2010
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_CLOS E);
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;
}
 
Reply With Quote
 
 
 
 
Joshua Cranmer
Guest
Posts: n/a
 
      02-21-2010
On 02/21/2010 06:26 PM, Nessuno wrote:
> 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.

--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
 
Reply With Quote
 
 
 
 
Knute Johnson
Guest
Posts: n/a
 
      02-21-2010
On 2/21/2010 3:26 PM, Nessuno wrote:
> 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_CLOS E);
> 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.

--

Knute Johnson
email s/nospam/knute2010/

 
Reply With Quote
 
markspace
Guest
Posts: n/a
 
      02-22-2010
Knute Johnson wrote:
>> public static void main( String[] args )
>> {
>> EventQueue.invokeLater(new Runnable()
>> {
>> public void run()
>> {
>> ButtonFrame frame = new ButtonFrame();
>> frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);


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.
 
Reply With Quote
 
Knute Johnson
Guest
Posts: n/a
 
      02-22-2010
On 2/21/2010 4:59 PM, markspace wrote:
> Knute Johnson wrote:
>>> public static void main( String[] args )
>>> {
>>> EventQueue.invokeLater(new Runnable()
>>> {
>>> public void run()
>>> {
>>> ButtonFrame frame = new ButtonFrame();
>>> frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);

>
> 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.

--

Knute Johnson
email s/nospam/knute2010/

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      02-22-2010
On Sun, 21 Feb 2010 15:26:57 -0800 (PST), Nessuno <>
wrote, quoted or indirectly quoted someone who said :

>
>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
 
Reply With Quote
 
markspace
Guest
Posts: n/a
 
      02-22-2010
Knute Johnson wrote:

> 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.

 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      02-22-2010
Nessuno wrote:
> 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.

--
Lew
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
edit update and canel buttons not firing correct event handlers lauralucas@gmail.com ASP .Net Datagrid Control 2 07-13-2006 03:56 PM
dynamically creating buttons with event handlers Michael Dawson ASP .Net 3 02-22-2006 06:33 PM
2 buttons but want enter key in textbox to execute one buttons' click event? Roger ASP .Net 1 05-20-2005 09:47 PM
Handlers and dynamic buttons noone ASP .Net 1 01-27-2004 11:30 AM
Dynamic Buttons and Event Handlers, revisited Boban Dragojlovic ASP .Net 0 06-24-2003 01:51 PM



Advertisments