Question

I'm getting this error that's really frustrating me from this bit of script...

    JPanel menu = new JPanel() {
            private static final long serialVersionUID = 1L;
            JTextArea output = new JTextArea(5, 30) {

                /**
                 * 
                 */
                private static final long serialVersionUID = 4714318125998709253L;
                this.setEditable(false);
            };
            JScrollPane scrollPane = new JScrollPane(output);

//          public void run() {
//              System.out.println("lol");
//              JPanel menu = this;
//              JButton restart = new JButton("Restart");
//              menu.add(restart);
//          }

        };

I'm trying to declare setEditable inside of JTextArea output... any ideas?

EDIT: the error is:

Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem: 
    Syntax error on token ";", < expected

    at citadelRPG.Server.createAndShowGUI(Server.java:94)
    at citadelRPG.Server.access$0(Server.java:16)
    at citadelRPG.Server$1.run(Server.java:162)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

appearing on the semi-colon of declaring serialVersionUID.

Was it helpful?

Solution

The statement

this.setEditable(false);

needs to be in method, static initializer or constructor rather than in the class block of the anonymous implementation of the JTextArea output. If you really wish to have this implementation inside the output class, you can override isEditable

@Override
public boolean isEditable() {
   return false;
}

OTHER TIPS

This this.setEditable(false); needs to defined within the context of a constructor or method. It can't be called outside of these contexts.

I find it extremely strange that people insist on implementing classes in this manner, but that's just me.

Your adding no new functionality to the JPanel, so why not just create it, a JTextarea, a JScrollPane and add them altogether. It will make your code more readable and solve silly mistakes like these.

Personally, a better solution would be...

JPanel menu = new JPanel() {
    private static final long serialVersionUID = 1L;
};
JTextArea output = new JTextArea(5, 30) {
    private static final long serialVersionUID = 4714318125998709253L;
};
output.setEditable(false);
JScrollPane scrollPane = new JScrollPane(output);
menu.add(scrollPane);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top