0

I'm trying to store lines of code as an array of strings to use throughout my code, for both readability and quicker writing.

Though I am a newbie to Java, learning this will help a lot in the future. Here is my code:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

    public class GUI extends JFrame {       
        
static Color muis = Color.decode("#433333"); 
//I set the theme here to use throughout; 
    public class setFormat {
        public String[][] catFormat = 
            {   { "" ,  //Button layout(theme) & 0,0 placeholder
            "(catFormat[0][0]).setBorderPainted(false)%n;",
            "(catFormat[0][0]).setBackground(muis)%n;",
            "(catFormat[0][0]).setForeground(Color.WHITE)%n;",
            "(catFormat[0][0]).setPreferredSize(new Dimension(151, 35));"}
            ,   {        //Menu layout(theme)
            "(catFormat[0][0]).setVisible(true)%n;",
            "(catFormat[0][0]).setSize(150, 320)%n",
            "(catFormat[0][0]).setLayout(new FlowLayout(FlowLayout.RIGHT, 20, 15) );%n",
            "(catFormat[0][0]).setResizable(false);%n",
            "(catFormat[0][0]).getContentPane().setBackground(muis);%n",
            "(catFormat[0][0]).setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);%n" } 
            };  };
        

    public JFrame  catmenuf = new JFrame(); {
            catmenuf = new JFrame();
            setFormat catmenu = new setFormat();
            catmenu.catFormat[0][0] = "generalb";
            catmenu.catFormat[0][1];
    }
        final JButton generalb = new JButton("General"); {
            setFormat general = new setFormat();
            general.catFormat[0][0] = "generalb";
            general.catFormat[0][1];    
            catmenuf.add(generalb);
        }
        final JButton skillsb = new JButton("Skills"); {
            setFormat skills = new setFormat();
            skills.catFormat[0][0] = "skillsb";
            skills.catFormat[0][1];
            catmenuf.add(skillsb);  
        }
        final JButton otherb = new JButton("Other"); {
            setFormat other = new setFormat();
            other.catFormat[0][0] = "otherb";
            other.catFormat[0][1]
            catmenuf.add(otherb);   
        }
        
        
        public static void main(String[] args) {
            new GUI();
        }
}
        

Here are the errors I get:

Eclipse IDE gives me the error:

Syntax error, insert "AssignmentOperator Expression" to complete Expression Google suggests this is a vague error in which the fix varies a lot and depends on the specific circumstances

This is from the line: catmenu.catFormat[0][1]; and every catFormat[0][1]; line onwards

In the "code snippet" tool here on SO I get:

{
  "message": "Uncaught SyntaxError: Cannot use import statement outside a module",
  "filename": "https://stacksnippets.net/js",
  "lineno": 72,
  "colno": 9
}
  • I don't know what this is though, but I thought it might help a little bit more than what I have from the IDE.

I can't figure this out myself. Any help is appreciated.

Bashir
  • 2,057
  • 5
  • 19
  • 44
Hobbyist
  • 11
  • 2
  • 3
    You need to learn about *methods*, and how to use them to do the same, or similar, operations in different situations. – Raedwald Dec 15 '20 at 09:12
  • 1
    "_In the "code snippet" tool here on SO I get_" The code snippet tool does not support Java. – Ivar Dec 15 '20 at 09:13
  • 1
    In Java, you cannot store code as String and then execute it. You can print or save strings, count characters, find substrings, and so on, but you cannot treat a string as code and execute that code. There are languages where this works, but not Java. What you're trying to achieve, is done in Java by defining methods. – Ralf Kleberhoff Dec 15 '20 at 09:33
  • 1
    While you _could_ use Java strings and `JavaCompiler` to generate and compile code at runtime this isn't what you should even begin trying to do with you being a Java newbie. Focus on the basic and most important design elements such as methods, fields, inheritance, overriding and overloading etc. - once you have a good grasp of the basics and still find a _real_ need for generating code at runtime you can revisit that topic. – Thomas Dec 15 '20 at 09:52
  • Thanks guys, I'll look into methods and use it to get the tool up and running ASAP. I would really like to learn how to do this, @Thomas - even if it's too advanced for me, I've done similar things with the AHK projects which got me interested in Java in the first place. And I don't mind learning. For now, though, methods. Thanks all! – Hobbyist Dec 15 '20 at 19:04
  • You're welcome. I guess that with AHK you refer to Autohotkey. If so keep in mind that this is a scripting language that should be interpreted while Java is compiled. That's a huge difference. And besides that, you definitely can learn how to generate Java code on the fly but it is rarely necessary and you'd need good reasons for that. In my professional career I almost never found a reason to do so. – Thomas Dec 16 '20 at 07:20
  • @Thomas - I've listened to some of the advice here to use a method for this, which implements Runnable as is the advice given on this question: https://stackoverflow.com/questions/13327571/in-a-simple-to-understand-explanation-what-is-runnable-in-java -- experimenting with run[][] in the place of run() to try and create sort of an index. Even if it won't be needed in the future, this sort of thing is kind of "beautiful" to me and it just "clicks" with something in my brain to really interest me, if you get what I mean. So I'd like to create code early to reuse throughout in this way for this. – Hobbyist Dec 16 '20 at 23:13

0 Answers0