Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » IOException in Java
IOException in Java [message #1858060] Tue, 14 March 2023 00:04 Go to next message
bretny relly is currently offline bretny rellyFriend
Messages: 14
Registered: March 2023
Junior Member
I'm working on a rudimentary Chat Bot application that replies to what the user enters. I'm saving the questions it responds to, as well as the answers to those questions, in a text file. I am reading the file with a scanner. This is also just my second application in which I utilise a GUI to interface with the user. I have one TextField, one Label, and two buttons (One to enter the question asked and the other to exit the program). As far as I understand GUIs in Java, you must extend JFrame in the main class. This is the other programme I made 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 application to experiment with alternative GUIs. It says I can't raise an IOException within the CalculateButtonHandler. I want to use a pretty similar structure for the Chat Bot software, however following this document I'm curious whether there is a solution for the IOException, or if there is a better and easier approach to create a GUI?

[Updated on: Wed, 15 March 2023 11:23]

Report message to a moderator

Re: IOException in Java [message #1858063 is a reply to message #1858060] Tue, 14 March 2023 07:32 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33218
Registered: July 2009
Senior Member
As mentioned, this forum is for asking questions about how Eclipse's Java Development Tools work. It's not a general forum for asking Java design questions. Better to use something like https://stackoverflow.com/

Ed Merks
Professional Support: https://www.macromodeling.com/
Re: IOException in Java [message #1858085 is a reply to message #1858063] Wed, 15 March 2023 11:11 Go to previous message
bretny relly is currently offline bretny rellyFriend
Messages: 14
Registered: March 2023
Junior Member
sorry about that
Previous Topic:Any way to keep font preferences between workspaces?
Next Topic:Eclipse SDK 4.26 for Java spell check flags "@param " in javadoc as misspelled: how to fix
Goto Forum:
  


Current Time: Tue Sep 24 16:05:27 GMT 2024

Powered by FUDForum. Page generated in 0.03910 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top