1 54 55 package com.mullassery.act.gui.components; 56 57 import java.awt.BorderLayout ; 58 import java.awt.Color ; 59 import java.awt.Dimension ; 60 import java.awt.event.ActionEvent ; 61 import java.awt.event.ActionListener ; 62 63 import javax.swing.JButton ; 64 import javax.swing.JColorChooser ; 65 import javax.swing.JPanel ; 66 67 import com.mullassery.act.gui.TextData; 68 69 74 public class TColorField extends JPanel implements TextData { 75 private JColorChooser jc; 76 private JButton jb; 77 private TTextField colorPanel; 78 private Color lastColor; 79 80 public TColorField() { 81 this("0xffffff"); 82 } 83 84 public TColorField(final String color) { 85 colorPanel = new TTextField(color); 86 setText(color); 87 jb = new JButton ("..."); 88 jb.addActionListener(new ActionListener () { 89 public void actionPerformed(ActionEvent ae) { 90 if (jc == null) { 91 jc = new JColorChooser (); 92 } 93 jc.setColor(lastColor); 94 lastColor = JColorChooser.showDialog(TColorField.this, "Select a Color", lastColor); 95 colorPanel.setText(getText()); 96 } 97 98 }); 99 setPreferredSize(new Dimension (200, 20)); 100 this.setLayout(new BorderLayout ()); 101 this.add(colorPanel, BorderLayout.CENTER); 102 this.add(jb, BorderLayout.EAST); 103 } 104 105 public String getText() { 106 return Integer.toHexString(lastColor.getRGB()); 111 } 112 113 public void setText(String value) { 114 try { 115 lastColor = new Color (Integer.parseInt(value)); 116 colorPanel.setBackground(lastColor); 117 } catch (NumberFormatException e) { 118 } 119 } 120 } 121 | Popular Tags |