-2

How to pass actual checkbox state (true/false) from GUI class to another class? I want to run some part of code only if checkbox in GUI is selected. I guess it has to be if statement (highlithed part below) but i cant get it working.

public class csvtoxls {

    public static void main() throws IOException {
    //here you enter the path to your directory.
    //for example: Path workDir = Paths.get("C:\\Users\\Kamil\Desktop\\csvtoxlspython\\Nowy folder (2)")
    JFileChooser jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
    jfc.setDialogTitle("Wybierz folder do konwersji: ");
    jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    jfc.setAcceptAllFileFilterUsed(false);
    int returnValue = jfc.showSaveDialog(null);
    if (returnValue == JFileChooser.APPROVE_OPTION) {
        if (jfc.getSelectedFile().isDirectory()) {
            System.out.println("You selected the directory: " + jfc.getSelectedFile());

            String z;
            //@SuppressWarnings("deprecation")
            Path workDir = jfc.getSelectedFile().toPath();
            System.out.println(workDir);
            //Path workDir = FileSystems.getDefault(jfc.getCurrentDirectory()).jfc.getCurrentDirectory();

            //Path workDir = Paths.get(gui.pickPath(jfc));

            File dirg = jfc.getSelectedFile();
            //String str = dirg.getPath();

            //  *************   CODE WITH ISSUE *************
            if TextAreaLogProgram.checkbox.isSelected() {
                try {
                    Thread.sleep(5000);                 //1000 milliseconds is one second.
                } catch(InterruptedException ex) {
                    Thread.currentThread().interrupt();
                }
                String str = dirg.getPath();
                delfiles td = new delfiles();

                td.deleteFiles(str + "/", ".csv");
                System.out.println("SUCCESS!");
                msgbox.infoBox("SUCCES!", "CSVtoXLS");
            }

GUI class:

public class TextAreaLogProgram extends JFrame {
    private JTextArea textArea;
    private JButton buttonStart = new JButton("CONVERT");
    private JButton buttonClear = new JButton("CLEAR");
    private PrintStream standardOut;

    public TextAreaLogProgram() {
        super("CSVtoXLS");
        JCheckBox checkbox = new JCheckBox();
        add(checkbox);
        checkbox.setText("Delete files");
        checkbox.setSelected(true);
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
euranoo
  • 69
  • 1
  • 11
  • Please improve your question to make it easier to answer without having to guess. You don't show where or how you run the `TextAreaLogProgram` class, you don't show where you create a `TextAreaLogProgram` object, and your posted code formatting isn't good, making the code difficult to read and follow. You really do want to make it easy for folks to help, so it's in your best interest to do these things. Also please go through the how-to-ask sections of the [help] for more on how to improve the question. – Hovercraft Full Of Eels Sep 13 '17 at 21:32
  • But having said that, the question boils down to checking the state of one object by another, and the key to this is ***references***, getting a reference of one object in the other. Also the JCheckBox shouldn't be a local variable but rather an instance field of the TextAreaLogProgram class. – Hovercraft Full Of Eels Sep 13 '17 at 21:33
  • I have edited and formatted your posted code. – Hovercraft Full Of Eels Sep 13 '17 at 22:12

1 Answers1

2

Your other class will need a method or constructor with a parameter to be able to accept the value from the other class

See Passing Information to a Method or a Constructor for more details

Other issues:

  • Your program structure needs to be redone completely. Right now your main method is much too large, meaning that you're doing too much within the static world and not using Java to its best OOPs advantage.
  • Before even thinking of creating the GUI, first create the non-GUI "model" classes that your program will need. Like all your classes, these should have minimal static fields and methods, and strive to follow object-oriented best practices
  • You've got a Thread.sleep within your GUI code, something that does not work well with Swing GUI's since this risks putting the entire GUI to sleep, making it non-responsive. If you want Swing delays, use a Swing Timer (google the excellent tutorial on this)
  • You're trying to check the checkbox as if it were a static field of the TextAreaLogProgram class. It's not a static field and in fact its not even a field of the class.
  • The fact that you're doing the above suggests that you would benefit greatly from studying introductory tutorials on Object-Oriented programming and Java -- you are putting the cart before the horse by trying to create a GUI before first understanding Java fundamentals. Again, you won't regret the effort expended doing this.
  • Whatever you do, don't make the JCheckBox a static field and try to access it this way. This will lead to spaghetti code and increased risk for bugs.
  • Instead, make it a non-static (instance) private field of the TextAreaLogProgram class, and give the class a getter method to allow other objects access to the JCheckgbox's state.
  • There's so much more that can be mentioned about your code and problem... but this will do for now.
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366