i'm working with swing builder in java, the form:
but I can't access the components from main:
how can I get access to the form components?
Asked
Active
Viewed 144 times
-1

Maifee Ul Asad
- 3,992
- 6
- 38
- 86

roni
- 59
- 6
-
1welcome. please don't post image of your code when posting, if it's necessary the add code too. – Maifee Ul Asad Oct 08 '19 at 19:37
2 Answers
1
This is how I solved it
public class firstSwingForm {
private JPanel config;
private JTextField startTxt;
private JTextField dogTextField;
private JPanel mainPanel;
private JTextField a5TextField;
private JButton startBtn;
private static firstSwingForm instance;
public static void main(String args[]) {
JFrame frame = new JFrame("App");
instance = new firstSwingForm();
frame.setContentPane(instance.mainPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
instance.startBtn.setText("text can be set");

roni
- 59
- 6
0
The issue is that your private JButton startBtn
needs to be declared static in order to be accessed within the main method: private static JButton startBtn;
You should also instantiate it as a new object within the main before calling anything on it: startBtn = new JButton(...);
It's also worth noting, by convention, your class name should be FirstSwingForm
,
and think through whether or not those instance variables will be used elsewhere or if they can be defined within main.

Darkserum
- 11
- 4