Kiran Chhabra

Greenhorn
+ Follow
since Jun 13, 2008
Merit badge: grant badges
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Kiran Chhabra

Hi,
In our application, we need to know and capture when a dialog box pops up. getWindows() in JDK 1.6 does that for us, but we need to do the same in 1.5 too. Is there a way to capture dialog boxes.
I have tried component and container listeners on a panel/frame but that does not work
Regards
Kiran
15 years ago
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.
}
}

}
15 years ago
Hi Daren,
Is there another way I can capture the dialog boxes that come up or go?
Regards
15 years ago
Just FYI..I am able to fire ContainerListener for components like buttons but not for Dialog boxes.
15 years ago
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");
}
}
15 years ago