I am pretty new to java, and I'm currently learning how to make GUIs. To test out making them, I tried making one on my own. In order to find out how to make text boxes uneditable, I googled it and found it on the oracle website and tried it out. Unfortunately when I attempt to execute this, I am given the errors 'Syntax error on token(s), misplaced construct(s)' and 'Syntax error on token "false", delete this token'. This is what I had so far for the text area:
JTextArea textArea = new JTextArea("Testing the text", 5, 10);
JScrollPane scrollPane = new JScrollPane(textArea);
textArea.setEditable(false);
The error is at the period between 'textArea' and 'setEditable' and (of course) the 'false'. I get the same kind of error when I try to do setText.
If it helps, what I imported was:
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
I also extended JFrame and implemented ActionListener
Any help would be appreciated :)
So upon request, I am posting the rest of the code here. Unfortunately I haven't gotten very far in the program yet, but I will post what I have so far and try to finish it soon.
RunTestGui.java file:
import javax.swing.JFrame;
public class RunTestGui {
public static void main(String[] args) {
// TODO Auto-generated method stub
TestGui gui = new TestGui();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
TestGui.java file:
/*import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;*/
//import javax.swing.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
//import java.awt.event.ActionEvent;
//import java.awt.event.ActionListener;
public class TestGui extends JFrame /*implements ActionListener*/ {
//Global components
JTextArea textArea = new JTextArea("Testing the text", 5, 10);
JScrollPane scrollPane = new JScrollPane(textArea);
textArea.setEditable(false);
JTextField textBox;
//constructor
TestGui(String title){
super(title);
this.init();
this.pack();
this.setVisible(true);
}//end constructor
public void init(){
JLabel ltextBox = new JLabel("TextBox: ");
textBox = new JTextField(10);
JPanel leftPanel = new JPanel();
leftPanel.add(ltextBox);
leftPanel.add(textBox);
JPanel rightPanel = new JPanel();
rightPanel.add(textArea);
}
}
Problem is you have to put this line textArea.setEditable(false); in the method body, so either you can put in constructor or method:
public void init(){
JLabel ltextBox = new JLabel("TextBox: ");
textBox = new JTextField(10);
JPanel leftPanel = new JPanel();
leftPanel.add(ltextBox);
leftPanel.add(textBox);
JPanel rightPanel = new JPanel();
rightPanel.add(textArea);
textArea.setEditable(false); //<-- put it here
}
The line that causes you the error (textArea.setEditable(false);) is a statement with a method call, not a statement with a declaration.
Declarations are allowed directly in the class, but statements with just a method call are only allowed inside a method.
Just move this line into your init() method and it will be fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With