I have 2 frames from different class, one is the main frame containing just a jbutton and jtextfield, and the other one contains a the setting to change the background color of the first frame, so i've a problem because when i clicked the the button OK button the color changed but it created a new main frame with new change instead of applying the change in the active main frame.
here is the main frame code
package com.srccodes.example;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Cheb extends JFrame implements ActionListener {
public static void main(String[] args) {
// TODO Auto-generated method stub
Cheb cheb = new Cheb();
}
JTextArea about = new JTextArea(3,30);
JButton callB = new JButton("Settings");
Cheb(){
setLayout(new FlowLayout(FlowLayout.CENTER, 20,25));
setSize(400,400);
setTitle("Check Box");
add(new Label("About you"));
JScrollPane aboutScroll= new JScrollPane(about);
add(aboutScroll);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(callB);
setVisible(true);
callB.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
//Countinent selection with Check Box
if(e.getSource() == callB)
{
CollorClass cl =new CollorClass();
}
}
}
here is the code to change the color of the main frame
package com.srccodes.example;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class CollorClass extends JFrame implements ActionListener {
public static void main(String[] args) {
// TODO Auto-generated method stub
CollorClass cc = new CollorClass();
}
JLabel bg = new JLabel("Background Color");
String colorName[] = {"None","RED","GREEN","BLACK","BLUE","YELLOW","WHITE"};
JComboBox bgColor = new JComboBox(colorName);
JButton ok = new JButton("OK");
CollorClass()
{
setLayout(new FlowLayout(FlowLayout.CENTER, 20,25));
setSize(400,400);
setTitle("Check Box");
add(bg);
add(bgColor);
add(ok);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
ok.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource() == ok)
{
Cheb cheb = new Cheb();
String SelectedValue = bgColor.getSelectedItem().toString();
switch (SelectedValue){
case "RED" :
cheb.about.setBackground(Color.red);
System.out.print(SelectedValue);
break;
case "YELLOW" :
cheb.about.setBackground(Color.yellow);
System.out.print(SelectedValue);
break;
case "BLUE" :
cheb.about.setBackground(Color.blue);
System.out.print(SelectedValue);
break;
case "GREEN" :
cheb.about.setBackground(Color.green);
System.out.print(SelectedValue);
break;
case "BLACK" :
cheb.about.setBackground(Color.black);
System.out.print(SelectedValue);
break;
case "WHITE" :
cheb.about.setBackground(Color.white);
System.out.print(SelectedValue);
break;
default:
System.out.print(SelectedValue);
}
}
}
}