Search code examples
javaswinguser-interfacejtextfieldjcheckbox

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.


Solution

  • 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"));
            }
        });
    }
    

    }