0

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 ?

amnon
  • 97
  • 1
  • 8
  • Methods names should NOT start with an upper case character. In your last question (https://stackoverflow.com/questions/52884348/how-to-put-a-component-at-the-center-of-other-component-that-sit-in-a-jpanel) you were asked to post a proper [mcve] demonstrating the problem. An "MCVE" should be posted with every question. You still haven't "accepted" the answer from that question and you still haven't posted an "MCVE" in either question, so I'll skip this because we don't have time to keep guessing what you are doing. – camickr Oct 22 '18 at 18:47
  • I wonder why do you have to make so many castings to JCombobox or JTextField - result of bad design I think. – Antoniossss Oct 31 '18 at 22:31
  • well, I think is not enhough, I am looking for a comment thet will point on the problem. – amnon Nov 01 '18 at 23:04
  • `I am looking for a comment thet will point on the problem` - you got the comment 10 days ago. Your question doesn't make sense. We can't see the behaviour or your code since you still haven't posted an [mcve]. And you still haven't fixed your method names. – camickr Nov 02 '18 at 21:13

0 Answers0