• We just launched and are currently in beta. Join us as we build and grow the community.

JColorChooser Component in Java

boobies69

Site Builder
B Rep
0
0
0
Rep
0
B Vouches
0
0
0
Vouches
0
Posts
134
Likes
97
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
Today, i will teach you how to create a program that has the JColorChooser component in Java. We have ColorDialog in VB.NET and C# and this is equivalent to the JColorChooser in Java.

So, now let's start this tutorial!

1. Open JCreator or NetBeans and make a java program with a file name of colorChooser.java.

2. Import java.awt.* and javax.swing.* packages because we will going to have the JColorChooser component in swing and also the JPanel as the container of this.

  1. import

    javax.swing.*

    ;
  2. import

    java.awt.*

    ;

3. Creates an instance of JColorChooser and set Color.RED as the default selected color and named it as jcc. This will be below in your public class colorChooser extends JFrame .

  1. JColorChooser

    jcc =

    new

    JColorChooser

    (

    Color

    .RED

    )

    ;

4. Create a constructor that is the same with the filename and create a panel out of there to hold the JColorChooser and its border.

  1. JPanel

    content =

    new

    JPanel

    (

    )

    ;
  2. content.setLayout

    (

    new

    BorderLayout

    (

    )

    )

    ;
  3. content.add

    (

    jcc, BorderLayout

    .CENTER

    )

    ;

Set window characteristics of the program. This will also add the panel and the jcolorchooser with its title and closing event.

  1. this

    .setContentPane

    (

    content)

    ;
  2. this

    .setTitle

    (

    "JColorChooser Component"

    )

    ;
  3. this

    .setDefaultCloseOperation

    (

    JFrame

    .EXIT_ON_CLOSE

    )

    ;
  4. this

    .pack

    (

    )

    ;

5. In your Main, create a JFrame component that will hold the panel and the JColorChooser component and all other components. This will set the visibility to True.

  1. public

    static

    void

    main(

    String

    [

    ]

    args)

    {
  2. JFrame

    chooser =

    new

    colorChooser(

    )

    ;
  3. chooser.setVisible

    (

    true

    )

    ;
  4. }

Press F5 and run the program.


Output:

color1.png

color2.png

color3.png


Full source code:

  1. import

    javax.swing.*

    ;
  2. import

    java.awt.*

    ;

  3. public

    class

    colorChooser extends

    JFrame

    {
  4. JColorChooser

    jcc =

    new

    JColorChooser

    (

    Color

    .RED

    )

    ;


  5. public

    colorChooser(

    )

    {

  6. JPanel

    content =

    new

    JPanel

    (

    )

    ;
  7. content.setLayout

    (

    new

    BorderLayout

    (

    )

    )

    ;

  8. content.add

    (

    jcc, BorderLayout

    .CENTER

    )

    ;


  9. this

    .setContentPane

    (

    content)

    ;
  10. this

    .setTitle

    (

    "JColorChooser Component"

    )

    ;
  11. this

    .setDefaultCloseOperation

    (

    JFrame

    .EXIT_ON_CLOSE

    )

    ;
  12. this

    .pack

    (

    )

    ;
  13. }


  14. public

    static

    void

    main(

    String

    [

    ]

    args)

    {
  15. JFrame

    chooser =

    new

    colorChooser(

    )

    ;

  16. chooser.setVisible

    (

    true

    )

    ;
  17. }
  18. }

Best Regards,

Engr. Lyndon Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer

If you have some queries, feel free to contact the number or e-mail below.
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]

Add and Follow me on Facebook: https://www.facebook.com/donzzsky

Visit and like my page on Facebook at: https://www.facebook.com/BermzISware


Download
You must upgrade your account or reply in the thread to view hidden text.
 

450,270

322,965

322,974

Top