|
How to create a color chooser in your application |
|
This Java swing tip illustrates a method of creating a color chooser in the
applications. JColorChooser is a component that is new to Swing and there
is no AWT equivalent. It lets the user interactively select a Color. The default behavior is to present a dialog box containing a tabbed pane letting the user choose via swatches, HSB values, or RGB values.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class ShowColors2 extends JFrame {
private JButton changeColor;
private Color color = Color.lightGray;
private Container c;
public ShowColors2()
{
super( "Using JColorChooser" );
c = getContentPane();
c.setLayout( new FlowLayout() );
changeColor = new JButton( "Change Color" );
changeColor.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent e )
{
color =
JColorChooser.showDialog( ShowColors2.this,
"Choose a color", color );
if ( color == null )
color = Color.lightGray;
c.setBackground( color );
c.repaint();
}
}
);
c.add( changeColor );
setSize( 400, 130 );
show();
}
public static void main( String args[] )
{
ShowColors2 app = new ShowColors2();
app.addWindowListener(
new WindowAdapter() {
public void windowClosing( WindowEvent e )
{
System.exit( 0 );
}
}
);
}
}
|
Related Tips
|
Page 1 of 0 ( 0 comments )
You can share your information about this topic using the form below!
Please do not post your questions with this form! Thanks.