0

I'm making a simple kg to pounds converter and I have a textfield which says "kgs here" and when the user types the input, it's getting appended, but not overwritten. Please help.

 import java.awt.Color;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
        public class UnitConvertKg {
        JFrame f;
        JPanel p;
        JButton b1;
        JTextField tf1,tf2;
        UnitConvertKg(){
            f=new JFrame("UnitConvertKg");
            p=new JPanel();
           tf1=new JTextField("Kg here");
    //this is predefined text and I want that this gets overwritten when t   //user types in kgs.

           tf2=new JTextField("             ");

           b1=new JButton("CONVERT");
           b1.addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent e){
                   double d = Double.parseDouble(tf1.getText())*2.204;
                   tf2.setText(String.valueOf(d));
               }
           });
           p.add(tf1);
           p.add(tf2);
           p.add(b1);
           p.setForeground(Color.blue);
           f.add(p);
           f.setSize(400,400);
           }
cc959
  • 103
  • 1
  • 12
Ms.Peach
  • 1
  • 2
  • 3
    You need to add placeholder in your field instead of text. see https://stackoverflow.com/questions/13033600/java-placeholder-on-textfield and https://stackoverflow.com/questions/16213836/java-swing-jtextfield-set-placeholder. Hope it will help – Preet Aug 29 '17 at 05:55
  • Place holder for JTextField:-https://stackoverflow.com/questions/36070306/placeholder-in-jtextfield-java-swing – AJ. Aug 29 '17 at 06:06
  • If this is not a duplicate, please [edit] your question to include a [mcve] that shows your chosen approach. – trashgod Aug 29 '17 at 10:34

2 Answers2

0

Try this and see if it does the job:

tf1.addMouseListener(new MouseListener() {
    @Override
    public void mouseClicked(MouseEvent e) {
    tf1.setText("");
    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {
    if(tf1.getText().equals("KG here") || tf1.getText().isEmpty())
        tf1.setText("KG here");
    }
});
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
-1

do you mean is like hint in android and placeholder in HTML?

tf1.addMouseListener(new MouseAdapter(){
@Override
    public void mouseClicked(MouseEvent e){
        tf1.setText("");
    }
});
always-a-learner
  • 3,671
  • 10
  • 41
  • 81
M. Alwi
  • 1
  • 1
  • No, this should not be a solution. Because, every time I want to update the text, it will remove the older text. – AJ. Aug 29 '17 at 06:03
  • tf1.addMouseListener(new MouseAdapter(){ @Override public void mouseClicked(MouseEvent e){ String oldText = tf1.getText(); if(oldText.toUpperCase().equals("KG HERE")){ tf1.setText(""); }else{ tf1.setText(oldText); } } }); – M. Alwi Aug 29 '17 at 06:38
  • Update your answer. But still Placeholder is the best solution. – AJ. Aug 29 '17 at 07:01