Wednesday, August 28, 2013

JColorChooser

JColorChooser represents a a dialog that you can manipulate and select a color value. JColorChooser object has its own model that handles color selection. This model can generate the change event. This event occurs when a color is selected. So you need to implement the ChangeListener interface to receive the event. Its useful constructors and methods are shown below.

Constructors:
-JColorChooser()
creates a color chooser without initial color.
-JColorChooser(Color c)
creates a color chooser with the initial color.
Methods:
-getColor():Color
gets the color from the color chooser.
-getSelectionModel():ColorSelectionModel
returns the model that handle the color selection.
-setColor(Color c):void
sets the color to the color chooser.
-showDialog(Component parent,String title, Color initColor): static Color displays the color-chooser dialog.

Example:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.colorchooser.DefaultColorSelectionModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

class JColorChooserShow extends JFrame implements ChangeListener{
JLabel lbl;
JColorChooserShow(String title){
setTitle(title);
setSize(550,500);
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contpane=getContentPane();
contpane.setLayout(new BorderLayout());
JColorChooser chooser=new JColorChooser(Color.BLACK);
chooser.getSelectionModel().addChangeListener(this);
contpane.add(chooser,BorderLayout.NORTH);
lbl=new JLabel("Select a color to see the change",JLabel.CENTER);
lbl.setFont(new Font("Arial",Font.PLAIN, 30));
contpane.add(lbl,BorderLayout.CENTER);
setVisible(true);
}

public void stateChanged(ChangeEvent e){
DefaultColorSelectionModel model=(DefaultColorSelectionModel)e.getSource();
lbl.setForeground(model.getSelectedColor());

}

}
public class JFrameSwing {
public static void main(String[] args){
new JColorChooserShow("JColorChooser");
}
}

JColorChooser Swing


If you want to display a color chooser in a separate widow, you need to use the showDialog(Component parent, String title,Color initColor) method. It is a static method. So, you no need to create an instance of the JColorChooser. This method returns the color that is selected.

Example:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

class JColorChooserShow extends JFrame{
JTextArea txta;
JColorChooserShow(String title){
setTitle(title);
setSize(550,300);
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contpane=getContentPane();
contpane.setLayout(new BorderLayout());
JButton btcolor=new JButton(new AAction("",new ImageIcon("d:/coloricon.png")));
btcolor.setBackground(Color.WHITE);
JPanel p=new JPanel(new FlowLayout(FlowLayout.LEFT));
p.add(btcolor);
contpane.add(p,BorderLayout.NORTH);
txta=new JTextArea(10,30);
txta.setLineWrap(true);
JScrollPane scroll=new JScrollPane(txta);
contpane.add(scroll,BorderLayout.CENTER);
setVisible(true);

}
class AAction extends AbstractAction{

AAction(String text,Icon c){
super(text, c);

}
public void actionPerformed(ActionEvent e){
Color c=JColorChooser.showDialog(null, "Select a color", Color.BLACK);
txta.setForeground(c);
}
}

}

public class JFrameSwing {
public static void main(String[] args){
new JColorChooserShow("JColorChooser");
}
}

No comments:

Post a Comment