lunes, 12 de julio de 2010

Agregar Panel de color

private JLabel assignedBackground;
private Color elegido;
private Color original;

/*............................*/

private void agregarPanelColor(){
final JPanel colorPanel = new JPanel();
colorPanel.add(new JLabel("Color asignado"));
assignedBackground = new JLabel();
assignedBackground.setOpaque(true);
assignedBackground.setPreferredSize(new Dimension(20, 15));
original = assignedBackground.getBackground();
colorPanel.add(assignedBackground);

JButton colorButton = new JButton("Seleccionar..");
colorButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {
Color actual = null;
if (elegido != null) {
actual = elegido;
} else if (entidad != null && entidad.getColor() != null) {
actual = new Color(Integer.parseInt(entidad.getColor(), 16));
}
elegido = JColorChooser.showDialog(EstadoComandoWindows.this, "Seleccionar el color para el estado.", actual);
assignedBackground.setBackground(elegido);
colorPanel.repaint();
}

});
colorPanel.add(colorButton);
componentes.add(colorPanel);
}

@Override
public EstadoComanda getEntidad() {
EstadoComanda e = super.getEntidad();
if (elegido == null) {
e.setColor(null);
} else {
e.setColor(ColorUtil.toHexString(elegido));
}
return e;
}

@Override
public void setEntidad(EstadoComanda entidad) {
super.setEntidad(entidad);
if(entidad != null){
if (entidad.getJavaColor() != null) {
elegido = entidad.getJavaColor();
assignedBackground.setBackground(elegido);
} else {
elegido = null;
assignedBackground.setBackground(original);
}
}
}