user3072800
user3072800

Reputation: 11

JFilechooser not working properly with select button

When ever I click either the select file button or write button no event occurs. When the select button is pressed no errors even come up in the console. The write button on the other hand does display errors.

 public class Exam extends JPanel
    implements ActionListener {
    /**
         * 
         */
        private static final long serialVersionUID = 1L;
    static private String newline = "\n";
    private JTextArea log;
    private JFileChooser fc;
    private static JButton selBtn = new JButton("Select File");
    private static JButton writeBtn = new JButton ("Write File");




        public static void main (String[] args)
        {

            //create a JFrame object

            JFrame frame = new JFrame("QBStats App");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            //create a JPanel named as mainPanel

            JPanel mainPanel = new JPanel();
            mainPanel.setLayout(new BorderLayout());
            mainPanel.setPreferredSize(new Dimension (500,500));

            //create an instance of BtnListener 
            BtnListener btnListener = new BtnListener();

            //create a JButton for Select File

            selBtn.setActionCommand("Select");


            //create a write button 


            writeBtn.addActionListener(btnListener);
            writeBtn.setActionCommand("Write");


            //create a sort button
            JButton sortBtn = new JButton ("Sort");
            sortBtn.setActionCommand("Sort");
            sortBtn.addActionListener(btnListener);

            //radio buttons
            JRadioButton dogButton = new JRadioButton("Last Name");
            dogButton.setActionCommand("test");

            JRadioButton rabbitButton = new JRadioButton("Yards");
            rabbitButton.setActionCommand("test");

            JRadioButton pigButton = new JRadioButton("Rating");
            pigButton.setActionCommand("test");

            //Group the radio buttons.
            ButtonGroup group = new ButtonGroup();
            group.add(dogButton);
            group.add(rabbitButton);
            group.add(pigButton);

            //Register a listener for the radio buttons

            //create a JPanel named as headPanel

            JPanel headPanel = new JPanel ();
            headPanel.add(selBtn);
            headPanel.add(writeBtn);

            //add headPanel to North of mainPanel
            mainPanel.add(headPanel, BorderLayout.NORTH);

            //create a JPanel named as footPanel
            JPanel footPanel = new JPanel ();
            footPanel.add(dogButton);
            footPanel.add(rabbitButton);
            footPanel.add(pigButton);
            footPanel.add(sortBtn);


            //add footPanel to South of mainPanel
            mainPanel.add(footPanel, BorderLayout.SOUTH);

            //create a JScrollPane with ta1 component from BtnListener instance as argument
            //set above JScrollPane to your preferred dimension size
            JScrollPane log = new JScrollPane (btnListener.ta1);
            log.setPreferredSize (new Dimension (450, 400));

            //create a JScrollPane with ta2 component from BtnListener instance as argument
            //set above JScrollPane to your preferred dimension size
            JScrollPane sp2 = new JScrollPane (btnListener.ta2);
            sp2.setPreferredSize (new Dimension (450, 400));

            //create a JSplitPane object that divides above two JScrollPane objects
            JSplitPane sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
                    log, sp2);
            //use setResizeWeight method from JSplitMethod to 0.5
            sp.setResizeWeight(0.5);
            //add above created JSplitPane to CENTER of the mainPanel
            mainPanel.add(sp, BorderLayout.CENTER);

            //add mainPanel to JFrame object ContentPane
            //Pack and set JFrame to be visible
            frame.getContentPane().add(mainPanel);

            frame.pack();
            frame.setVisible(true);}

        public void actionPerformed(ActionEvent e) {
             //Handle open button action.
            if (e.getSource() == selBtn) {
                int returnVal = fc.showOpenDialog(Exam.this);

                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    File file = fc.getSelectedFile();
                    //This is where a real application would open the file.
                    log.append("Opening: " + file.getName() + "." + newline);
                } else {
                    log.append("Open command cancelled by user." + newline);
                }
                log.setCaretPosition(log.getDocument().getLength());

            //Handle save button action.
            } else if (e.getSource() == writeBtn) {
                int returnVal = fc.showSaveDialog(Exam.this);
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    File file = fc.getSelectedFile();
                    //This is where a real application would save the file.
                    log.append("Saving: " + file.getName() + "." + newline);
                } else {
                    log.append("Save command cancelled by user." + newline);
                }
                log.setCaretPosition(log.getDocument().getLength());
            }
        }
    }//end of Exam class

Upvotes: 0

Views: 199

Answers (1)

Andrew Thompson
Andrew Thompson

Reputation: 168845

At no point does that code call selBtn.addActionListener(..). So obviously they would not fire events detectable by one.

Upvotes: 1

Related Questions