Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / October 2005

Tip: Looking for answers? Try searching our database.

Event handling in a JPanel

Thread view: 
Casper - 03 Oct 2005 22:16 GMT
Is there a reason why the following actionlistener setup would not work
in a JDialog deriven window, when my button is placed within JPanel's
(for layout reasons)?

public class DateTimeChooser extends JDialog implements ActionListener{
...
    jButtonOk = new JButton("Ok");
    JPanel panel = new JPanel(new CenterLayout() );
    panel.add( jButtonOk );
...
    jButtonOk.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            jButtonOk_actionPerformed(e);
        }
    }
    );
...
    private void jButton1_actionPerformed(ActionEvent e)
    {
        System.out.println("Hello");
    }
}

...if I simply add the button to the JDialog, all is fine and dandy.
First I though it was because I had to handle the actionPerformed in the
parent JPanel but I am sure I have handled button events before without
subclassing the JPanel it happens to be located on.

Thanks in advance,
Casper
Monique Y. Mudama - 03 Oct 2005 23:05 GMT
> Is there a reason why the following actionlistener setup would not work
> in a JDialog deriven window, when my button is placed within JPanel's
[quoted text clipped - 23 lines]
> parent JPanel but I am sure I have handled button events before without
> subclassing the JPanel it happens to be located on.

Erm, you call jButtonOk_actionPerformed, but you show
jButton1_actionPerformed ...

Signature

monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html

Casper - 04 Oct 2005 00:31 GMT
Monique Y. Mudama:
> Erm, you call jButtonOk_actionPerformed, but you show
> jButton1_actionPerformed ...

Formatting typo. jButtonOk is the name used consequently. The problem
persists even if the handler is called:

private void jButtonOk_actionPerformed(ActionEvent e)
{
    System.out.println("Hello");
}

Regards,
Casper
Andrew Thompson - 04 Oct 2005 01:00 GMT
> Monique Y. Mudama:
>
>> Erm, you call jButtonOk_actionPerformed, but you show
>> jButton1_actionPerformed ...
>
> Formatting typo.

Looks more like a 'spelling' or 'naming' typo., but..

>..jButtonOk is the name used consequently.

Is there a jButton1 *anywhere* in the rest of your code?
My guess is that sopmhow those buttons/actions/methods
have become entangled.

> ...The problem
> persists even if the handler is called:

Does it persist if you reduce the GUI to a single button?
Casper - 04 Oct 2005 01:25 GMT
I obviously double and tripple checked the code, tried both with letting
my JDialog implementing the ActionListener interface as well as an
anonymous inner class.

The thing is, if I just place the button as the top-most component in
the JDialog, all is fine and dandy. But placing it wihin a JPanel on the
JDialog it just wont work.

---------------------------------------
package com.brunata.ptd.views.widgets;

<import section>

public class DateTimeChooser extends JDialog
{
    Vector hours = new Vector();
    Vector minutes = new Vector();
    JButton ok;
    JTextComponent component;
    DateChooserPanel dateChooser;
    JPanel border;
    JPanel okTimePanel;
    JPanel okPanel;
   
    public DateTimeChooser(JDialog owner, JTextComponent component, String
title)
    {
       super(owner);

        this.component = component;
       DecimalFormat twoPlaces = new DecimalFormat("00");
        for(int i = 0; i < 24; i++)
            hours.add(twoPlaces.format(i));
       for(int i = 0; i < 60; i++)
           minutes.add(twoPlaces.format(i));

       //ok.addActionListener(this);
        ok = new JButton("Ok");
       ok.addActionListener(new ActionListener() {
                                public void actionPerformed(ActionEvent e)
                                {
                                    jButton1_actionPerformed(e);
                                }
                            }
       );

       
        border = new JPanel();
       
        border.setBorder( BorderFactory.createRaisedBevelBorder());
        border.setLayout(new BorderLayout());
       
       //super( owner, title, true );
        dateChooser = new DateChooserPanel();
        dateChooser.setBorder(new EmptyBorder(5,5,5,5));
   
        this.setSize(160,120);
        this.setLocation((int)(component.getLocation().getX() + owner.getX()),
            (int)(component.getLocation().getY()+(component.getHeight()) +
owner.getY()));
       
        this.setUndecorated(true);
       
        border.add(dateChooser, BorderLayout.WEST);
       
        okTimePanel = new JPanel();
        okTimePanel.setLayout( new BorderLayout());
       
        JPanel timePanel = new JPanel();
        timePanel.setLayout( new FlowLayout());
       
        timePanel.add(new JComboBox(hours));
       timePanel.add(new JComboBox(minutes));

        timePanel.setBorder( new EmptyBorder(5,5,5,5));
       
        okTimePanel.add(timePanel, BorderLayout.NORTH);
       
        okPanel = new JPanel();
        okPanel.setLayout(new CenterLayout());
        okPanel.add(new JButton(" Ok "));
        okPanel.setBorder(new EmptyBorder(5,5,5,5));
        okTimePanel.add(okPanel, BorderLayout.SOUTH);
       
        border.add( okTimePanel, BorderLayout.CENTER);
   
        this.setLayout( new BorderLayout());
        this.getContentPane().add(border,BorderLayout.CENTER);
        this.setFocusable(true);
       this.pack();
       this.setVisible(true);
       this.toFront();
    }

