IOException in Java

Started by
3 comments, last by frob 1 year, 2 months ago

I'm creating a basic Chat Bot software that replies to what the user enters. I'm keeping a text file with the questions it responds to and the answers to those questions. I'm reading the document with a scanner. This is also just my second application in which I employ a graphical user interface (GUI) to communicate with the user. One TextField, one Label, and two buttons are present (One to enter the question asked and the other to exit the program). According to my understanding of Java GUIs, you must extend JFrame in the main class. This is the second programme I created for a graphical software that estimates the area of a rectangle:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class RectangleProgram extends JFrame
{
    private static final int WIDTH = 400;
    private static final int HEIGHT = 200;

    private JLabel lengthL, widthL, areaL;
    private JTextField lengthTF, widthTF, areaTF;
    private JButton calculateB, exitB;

    //Button handlers:
    private CalculateButtonHandler cbHandler;
    private ExitButtonHandler ebHandler;

    public RectangleProgram()
    {
        lengthL = new JLabel("Enter the length: ", SwingConstants.RIGHT);
        widthL = new JLabel("Enter the width: ", SwingConstants.RIGHT);
        areaL = new JLabel("Area: ", SwingConstants.RIGHT);

        lengthTF = new JTextField(10);
        widthTF = new JTextField(10);
        areaTF = new JTextField(10);

        //SPecify handlers for each button and add (register) ActionListeners to each button.
        calculateB = new JButton("Calculate");
        cbHandler = new CalculateButtonHandler();
        calculateB.addActionListener(cbHandler);
        exitB = new JButton("Exit");
        ebHandler = new ExitButtonHandler();
        exitB.addActionListener(ebHandler);

        setTitle("Sample Title: Area of a Rectangle");
        Container pane = getContentPane();
        pane.setLayout(new GridLayout(4, 3));

        //Add things to the pane in the order you want them to appear (left to right, top to bottom)
        pane.add(lengthL);
        pane.add(lengthTF);
        pane.add(widthL);
        pane.add(widthTF);
        pane.add(areaL);
        pane.add(areaTF);
        pane.add(calculateB);
        pane.add(exitB);

        setSize(WIDTH, HEIGHT);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    private class CalculateButtonHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            double width, length, area;
            length = Double.parseDouble(lengthTF.getText()); //We use the getText & setText methods to manipulate the data entered into those fields.
            width = Double.parseDouble(widthTF.getText());
            area = length * width;
            areaTF.setText("" + area);

        }
    }
    public class ExitButtonHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            System.exit(0);
        }
    }

    public static void main(String[] args)
    {
        RectangleProgram rectObj = new RectangleProgram();
    }
}

I'm using this to experiment with alternative GUIs. When I try to raise an IOException within the CalculateButtonHandler, it claims I can't. I want to use a very similar configuration for the Chat Bot software, however I'm wondering if there is a solution for the IOException, or if there is a better and easier approach to create a GUI?

Advertisement

You need to declare what exceptions can come out of your methods, and then catch them as needed. You'd probably find out that you can't throw exceptions freely because you are constrained by interfaces.

Why would you want to throw some IOException from GUI event handlers? Actually, why would you want to throw any exception at all? Your application is the higher level layer that should catch exceptions, not throw them, and handle low-level problems (e.g. by displaying a warning).

Omae Wa Mou Shindeiru

@LorenzoGatti Okay got it

Note that I almost closed this and another homework related thread yesterday when I saw them, they come very close to the line of what is allowed on the site and what isn't, the new one I did close down.

The actual question you asked here (why can't I raise an exemption in this handler, and is there an easier approach) was not quite about understanding the homework, but it is something your teacher would benefit from knowing you asked.

Please be careful about homework-related questions on the site. Your best resources are your teacher (who can do their job better when you ask them questions) and your own searches of documentation and web pages. Q&A sites and discussion sites can help you get answers but generally aren't the best resource as a student for assignments.

This topic is closed to new replies.

Advertisement