Question

I am working on an html editing program. In the program I have a function which I have named easy insert. This comes with a <div></div> insert system which also will allow you to automatically write in classes and other similar stuff to the modifiers of the div.

I am having two problems with my code:

  1. the code of which I am returning is code so it contains an apostrophe which makes the code not working.
  2. the return is inside of a button using action listener and action performed. this means that I am having an error in my code which is this. Void methods cannot return a string variable.

Here is my code:

package main;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class InsertSettings {
public static String getSettings(){
    JFrame f = new JFrame("Insert settings");
    
    JTextField classSetting = new JTextField(20);
    
    JButton done = new JButton("Done!");
    
    done.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            return "class=""+classSetting+"" "; //THIS LINE HERE
        }
    });
    
    f.setLayout(new GridLayout(2,1));
    
    f.add(new JLabel("Class:"));
    f.add(classSetting);
    
    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    f.pack();
    f.setVisible(true);
}
}

The code that is calling the getSettings

package main;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;

public class EasyInsert {
public static void start(final JTextArea textArea){
    JFrame f = new JFrame("Easy insert tool");
    
    JButton insertDiv = new JButton("Insert <div> element");
    JButton insertHtmlTag = new JButton("Insert <html> tag");
    JButton insertTestElement = new JButton("Insert testing element");
    
    final String newLnCharacter = "\n";
    
    insertDiv.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            textArea.insert("<div>"+newLnCharacter+newLnCharacter+"</div>",textArea.getCaretPosition());
            textArea.setCaretPosition(textArea.getCaretPosition()-7);
        }
    });
    
    insertHtmlTag.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            textArea.insert("<html>"+newLnCharacter+newLnCharacter+"</html>", textArea.getCaretPosition());
            textArea.setCaretPosition(textArea.getCaretPosition()-8);
        }
    });
    
    insertTestElement.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            String settings = InsertSettings.getSettings();
            textArea.insert("<div "+settings+">"+newLnCharacter+newLnCharacter+"</div>", textArea.getCaretPosition());
            textArea.setCaretPosition(textArea.getCaretPosition()-7);
        }
    });
    
    f.setLayout(new GridLayout(3,1));
    
    f.add(insertDiv, BorderLayout.PAGE_START);
    f.add(insertHtmlTag);
    f.add(insertTestElement);
    
    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    f.pack();
    f.setVisible(true);
}
}
Was it helpful?

Solution

You're trying to return a String form the actionPerformed method, something which can't be allowed, since as the compiler is telling you, it is declared to be void and can return nothing. Instead you should call a method inside of actionPerformed. Please tell us: just where is that String supposed to go? What code calls and displays this JFrame?

Edit: Also as JB Nizet states, if you want to show double quotes in a String, escape it:

String foo = "This is \"foo\" String".

I wonder if what you really want to do is to create a modal JDialog, and have the dialog get user information and then return a String. If so, consider using a JOptionPane.

You also need to extract the text from your JTextField which will require that you call getText() on it.


EDit
Just a JOptionPane should do:

public static String getSettings() {
  String input = JOptionPane.showInputDialog(null, "Class: ",
        "Insert Settings", JOptionPane.QUESTION_MESSAGE);

  return String.format("class=\"%s\"", input);
}

OTHER TIPS

getSettings will create the button and display it, but it won't wait for it to be clicked. Thus, if you're hoping to write a getSettings method that waits for the button and then returns the value in the JTextField, you'll have to do more. You can try a JDialog or JOptionPane.showInputDialog as Hovercraft Full Of Eels suggested. If you don't want to use one of those, you will probably need to do your own thread synchronization. You might want to check out the Concurrency in Swing tutorial. Methods defined for Object like wait and notify could help you too.

P.S. don't call wait from the event dispatch thread, or your program will get stuck.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top