How can you use file choosers and color choosers in Swing? Explain with suitable example,
Using/ Creating Color Choosers
We can use JColorChooser class to enable users to choose from a palette of colors. A color chooser is a component that we can place anywhere within our program GUI. The JColorChooser API also makes it easy to bring up a model or non-modal dialog that contains a color-chooser. We can create color-chooser using any one of the following constructors:
JColor Chooser();
JColor Chooser(intial_color);
Once an object of JColorChooser class is created, we can display it in a dialog box by calling its show Dialog() method. Signature of this method is as below:
Color show Dialog(parent, title, intial_color)
Example
import javax.swing.*;
import java.awt.*;
import java.awt.event. *;
class DialogDemo extends JFrame
{
JColorChooser cc;
JPanel p;
JDialog d;
DialogDemo()
{
setSize(700, 500);
setDefault LokAnd FeelDecorated (true);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
setTitle("Paraent Frame");
p-new JPanel();
add (p);
setVisible(true);
cc = new JColorChooser();
Color c=cc.showDialog (this, "Select new color....", Color.white);
p.setBackground (c);
}
public static void main(String args[])
{
DialogDemo frame=new DialogDemo();
}
}
Comments
Post a Comment