0

I'm making an application with calculating tax for my computer science classes. I am only at the beggining, so it does not have much code. I want text in JTextField "nazwa" and "netto" disappear, when a user wants to type text in it. I think I did it correctly, but it doesn't work :( I will not include my whole code, but most of it, because I don't know, where the mistake might be.

Instead of MouseEvent I have tried FocusEvent, but it didn't work either.

public class Podatek3 extends JFrame implements ActionListener, MouseListener {

    JButton oblicz;
    JTextField nazwa, netto;
    JTextArea wynik;
    JRadioButton pierwszybutton, drugibutton, trzecibutton;
    static String pierwszy = "8%";
    static String drugi = "12%";
    static String trzeci = "23%";
    static double result;

    Podatek3(){
        super("Aplikacja do obliczania podatku");
        JLayeredPane ekran = new JLayeredPane();
        getContentPane().add(ekran);
        ekran.setPreferredSize(new Dimension(400,300));
        ekran.setBackground(Color.blue);

        nazwa = new JTextField("nazwa produktu");
        ekran.add(nazwa);
        nazwa.setBounds(10, 10, 100, 30);
        nazwa.addFocusListener((FocusListener) this);
        nazwa.setBackground(Color.pink);
        nazwa.setEditable(true);

        netto = new JTextField("cena netto");
        ekran.add(netto);
        netto.setBounds(120, 10, 100, 30);
        netto.addMouseListener(this);
        netto.setBackground(Color.pink);


        pierwszybutton = new JRadioButton(pierwszy);
        ekran.add(pierwszybutton);
        pierwszybutton.setMnemonic(KeyEvent.VK_B);
        pierwszybutton.setActionCommand(pierwszy);


        drugibutton = new JRadioButton(drugi);
        ekran.add(drugibutton);
        drugibutton.setMnemonic(KeyEvent.VK_C);
        drugibutton.setActionCommand(drugi);


        trzecibutton = new JRadioButton(trzeci);
        ekran.add(trzecibutton);
        trzecibutton.setMnemonic(KeyEvent.VK_D);
        trzecibutton.setActionCommand(trzeci);

        ButtonGroup group = new ButtonGroup();
        group.add(pierwszybutton);
        group.add(drugibutton);
        group.add(trzecibutton);

        JPanel radioPanel = new JPanel(new GridLayout(3,1));
        ekran.add(radioPanel);
        radioPanel.setBounds(230, 10, 60, 50);
        radioPanel.add(pierwszybutton);
        radioPanel.add(drugibutton);
        radioPanel.add(trzecibutton);



        oblicz = new JButton("Oblicz");
        ekran.add(oblicz);
        oblicz.setBounds(290, 10, 100, 30);
        oblicz.setBackground(Color.pink);
        oblicz.addActionListener(this);

        wynik = new JTextArea();
        ekran.add(wynik);
        wynik.setBounds(10, 70, 380, 220);
        wynik.setBackground(Color.pink);
        wynik.setEditable(false);   
    }


    public static void main(String[] args) {
        JFrame okno = new Podatek2();
        okno.setVisible(true);
        okno.setDefaultCloseOperation(EXIT_ON_CLOSE);
        okno.setLocationRelativeTo(null);
        okno.pack();

    }

    public void actionPerformed1(ActionEvent e) {



    }

    public void mouseClicked2(MouseEvent arg0) {
        Object klik = arg0.getSource();
        if (klik == nazwa) {
        nazwa.setText("");
        }

        }
    public void mouseClicked3(MouseEvent arg1) {
        Object klik1 = arg1.getSource();
        if (klik1 == netto) {
            netto.setText("");
        }
    }
Samuel Philipp
  • 10,631
  • 12
  • 36
  • 56
  • Possible duplicate of [java swing JTextField set PlaceHolder](https://stackoverflow.com/questions/16213836/java-swing-jtextfield-set-placeholder) – sjr Apr 07 '19 at 14:07
  • As @sjr mentioned, your question has been asked and answered a few times already on _StackOverflow_. However, this Web page helped me to implement a "hint" `JTextField`: [Java Swing : Hint Text on a JTextField](https://blogs.sequoiainc.com/java-swing-hint-text-on-a-jtextfield/) – Abra Apr 07 '19 at 18:14

0 Answers0