JColorChooser

The JColorChooser class encapsulates panes you can incorporate into a dialog to allow a color to be chosen. The class also provides static methods that create and display predefined dialogs by calling the method showDialog() as you have done here. The arguments to showDialog() are a reference to the parent component for the dialog, the title for the dialog, and the initial color selection. You can choose a color using any of the three tabs, which provide different mechanisms for defi ning the color that you want. When you click OK, the color that you chose is returned as type Color.

Program

fdfdf



 
 
 

Program Source

import javax.swing.JFrame;
import javax.swing.JColorChooser;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.FlowLayout;
import java.awt.Color;

class JColorChooserTest extends JFrame implements ActionListener{
  
    private JButton jb;
            
    JColorChooserTest()
    {
        setTitle("JColorChooser");
        setLayout(new FlowLayout());
        setJButton();
        setAction();
        setSize(700,450);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    
    void setJButton()
    {
        jb = new JButton("JButton");
        add(jb);
    }
    
    void setAction()
    {
        jb.addActionListener(this);
    }
  
    public void actionPerformed(ActionEvent eve)
    {
        Color col = JColorChooser.showDialog(rootPane, "Choose JButton Background",Color.BLACK);
        jb.setBackground(col);
    }
}

public class Javaapp {
  
    public static void main(String[] args) {
      
        JColorChooserTest jc = new JColorChooserTest();
    }
}

Leave a Comment