I have to write a program in which JColorChooser is opened twice which will allow the user to select background and foreground colors. However, if the user selects the colors which are too similar the program should throw out an error message and open the JColorChoosers again...
I know that I have to use the VetoableChangeListener but I have no idea how to implement it into my code...
Below the code which I have up to this point.
Help would be greatly appreciated
package test;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class jcolortest{
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
public jcolortest(){
prepareGUI();
}
public static void main(String[] args){
jcolortest swingControlDemo = new jcolortest();
swingControlDemo.showColorChooserDemo();
}
private void prepareGUI(){
mainFrame = new JFrame("JColorChooser");
mainFrame.setSize(400,200);
mainFrame.setLayout(new GridLayout(3, 1));
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
headerLabel = new JLabel("", JLabel.CENTER);
statusLabel = new JLabel("",JLabel.CENTER);
statusLabel.setSize(350,100);
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
private void showColorChooserDemo(){
headerLabel.setText("Test foreground text");
headerLabel.setForeground(Color.RED);
Color backgroundColor = JColorChooser.showDialog(mainFrame,
"Background Color", Color.white);
if(backgroundColor != null){
controlPanel.setBackground(backgroundColor);
mainFrame.getContentPane().setBackground(backgroundColor);
}
Color foregroundColor = JColorChooser.showDialog(mainFrame,
"Text Color", Color.white);
if(foregroundColor != Color.RED){
controlPanel.setForeground(foregroundColor);
headerLabel.setForeground(foregroundColor);
}
mainFrame.setVisible(true);
}
}
In such case I would propably use something like that:
and add mathod:
nothing fancy, and it is rather primitive, but it works in some degree. The problem is to set "sensitivity" on a satisfying level.