0

I'm trying to output a message dialog box as a result of an Action Listener in my program. I keep receiving an error that says 'Uncompilable source code - incompatible types: void cannot be converted to java.lang.String'. I can't figure out what I'm doing wrong, especially when I can get the Input dialog and Confirm dialog boxes to come up the way I want the Message dialog boxes to, using pretty much the same code. Here's the code I used...

    //add an ActionListener for Search Menu Item
srch.addActionListener(new ActionListener(){ 
               @Override 
        public void actionPerformed(ActionEvent e) {

                if(database_name == null){

                   error = JOptionPane.showMessageDialog(null, "This is an Error Message", "Error", JOptionPane.ERROR_MESSAGE);

                }
                else if(database_name != null){
                    jta.setText("It works");
                }//end if statement

            }//end actionPerformed
        });//end Search menuItem action listener 
Chandrayya G K
  • 8,719
  • 5
  • 40
  • 68

2 Answers2

3

just as the error says, the error variable is most likely a String whereas JOptionPane.showMessageDialog doesn't return a value, as you can see in the docs

if(database_name == null) {
    JOptionPane.showMessageDialog(null, "This is an Error Message", "Error", JOptionPane.ERROR_MESSAGE);
}

will work fine.

Wim Ombelets
  • 5,097
  • 3
  • 39
  • 55
0

You probably do not need to do this the way you are doing it. The actionListener in Java do not need the use of adapter class as its got only one method (i.e. actionperformed()). Try doing it without the use of adapter and writing srch.addActionListener(this); instead of the first line and then specify the actionPerformed() seperately.

Parth
  • 568
  • 2
  • 6
  • 23