    private void jButton1_actionPerformed(ActionEvent e)
    {
        System.out.println("Finally works");
    }

}
Andrew Thompson - 04 Oct 2005 03:55 GMT
> I obviously double and tripple checked the code, tried both with letting
> my JDialog implementing the ActionListener interface as well as an
> anonymous inner class.

You are gonna kick yourself when you realise what you
did wrong..

> The thing is, if I just place the button as the top-most component in
> the JDialog, all is fine and dandy. But placing it wihin a JPanel on the
> JDialog it just wont work.
[SNIP]

Your code was difficult to work with, it wold not compile
easily for me, and I had to add a 'main()' and change
components and layouts just to see the problem.  In future,
please consider submitting only SSCCE's, as they are a lot
easier for others to debug.
<http://www.physci.org/codes/sscce.jsp>

Having said that, after a few tweaks to your code, I got
it to run and fixed the problem you saw.  Look carefully
at the comments, and note that *this* is an SSCCE.
<sscce>
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.border.*;
import java.util.*;

public class DateTimeChooser extends JDialog
{
    Vector hours = new Vector();
    Vector minutes = new Vector();
    JButton ok;
    JTextComponent component;
    // cjange to a JPanel for compilation
    // DateChooserPanel dateChooser;
    JPanel dateChooser;
    JPanel border;
    JPanel okTimePanel;
    JPanel okPanel;

    public DateTimeChooser(JDialog owner, JTextComponent component,
String title)
    {
        super(owner);

        this.component = component;
        DecimalFormat twoPlaces = new DecimalFormat("00");
        for(int i = 0; i < 24; i++)
            hours.add(twoPlaces.format(i));
        for(int i = 0; i < 60; i++)
            minutes.add(twoPlaces.format(i));

        //ok.addActionListener(this);
        ok = new JButton("Ok");
        ok.addActionListener(new ActionListener() {
                                 public void actionPerformed(ActionEvent e)
                                 {
                                     jButton1_actionPerformed(e);
                                 }
                             }
        );

        border = new JPanel();

        border.setBorder( BorderFactory.createRaisedBevelBorder());
        border.setLayout(new BorderLayout());

        //super( owner, title, true );
        //dateChooser = new DateChooserPanel();
        // change to a generic panel with borderlayout..
        dateChooser = new JPanel();
        dateChooser.setBorder(new EmptyBorder(5,5,5,5));

        this.setSize(160,120);
        this.setLocation((int)(component.getLocation().getX() +
owner.getX()),

(int)(component.getLocation().getY()+(component.getHeight()) +
owner.getY()));

        this.setUndecorated(true);

        border.add(dateChooser, BorderLayout.WEST);

        okTimePanel = new JPanel();
        okTimePanel.setLayout( new BorderLayout());

        JPanel timePanel = new JPanel();
        timePanel.setLayout( new FlowLayout());

        timePanel.add(new JComboBox(hours));
        timePanel.add(new JComboBox(minutes));

        timePanel.setBorder( new EmptyBorder(5,5,5,5));

        okTimePanel.add(timePanel, BorderLayout.NORTH);

        okPanel = new JPanel(new BorderLayout());
        //okPanel.setLayout(new CenterLayout());

        // you just went to all the trouble ot configure the
        // 'ok' butoon, now you are not going to *use* it???
        //okPanel.add(new JButton(" Ok "));
        okPanel.add(ok);
        okPanel.setBorder(new EmptyBorder(5,5,5,5));
        okTimePanel.add(okPanel, BorderLayout.SOUTH);

        border.add( okTimePanel, BorderLayout.CENTER);

        this.setLayout( new BorderLayout());
        this.getContentPane().add(border,BorderLayout.CENTER);
        this.setFocusable(true);
        this.pack();
        this.setVisible(true);
        this.toFront();
    }

    private void jButton1_actionPerformed(ActionEvent e)
    {
        System.out.println("Finally works");
    }

    public static void main(String[] args) {
        JFrame f  = new JFrame();
        f.pack();
        f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        JDialog d1 = new JDialog(f);
        d1.setVisible(true);
        DateTimeChooser dtc = new DateTimeChooser(d1,
            new JTextArea(), "Problem?");
    }

}
</sscce>
Casper - 04 Oct 2005 16:31 GMT
Andrew Thompson:
> You are gonna kick yourself when you realise what you
> did wrong..

OMFG, can't believe I did not see that. Next time I will take a break
before to post here.

Thanks,
Casper

*Kicks himself*


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.