Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: HELP WITH MY JAVA GUI

  1. #1
    Junior Member
    Join Date
    May 2019
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question HELP WITH MY JAVA GUI

    PLEASE HELP ME WITH MY CODE. WHENEVER I INPUT AN INVALID CHARACTER IN THE FIRST TEXT FIELD, THE WHOLE PROGRAM JUST FREEZES. BUT IF I PUT AN INVALID CHARACTER ON THE OTHER TEXT FIELDS IT WORKS JUST FINE. PLEASE HELP ME.

    import java.util.InputMismatchException;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class GUIarea extends JFrame {
    private JLabel inputradiusc, inputradiuss, inputwidth, inputlength, area1, area2, area3, cl1,cl2,cl3;
    private JTextField inputrad1, inputrad2, inputwid, inputleng;
    private JButton compute, exit;
    private CalculateButtonHandler comhandler;
    private ExitButtonHandler exithandler;
    private static final int width = 1050;
    private static final int length = 500;
    public GUIarea() {
    inputradiusc = new JLabel ("Enter the radius of the Circle: ", SwingConstants.LEFT );
    inputradiuss = new JLabel ("Enter the radius of the Sphere: ", SwingConstants.LEFT );
    inputwidth = new JLabel ("Enter the Width of a Rectangle: ", SwingConstants.LEFT );
    inputlength = new JLabel ("Enter the Length of a Rectangle: ", SwingConstants.LEFT );
    area1 = new JLabel("Area of a Circle: ", SwingConstants.RIGHT);
    area2 = new JLabel("Area of a Sphere: ", SwingConstants.RIGHT);
    area3 = new JLabel("Area of a Rectangle: ", SwingConstants.RIGHT);
    cl1 = new JLabel("");
    cl2 = new JLabel("");
    cl3 = new JLabel("");
    inputrad1 = new JTextField(1);
    inputrad2 = new JTextField(1);
    inputwid = new JTextField(1);
    inputleng = new JTextField(1);
    compute = new JButton("Compute");
    comhandler = new CalculateButtonHandler();
    compute.addActionListener(comhandler);
    exit = new JButton("Exit");
    exithandler = new ExitButtonHandler();
    exit.addActionListener(exithandler);
    setTitle("Area Calculator by Miguel Ybera");
    Container ybs = getContentPane();
    ybs.setLayout(new GridLayout(8, 2));
    ybs.add(inputradiusc);
    ybs.add(inputrad1);
    ybs.add(inputradiuss);
    ybs.add(inputrad2);
    ybs.add(inputwidth);
    ybs.add(inputwid);
    ybs.add(inputlength);
    ybs.add(inputleng);
    ybs.add(area1);
    ybs.add(cl1);
    ybs.add(area2);
    ybs.add(cl2);
    ybs.add(area3);
    ybs.add(cl3);
    ybs.add(compute);
    ybs.add(exit);
    setSize(width, length);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
    private class CalculateButtonHandler implements ActionListener{
    public void actionPerformed(ActionEvent asdf) {
    double num1 , num2 , num3, num4,
    areaofcircle, areaofsphere, areaofrectangle;
    int x=1;
    do {
    try {
    num1 = Double.parseDouble(inputrad1.getText());
    x=2;
    areaofcircle = 3.14*num1*num1;
    cl1.setText(""+ String.format("%.2f", areaofcircle));


    }
    catch(Exception wrong) {
    cl1.setText("Invalid");
    }
    }
    while (x ==1);
    do {
    try {
    num2 = Double.parseDouble(inputrad2.getText());
    x=2;
    areaofsphere = 4*3.14*num2*num2;
    cl2.setText(""+ String.format("%.2f", areaofsphere));
    }
    catch(Exception Exception) {
    cl2.setText("Invalid");
    }
    }
    while (x ==1);
    do {
    try {
    num3 = Double.parseDouble(inputwid.getText());
    num4 = Double.parseDouble(inputleng.getText());
    x=2;
    areaofrectangle = num3 *num4;
    cl3.setText(""+ String.format("%.2f", areaofrectangle));
    }
    catch(Exception Exception) {
    cl3.setText("Invalid");
    }
    }
    while (x ==1);
    }
    }
    private class ExitButtonHandler implements ActionListener{
    public void actionPerformed(ActionEvent e) {
    System.exit(0);
    }
    }
    public static void main(String[] args) {
    GUIarea graph = new GUIarea();
    }

    }

  2. #2
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    261
    My Mood
    Amused
    Thanks
    8
    Thanked 18 Times in 18 Posts

    Default Re: HELP WITH MY JAVA GUI

    1. Please don't shout(Don't type all the words in capital letter).
    2. Please format your code probably, so it is more readable.Wrap your code using [CODE] Tag.
    WHENEVER I INPUT AN INVALID CHARACTER IN THE FIRST TEXT FIELD, THE WHOLE PROGRAM JUST FREEZES
    Your system did not freeze, it just keep looping (in do-while loop) since x is always 1. What you want to do with the do-while loop?
    Why you need to have 3 do-while loop?
    Last edited by John Joe; May 4th, 2019 at 12:12 PM.
    Whatever you are, be a good one

  3. #3
    Junior Member
    Join Date
    May 2019
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HELP WITH MY JAVA GUI

    if I input an invalid character on the first text field it should display "Invalid" on the botton after "Area of a circle" but it works fine if I place an invalid character on the 2nd 3rd and 4th text field but my 1st textfield has to have the correct input

  4. #4
    Junior Member
    Join Date
    Apr 2019
    Posts
    24
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: HELP WITH MY JAVA GUI

    Please type you code in
     
    format it to easy to read.

  5. #5
    Junior Member
    Join Date
    May 2019
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HELP WITH MY JAVA GUI

    [CODE]
    import java.util.InputMismatchException;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class GUIarea extends JFrame {
    private JLabel inputradiusc, inputradiuss, inputwidth, inputlength, area1, area2, area3, cl1,cl2,cl3;
    private JTextField inputrad1, inputrad2, inputwid, inputleng;
    private JButton compute, exit;
    private CalculateButtonHandler comhandler;
    private ExitButtonHandler exithandler;
    private static final int width = 1050;
    private static final int length = 500;
    public GUIarea() {
    inputradiusc = new JLabel ("Enter the radius of the Circle: ", SwingConstants.LEFT );
    inputradiuss = new JLabel ("Enter the radius of the Sphere: ", SwingConstants.LEFT );
    inputwidth = new JLabel ("Enter the Width of a Rectangle: ", SwingConstants.LEFT );
    inputlength = new JLabel ("Enter the Length of a Rectangle: ", SwingConstants.LEFT );
    area1 = new JLabel("Area of a Circle: ", SwingConstants.RIGHT);
    area2 = new JLabel("Area of a Sphere: ", SwingConstants.RIGHT);
    area3 = new JLabel("Area of a Rectangle: ", SwingConstants.RIGHT);
    cl1 = new JLabel("");
    cl2 = new JLabel("");
    cl3 = new JLabel("");
    inputrad1 = new JTextField(1);
    inputrad2 = new JTextField(1);
    inputwid = new JTextField(1);
    inputleng = new JTextField(1);
    compute = new JButton("Compute");
    comhandler = new CalculateButtonHandler();
    compute.addActionListener(comhandler);
    exit = new JButton("Exit");
    exithandler = new ExitButtonHandler();
    exit.addActionListener(exithandler);
    setTitle("Area Calculator by Miguel Ybera");
    Container ybs = getContentPane();
    ybs.setLayout(new GridLayout(8, 2));
    ybs.add(inputradiusc);
    ybs.add(inputrad1);
    ybs.add(inputradiuss);
    ybs.add(inputrad2);
    ybs.add(inputwidth);
    ybs.add(inputwid);
    ybs.add(inputlength);
    ybs.add(inputleng);
    ybs.add(area1);
    ybs.add(cl1);
    ybs.add(area2);
    ybs.add(cl2);
    ybs.add(area3);
    ybs.add(cl3);
    ybs.add(compute);
    ybs.add(exit);
    setSize(width, length);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
    private class CalculateButtonHandler implements ActionListener{
    public void actionPerformed(ActionEvent asdf) {
    double num1 , num2 , num3, num4,
    areaofcircle, areaofsphere, areaofrectangle;
    int x=1;
    do {
    try {
    num1 = Double.parseDouble(inputrad1.getText());
    x=2;
    areaofcircle = 3.14*num1*num1;
    cl1.setText(""+ String.format("%.2f", areaofcircle));


    }
    catch(Exception wrong) {
    cl1.setText("Invalid");
    }
    }
    while (x ==1);
    do {
    try {
    num2 = Double.parseDouble(inputrad2.getText());
    x=2;
    areaofsphere = 4*3.14*num2*num2;
    cl2.setText(""+ String.format("%.2f", areaofsphere));
    }
    catch(Exception Exception) {
    cl2.setText("Invalid");
    }
    }
    while (x ==1);
    do {
    try {
    num3 = Double.parseDouble(inputwid.getText());
    num4 = Double.parseDouble(inputleng.getText());
    x=2;
    areaofrectangle = num3 *num4;
    cl3.setText(""+ String.format("%.2f", areaofrectangle));
    }
    catch(Exception Exception) {
    cl3.setText("Invalid");
    }
    }
    while (x ==1);
    }
    }
    private class ExitButtonHandler implements ActionListener{
    public void actionPerformed(ActionEvent e) {
    System.exit(0);
    }
    }
    public static void main(String[] args) {
    GUIarea graph = new GUIarea();
    }

    }

  6. #6
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    261
    My Mood
    Amused
    Thanks
    8
    Thanked 18 Times in 18 Posts

    Default Re: HELP WITH MY JAVA GUI

    The reason is because of the x. If you input invalid character on the first text field, the x is always 1. So it will keep on looping. But when you key in an invalid input in 2nd,3rd and 4th text field, but not 1st text field, the x is already change to 2, so it will not execute the do-while loop, and thus invalid text can be displayed.
    Last edited by John Joe; May 5th, 2019 at 09:17 AM.
    Whatever you are, be a good one

Similar Threads

  1. GUI Java
    By nick777 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 28th, 2013, 05:31 PM
  2. Replies: 2
    Last Post: January 18th, 2013, 11:12 AM
  3. Java GUI help
    By needJavaHelp in forum AWT / Java Swing
    Replies: 3
    Last Post: August 9th, 2011, 11:06 AM

Tags for this Thread