Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Adding a Container Listener to a panel  RSS feed

 
Kiran Chhabra
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to add a Container listener to a panel. But am unable to catch the events. Can someone point me as to what I am missing?

Please find the code appended
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SwingSample implements ContainerListener{
/**
* Sample swing application to try various functions on Dialog Boxes
* @author kiranc
* @version June 10
*/
JFrame jF = new JFrame();
JPanel panel = new JPanel();
JTextField txtfield = new JTextField (10);
JButton OkButton = new JButton ("Click Me");
JLabel Login = new JLabel ("LogIn:");


public void componentAdded(ContainerEvent e)
{
e.getChild();
System.out.println("Added");
}
public void componentRemoved(ContainerEvent e)
{
e.getChild();
System.out.println("Removed");
}
public SwingSample() {
panel.setLayout(null);
Login.setBounds (200, 150, 40,40);
panel.add(Login);
txtfield.setBounds (250, 160, 60,20);
panel.add(txtfield);
OkButton.setText("click");
OkButton.setBounds (225, 200, 100,140);
OkButton.setPreferredSize(new Dimension(200, 80));
panel.add(OkButton, BorderLayout.CENTER);
JComponent newContentPane = panel;
newContentPane.setOpaque(true); //content panes must be opaque
jF.setContentPane(panel);

jF.addContainerListener(this);

}


public static void main (String []args) {
SwingSample lgform = new SwingSample ();

lgform.jF.pack();
lgform.jF.setVisible(true);

System.out.println("Printing before component is added: dialog");
JOptionPane.showMessageDialog(lgform.jF, "Message");
}
}
 
Kiran Chhabra
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just FYI..I am able to fire ContainerListener for components like buttons but not for Dialog boxes.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Umm, shouldn't you be invoking panel.addContainerListener and not jF.addContainerListener?

edit Hold it. A dialog is not added to its parent component.
[ June 16, 2008: Message edited by: Darryl Burke ]
 
Kiran Chhabra
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Daren,
Is there another way I can capture the dialog boxes that come up or go?
Regards
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps ComponentListener will work.
 
Kiran Chhabra
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I tried that but the dialog boxes are not getting captured. I am basically looking to capture the dialogs that pop up. Similar to what getWindows does in 1.6
Please find the code using ComponentListener appended.

public class SwingSample implements ContainerListener, ComponentListener, ActionListener{
/**
* Sample swing application to try various functions on Dialog Boxes
* @author kiranc
* @version June 19
*/
JFrame jF = new JFrame();
JPanel panel = new JPanel();
JTextField txtfield = new JTextField (10);
JButton OkButton = new JButton ("Click Me");
JLabel Login = new JLabel ("LogIn:");
JDialog jD = new JDialog(jF,"Title: test");

public void actionPerformed(ActionEvent e){
System.out.println("ButtonClicked");
JOptionPane.showMessageDialog(panel, "Did the component event happen?");
panel.revalidate();
}
public void componentMoved(ComponentEvent e)
{
System.out.println("Moved");
}
public void componentHidden(ComponentEvent e)
{
System.out.println("Hidden");
}
public void componentResized(ComponentEvent e)
{
System.out.println("Resized");
}
public void componentShown(ComponentEvent e)
{
System.out.println("Shown");
}
public void componentAdded(ContainerEvent e)
{
e.getChild();
System.out.println("Added");
}
public void componentRemoved(ContainerEvent e)
{
e.getChild();
System.out.println("Removed");
}
public SwingSample() {
panel.setBounds(0, 0, 1400, 1500);
Login.setBounds (200, 150, 40,40);
panel.add(Login);
txtfield.setBounds (250, 160, 60,20);
panel.add(txtfield);
OkButton.setText("click");
OkButton.setBounds (225, 200, 10,40);
OkButton.setPreferredSize(new Dimension(200, 80));
OkButton.addActionListener(this);
panel.add(OkButton, BorderLayout.CENTER);
JComponent newContentPane = panel;
newContentPane.setOpaque(true); //content panes must be opaque
jF.setContentPane(panel);
jF.addComponentListener(this);
//panel.addContainerListener(this);
panel.addComponentListener(this);
}


public static void main (String []args) {
SwingSample lgform = new SwingSample ();


lgform.jF.pack();
lgform.jF.setVisible(true);

System.out.println("Printing before confirm dialog");
/* winArray= Window.getWindows();
for(int i =0;i < winArray.length;i++){
System.out.println(i+":"+winArray[i].toString());
}*/
//Dialog box 1 with null parent
JOptionPane.showConfirmDialog(null, "Confirm");
//lgform.jF.update(Graphics g);
System.out.println("Printing after confirm dialog");

//Dialog box 2 with parent
JOptionPane.showMessageDialog(lgform.panel, "Message");
lgform.panel.revalidate(); //trying to kinda refresh.
System.out.println("Printing after Message dialog");

JButton newButton = new JButton("JButton #");
lgform.panel.add(newButton);
lgform.panel.revalidate(); //Makes the button show up.
}
}

}
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!