user1318160
user1318160

Reputation: 73

Java first GUI, two classes

I'm trying to program my first GUI-class in Java, using the Window Builder. Ok, the GUI is ready but has no functions yet (it contains 10 checkboxes (yes, they are all neccessary) and 2 buttons and a JTextField, so nothing special). So I just drag'n'dropped the checkboxes and button into the window, I haven't coded anything yet.

I have two classes: GUI.java -> only for the layout Main.java -> should get 'inputs' from GUI.java

Now the user should check or uncheck the checkboxes and then finally press a button.

But how do I 'read out' if a checkbox is checked or not - I have two classes?

I have to add that I'm a beginner to Java and especially to GUI-programming, but I just want to learn it. I would be happy to receive some help but NOT the complete code.

Upvotes: 2

Views: 8084

Answers (6)

Jordon Biondo
Jordon Biondo

Reputation: 4029

I know you said you didn't want the full, code but this isn't really it, just a very basic working demo of what you want to do

enter image description here

Gui.java

import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Gui {
JFrame frame;
JButton button;
JCheckBox checkBox;
JLabel label;

public Gui() {
    frame = new JFrame("demo");
    button = new JButton("is it checked?");
    checkBox = new JCheckBox();
    label = new JLabel("no");
    JPanel panel = new JPanel();
    panel.add(checkBox);
    panel.add(button);
    panel.add(label);
    frame.add(panel);
    frame.pack();
    //frame.setSize(200, 60);
    frame.setResizable(false);
    frame.setLocation(400, 400);
    frame.setVisible(true);
}

// method to add an action listener to the gui's
// private button
public void setButtonActionListener(ActionListener al) {
    button.addActionListener(al);
}

// gui method to check if box is checked
public boolean isBoxChecked() {
    return checkBox.isSelected();
}

// method to set lable
public void setText(String text) {
    label.setText(text);
}

}

Main.java

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class Main {

public static void main(String[] args) {

    // create an instance of your gui class
    final Gui gui = new Gui();

    // add the action listener to the button
            // notice how we reference the gui here by running the methods in the
            // gui class
            // this action listener could be created in the gui
            // class but in general you don't want to do that because actions will 
            // involve multiple classes
    gui.setButtonActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            gui.setText((gui.isBoxChecked() ? "yes" : "no"));
        }
    });
}

}

Upvotes: 1

BlueCoder
BlueCoder

Reputation: 293

Assuming your checkbox is an object named "myCheckBox", you can check if it's selected or not by using:

myCheckBox.isSelected()

Returns true if the checkbox is selected.

I reccomend you to check Java's tutorials on how to use the various GUI components, e.g: http://docs.oracle.com/javase/tutorial/uiswing/components/button.html

Upvotes: 0

user12345613
user12345613

Reputation: 833

You could expose all of your gui objects as fields in your gui class

public JTextField getNameTextField() {
    return nameTextField;
}

public JCheckBox getCheckBox1() {
    return checkBox1;
}

and then in main:

if (gui.getCheckBox1().isSelected())
    // do stuff

Upvotes: 0

Battlecake
Battlecake

Reputation: 1

Either like that:

checkbox.isSelected();

Or this would be a way by an Itemlistener which will be called if there is a change:

checkbox.addItemListener(new ItemListener() {

        @Override
        public void itemStateChanged(ItemEvent e) {
            if (e.getStateChange() == ItemEvent.DESELECTED) {
                foo = false;

            } else {
                foo = true;
            }
        }
    });

Upvotes: 0

gobernador
gobernador

Reputation: 5739

Attach an ItemListener.

You can determine if the box is rising (unchecked to checked) or falling (checked to unchecked) with the following line in itemStateChanged(ItemEvent e)

boolean selected = e.getStateChange() == ItemEvent.SELECTED;

To determine whether it is checked when it is not changing, just use isSelected()

Upvotes: 0

Valentino Ru
Valentino Ru

Reputation: 5052

Basically you instantiate an object of your GUI from the Main.java. Then you have access to this GUI (assumed you have setter/getter-methods or the Components in the GUI are public). In the GUI constructor you call all builder methods, so when you then call new GUI() from Main.java, you got a) the GUI running and b) access to it from Main.java

For your specific question about the checkboxes, you can call

nameOfCheckbox.isSelected()

which returns a boolean wheter the checkbox is checked or not.

Viceversa: since your Main.java has (or should have) static methods (like the main-method), you can then simply call Main.anyMethodName() from GUI.java (assuming this anyMethod is static) and pass data from the "visual area" to the "logic area" (it is recommended to seperate this two componentes as good as possible).

Upvotes: 3

Related Questions