Refer following example of SwingUtilities class of java:
Code:
import javax.swing.SwingUtilities;
import javax.swing.JFrame;
public class Main extends JFrame
{
public Main()
{
this.setDefaultCloseOperation(JFrame.EXIT_CLOSE);
this.setSize(500, 200);
this.setLayout(new FlowLayout(FlowLayout.LEFT));
JButton button1 = new JButton("Change Color");
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Component component4 = (Component) e.getSource();
JFrame frame3 = (JFrame) SwingUtilities.getRoot(component4);
frame3.setBackground(Color.BLUE);
}
});
this.getContentPane().add(button);
}
public static void main(String[] ars) {
new Main().setVisible(true);
}
}
Bookmarks