-1

So if a user do not press any button,the action listener is not triggered and i end up with an exception. So i thought to put a default String in my FrameClass and change that String whenever a button is clicked,than in my main class i do a loop that keeps on looping until the default String is changed,so i think it is an infinite loop. Is it okay to do that ?

package gui;

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

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRootPane;

/**
 * 
 * @author E-TECH
 */
public class ButtonsFrame extends JFrame {

    private JButton ScPerF, weekSc, both,cancel;
    private SchedulePerWeek week;
    private CoursesPerWeek course;
    private JPanel panel;
    private String choice;
    private File file;

    public ButtonsFrame() {
        ScPerF = new JButton("Generate schedule/faculty");
        weekSc = new JButton("Generate weekly class schedule");
        both = new JButton("Generate Both");
        cancel = new JButton("Cancel");
        choice="nothing";

        ScPerF.addActionListener(new ButtonListener());
        weekSc.addActionListener(new ButtonListener());
        both.addActionListener(new ButtonListener());
        cancel.addActionListener(new ButtonListener());
        setResizable(false);
        setUndecorated(true);
        getRootPane().setWindowDecorationStyle(JRootPane.NONE);

        panel = new JPanel();
        panel.add(ScPerF);
        panel.add(weekSc);
        panel.add(both);
        panel.add(cancel);
        getContentPane().add(panel);
        setVisible(true);
        pack();
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    }

    private class ButtonListener implements ActionListener {

        public void actionPerformed(ActionEvent event) {
            if (event.getSource() == ScPerF) {
                dispose();

                choice = "faculty";

            }
            if (event.getSource() == weekSc) {
                dispose();

                choice = "course";
            }
            if (event.getSource() == both) {
                dispose();
                choice = "both";
            }
            if (event.getSource()==cancel){
                dispose();
                choice="cancel";
            }


        }

    }

    public boolean Activated() {
        return ScPerF.isSelected() || weekSc.isSelected();
    }

    public String getChoice() {
        return choice;
    }

    public File getFile() {
        return file;
    }

}

public class SchedulePerWeek {
    HSSFSheet weekSh,courseSh;
    int instructor_count;
    HSSFWorkbook wb;

    public SchedulePerWeek() {


        ExcelReader reader = new ExcelReader();

        HSSFSheet sh = reader.getSortedSheet();
        String choice=reader.getChoice();

        if(choice.equals("cancel")||choice.equals("nothing")){///i fixed the exception with this condition by closing the program instead of continuing,but i want to wait for the user instead of just exiting the program
            System.exit(1);
}
        wb = new HSSFWorkbook();
/////
///more code
Radu Murzea
  • 10,724
  • 10
  • 47
  • 69
Roudy Tarabay
  • 29
  • 2
  • 7

1 Answers1

1

I ran your code from a couple of edits ago, and it works fine on my Windows 8 workstation, Java 7.

Before you go much further in your GUI design, read this answer to The Use of Multiple JFrames, Good/Bad Practice?

I modified your code to use a JFrame, rather than extend one. You should only extend a Swing component when you override one of the component methods.

You only need to define your Button listener once. You set the listener on your buttons.

I changed the JFrame default close operation to exit on close.

I added a main method so I could run your code.

Here's the code with the changes.

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

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.SwingUtilities;

/**
 * 
 * @author E-TECH
 */
public class ButtonsFrame{

    private JButton scPerf, weekSc, both, cancel;
    // private SchedulePerWeek week;
    // private CoursesPerWeek course;
    private JFrame  frame;
    private JPanel  panel;
    private String  choice;
    private File    file;

    public ButtonsFrame() {
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        scPerf = new JButton("Generate schedule/faculty");
        weekSc = new JButton("Generate weekly class schedule");
        both = new JButton("Generate Both");
        cancel = new JButton("Cancel");
        choice = "nothing";

        ButtonListener listener = new ButtonListener();
        scPerf.addActionListener(listener);
        weekSc.addActionListener(listener);
        both.addActionListener(listener);
        cancel.addActionListener(listener);

        frame.setResizable(false);
        frame.setUndecorated(true);
        frame.getRootPane().setWindowDecorationStyle(JRootPane.NONE);

        panel = new JPanel();
        panel.add(scPerf);
        panel.add(weekSc);
        panel.add(both);
        panel.add(cancel);

        frame.getContentPane().add(panel);
        frame.pack();
        frame.setVisible(true);
    }

    private class ButtonListener implements ActionListener {

        public void actionPerformed(ActionEvent event) {
            if (event.getSource() == scPerf) {
                frame.dispose();
                choice = "faculty";
            }
            if (event.getSource() == weekSc) {
                frame.dispose();
                choice = "course";
            }
            if (event.getSource() == both) {
                frame.dispose();
                choice = "both";
            }
            if (event.getSource() == cancel) {
                frame.dispose();
                choice = "cancel";
            }
        }

    }

    public boolean Activated() {
        return scPerf.isSelected() || weekSc.isSelected();
    }

    public String getChoice() {
        return choice;
    }

    public File getFile() {
        return file;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new ButtonsFrame();
            }
        });
    }

}
Community
  • 1
  • 1
Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
  • The problem is not with the GUI itself,when i use it in another class,and i use this other class in a main method,the program don't wait for the user to press the button,it keeps on running. – Roudy Tarabay May 25 '13 at 11:46
  • @Roudy Tarabay: Then you posted the wrong code. Use a JDialog instead of the JFrame in the ButtonsFrame. – Gilbert Le Blanc May 25 '13 at 11:47