I created class of object called JLabeledComponentField as you can see, and I put it in a JPanel with other identical objects. here is the code example
class JLabeledComponentField extends JPanel{
private JLabel jLabel;
private JComponent comp;
private boolean editable;
public JLabeledComponentField(JComponent Comp) {
jLabel = new JLabel();
jLabel.setBorder(BorderFactory.createLineBorder(new Color(122,138,153)));
jLabel.setOpaque(true);
jLabel.setBackground(Color.white);
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
onClicked_Label(jLabel);
comp = Comp;
if((comp.getClass().toString().equals("class FixedLengthTextField"))||(comp.getClass().toString().equals("class javax.swing.JFormattedTextField"))||(comp.getClass().toString().equals("class personalDetails$changedColoredFixedLengthTextField")))
{
((JTextField)comp).setUI(new MetalTextFieldUI());
((JTextField)comp).setBorder(BorderFactory.createLineBorder(new Color(122,138,153)));
}
editable = true;
onExit_TextField();
setLayout(new BorderLayout());
add(jLabel,BorderLayout.CENTER);
}
private void onExit_TextField() {
comp.addFocusListener(new FocusListener(){
public void focusGained(FocusEvent arg0) {
}
public void focusLost(FocusEvent arg0) {
set();
remove(comp);//remove component
add(jLabel, BorderLayout.CENTER);//
//refresh JFrame
revalidate();
repaint();
}
});
}
private void onClicked_Label(JLabel lbl) {
lbl.addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent mouseEvent)
{
if(editable)
{
remove(jLabel);//remove component
add(comp, BorderLayout.CENTER);//
comp.requestFocus();
//refresh JFrame
revalidate();
repaint();
}
}
public void mouseEntered(MouseEvent mouseEvent) {}
public void mouseExited(MouseEvent mouseEvent) {}
public void mousePressed(MouseEvent mouseEvent){}
public void mouseReleased(MouseEvent mouseEvent){}
});
}
private void set()
{
if(comp.getClass().toString().equals("class javax.swing.JComboBox"))
{
String text = ((JComboBox)comp).getSelectedItem().toString();
jLabel.setText(text);
}
if((comp.getClass().toString().equals("class FixedLengthTextField"))||(comp.getClass().toString().equals("class javax.swing.JFormattedTextField"))||(comp.getClass().toString().equals("class personalDetails$changedColoredFixedLengthTextField")))
{
String text = ((JTextField)comp).getText();
jLabel.setText(text);
}
}
public void setColor(String text)
{
if(text.equals(""))
changeColor();
else
returnColor();
}
public void changeColor()
{
jLabel.setBackground(Color.yellow);
comp.setBackground(Color.yellow);
}
public void returnColor()
{
jLabel.setBackground(Color.gray);
comp.setBackground(Color.gray);
}
public void setSelectedItem(String text)throws Exception
{
if(comp.getClass().toString().equals("class javax.swing.JComboBox"))
{
jLabel.setText(text);
((JComboBox)comp).setSelectedItem(text);
}
else
throw new Exception("Exception occered : it's not JComboBox, its "+comp.getClass().toString());
}
public void setText(String text)throws Exception
{
if((comp.getClass().toString().equals("class FixedLengthTextField"))||(comp.getClass().toString().equals("class javax.swing.JFormattedTextField"))||(comp.getClass().toString().equals("class personalDetails$changedColoredFixedLengthTextField")))
{
jLabel.setText(text);
((JTextField)comp).setText(text);
jLabel.setToolTipText(text);
((JTextField)comp).setToolTipText(text);
}
else
throw new Exception("Exception occered : it's not JTextField, its "+comp.getClass().toString());
}
public String getSelectedItem()throws Exception
{
if(comp.getClass().toString().equals("class javax.swing.JComboBox"))
return ((JComboBox)comp).getSelectedItem().toString();
else
throw new Exception("Exception occered : it's not JComboBox, its "+comp.getClass().toString());
}
public String getText()throws Exception
{
if((comp.getClass().toString().equals("class FixedLengthTextField"))||(comp.getClass().toString().equals("class javax.swing.JFormattedTextField"))||(comp.getClass().toString().equals("class personalDetails$changedColoredFixedLengthTextField")))
return jLabel.getText();
else
throw new Exception("Exception occered : it's not JTextField, its "+comp.getClass().toString());
}
public void setEditable(boolean bool)throws Exception
{
if((comp.getClass().toString().equals("class FixedLengthTextField"))||(comp.getClass().toString().equals("class javax.swing.JFormattedTextField"))||(comp.getClass().toString().equals("class javax.swing.JComboBox"))||(comp.getClass().toString().equals("class personalDetails$changedColoredFixedLengthTextField")))
{
editable = bool;
SetEnabled(bool);
}
else
throw new Exception("Exception occered : it's not selected component, its "+comp.getClass().toString());
}
public void SetEnabled(boolean bool)throws Exception
{
if((comp.getClass().toString().equals("class FixedLengthTextField"))||(comp.getClass().toString().equals("class javax.swing.JFormattedTextField"))||(comp.getClass().toString().equals("class javax.swing.JComboBox"))||(comp.getClass().toString().equals("class personalDetails$changedColoredFixedLengthTextField")))
{
jLabel.setEnabled(bool);
}
else
throw new Exception("Exception occered : it's not selected component, its "+comp.getClass().toString());
}
public void SetFocus()
{
if(editable)
{
remove(jLabel);//remove component
add(comp, BorderLayout.CENTER);//
comp.requestFocus();
//refresh JFrame
revalidate();
repaint();
}
}}
and as you can see after I have the code,I call it and put the object in the jpanel.
JLabeledComponentField name = new JLabeledComponentField(new JTextfield());
JP.add(name);
Its working fine and really clean but... Then when I double click on the component and the JTextfield replace thr JLabel, the text somehow disappear in about 50% of cases, which don't supose to happen and it looks like it goes to some kind of zone that we still can see the JTextfield but without the text. I need an expert opinion that will advise me what to do. Any Idea ?