> Monique Y. Mudama:
>
>> Erm, you call jButtonOk_actionPerformed, but you show
>> jButton1_actionPerformed ...
>
> Formatting typo.
>..jButtonOk is the name used consequently.
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*