2
private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JPanel panel = new JPanel();
    panel.setBounds(0, 0, 434, 261);
    frame.getContentPane().add(panel);
    panel.setLayout(null);

    JCheckBox chckbxCheckbox = new JCheckBox("checkbox");
    chckbxCheckbox.setBounds(44, 41, 97, 23);
    panel.add(chckbxCheckbox);

    JCheckBox chckbxCheckbox_1 = new JCheckBox("checkbox 2");
    chckbxCheckbox_1.setBounds(197, 41, 97, 23);
    panel.add(chckbxCheckbox_1);
}

Basically, I want chckbxCheckbox, to alter the state of chckbxCheckbox_1. After an alteration, the code section looks like this:

JCheckBox chckbxCheckbox = new JCheckBox("checkbox");
    chckbxCheckbox.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            chckbxCheckbox_1.setEnabled(false);
        }
    });
    chckbxCheckbox.setBounds(44, 41, 97, 23);

    panel.add(chckbxCheckbox);

    JCheckBox chckbxCheckbox_1 = new JCheckBox("checkbox 2");
    chckbxCheckbox_1.setBounds(197, 41, 97, 23);
    panel.add(chckbxCheckbox_1);

I get the following:

Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem: 
chckbxCheckbox_1 cannot be resolved

The Question becomes, how to I work with components that appear lower in the code? Again, this is just an example code. The code I'm working on has a lot more components interacting with each other in ways that would eventually prevent me from moving code around to avoid this issue.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Cygnuson
  • 160
  • 1
  • 11
  • `chckbxCheckbox_1.setBounds(197, 41, 97, 23);` Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. For a robust GUI, instead use layout managers, or combinations of them, along with layout padding & borders for white space, to organize the components. – Andrew Thompson May 12 '13 at 16:32
  • remove JCheckBox chckbxCheckbox_1 = new JCheckBox("checkbox 2"); from initialization block and make is instance variable and then try to use this instance variable in your after alteration block. dont create new checkbox in after alteration block. – Sudhir Dhumal May 12 '13 at 16:32
  • Thank you, your help has been invaluable. – Cygnuson May 12 '13 at 19:36

1 Answers1

2

You're shadowing the chckbxCheckbox_1 variable. You need to declare it in the class not inside of a method or constructor.

So rather than,

private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JPanel panel = new JPanel();
    panel.setBounds(0, 0, 434, 261);
    frame.getContentPane().add(panel);
    panel.setLayout(null);

    JCheckBox chckbxCheckbox = new JCheckBox("checkbox");
    chckbxCheckbox.setBounds(44, 41, 97, 23);
    panel.add(chckbxCheckbox);

    JCheckBox chckbxCheckbox_1 = new JCheckBox("checkbox 2");
    chckbxCheckbox_1.setBounds(197, 41, 97, 23);
    panel.add(chckbxCheckbox_1);
}

do:

public MyClass {
    // declared in class
    private JCheckBox chckbxCheckbox_1;

    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JPanel panel = new JPanel();
        panel.setBounds(0, 0, 434, 261);
        frame.getContentPane().add(panel);
        panel.setLayout(null);

        JCheckBox chckbxCheckbox = new JCheckBox("checkbox");
        chckbxCheckbox.setBounds(44, 41, 97, 23);
        panel.add(chckbxCheckbox);

        // declare this variable in the class
        chckbxCheckbox_1 = new JCheckBox("checkbox 2");
        chckbxCheckbox_1.setBounds(197, 41, 97, 23);
        panel.add(chckbxCheckbox_1);
    }

Next you will want to read up on and learn to use Swing layout managers.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 1
    Thank you, as always, Stack Overflow has made up for my ignorance in this matter(Or my ignorance in coding, perhaps). Ill look into Swing layouts some more, and get past my preference for absolute. Its not the first time I'v been told that. – Cygnuson May 12 '13 at 19:33