-2

I was trying to write a program which would give me a message when something (xx) in this case is typed, but it doesn't print a message. Can you please tell me why that happens ? I've also checked the code if it's equal to xx when I type it, and it is.

import javax.swing.*;
import java.awt.*;
import javax.swing.text.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
class acc
{
public static void main(String[] args)
{
ramka ramka = new ramka();
panel panel1 = new panel();
ramka.show();
ramka.add(panel1);
}
}
class ramka extends JFrame
{ 
 public ramka()
 {
  setSize(800, 600);   
  setTitle("Super duper programer");
  Container kontener = getContentPane();
  textfield pole = new textfield();
  kontener.add(pole);
  setDefaultCloseOperation(EXIT_ON_CLOSE);  
  sluchacz listener = new sluchacz();
  pole.getDocument().addDocumentListener(listener);
 } 
}
class panel extends JPanel
{
 public void paintComponent(Graphics g)   
 {
  super.paintComponent(g);
 }
}
class textfield extends JTextField
{ 
}
class sluchacz implements DocumentListener 
{
  @Override
  public void changedUpdate(DocumentEvent e)
  {} 
  @Override
    public void removeUpdate(DocumentEvent e){}
  @Override
      public void insertUpdate(DocumentEvent e) 
          {
          try
          {
         String input = e.getDocument().getText(0, e.getDocument().getLength()).trim();
if(input == "xx")
{
    String message = JOptionPane.showInputDialog("You've just typed ''xx''");
}
    }
     catch(BadLocationException ex)
     {   
     }
          }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
honzix
  • 23
  • 3
  • Please no links. We're volunteers, and so you need to put in just a little effort to make it easier to help you. I've loaded your code from the link but you need to format it to make it readable -- again some more effort that you should put into this question. – Hovercraft Full Of Eels Sep 01 '14 at 14:26

1 Answers1

1

Your problem may be here:

if(input == "xx")

don't use == to check for equality of Strings. Use .equals(...).

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373