Choose foreground or background color : Color Chooser « Swing JFC « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Swing JFC » Color ChooserScreenshots 
Choose foreground or background color
   
/*
 * Created on 17.12.2004
 *
 */

/*
This file is part of BORG.

    BORG is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    BORG is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with BORG; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

Copyright 2003 by Mike Berger
 */


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;


/**
 * GUI control to easy choose foreground or background color.
 * Indicates color to be stored by its own foreground or background.
 
 @author bsv
 
 */
public class JButtonKnowsBgColor extends JButton {
  // colorProperty is ONE color, but can be indicated by fore or back color
  protected Color colorProperty;
  // bg=true means "choosed color is background color"
  // bg=false means "choosed color is foreground color"
  protected boolean bg;
  
  public JButtonKnowsBgColorString p_text, Color p_color, boolean p_bg ){
    setTextp_text );
    setColorPropertyp_color );
    setBgp_bg );
    setColorByProperty();
    addActionListener(new ModalListener());
        
  }
  
  public void setColorByProperty(){
    ifisBg() ){
      setBackgroundgetColorProperty() );
    else {
      setForegroundgetColorProperty() );
    }
  }

  // for testing purposes only
  public static void main(String[] args) {
    JButtonKnowsBgColor jbkbc = new JButtonKnowsBgColor"choose back", Color.RED, true );
    JButtonKnowsBgColor jbkbc1 = new JButtonKnowsBgColor"choose fore", Color.BLUE, false );
    JFrame jf = new JFrame();
    jf.setLayoutnew BorderLayout() );
    jf.getContentPane().addjbkbc, BorderLayout.NORTH );
    jf.getContentPane().addjbkbc1, BorderLayout.CENTER );
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jf.setSize100200 );
    jf.setVisible(true);
  }
  /**
   @return Returns the color.
   */
  public Color getColorProperty() {
    return colorProperty;
  }
  /**
   @param color The color to set.
   */
  public void setColorProperty(Color color) {
    this.colorProperty = color;
  }
  /**
   @return Returns the bg.
   */
  protected boolean isBg() {
    return bg;
  }
  /**
   @param bg The bg to set.
   */
  protected void setBg(boolean bg) {
    this.bg = bg;
  }

  private class ModalListener implements ActionListener{
    public void actionPerformed(ActionEvent event){
      Color selected = JColorChooser.showDialog(
        null, 
        isBg()?"Set background":"Set foreground"
        getColorProperty());
      setColorProperty(selected);
      setColorByProperty();
      }
   }

}

   
    
    
  
Related examples in the same category
1. JColorChooser dialog with a custom preview pane.JColorChooser dialog with a custom preview pane.
2. JColorChooser dialog with the custom GrayScalePanel picker tab.JColorChooser dialog with the custom GrayScalePanel picker tab.
3. A quick test of the JColorChooser dialogA quick test of the JColorChooser dialog
4. ColorChooser Sample 1ColorChooser Sample 1
5. Color Chooser DemoColor Chooser Demo
6. System Color ChooserSystem Color Chooser
7. JColorChooser demoJColorChooser demo
8. ColorChooser Demo 2ColorChooser Demo 2
9. Swing ColorChooser DemoSwing ColorChooser Demo
10. Setting the Order of the Color Chooser Panel Tabs in a JColorChooser Dialog
11. Removing a Color Chooser Panel from a JColorChooser Dialog
12. Preview pane simply displays the currently selected color.
13. Retrieving the Color Chooser Panels in a JColorChooser Dialog
14. Customizing the Preview Panel of a JColorChooser Dialog
15. Getting and Setting the Selected Color in a JColorChooser Dialog
16. Creating a JColorChooser Dialog
17. Listening for Changes to the Selected Color in a JColorChooser Dialog
18. Listening for OK and Cancel Events in a JColorChooser Dialog
19. Removing the Preview Panel from a JColorChooser Dialog
20. Adding a Custom Color Chooser Panel to a JColorChooser Dialog
21. extends AbstractColorChooserPanelextends AbstractColorChooserPanel
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.