Search code examples
javaswingwindowjpanelgrid-layout

Error when I tried to run my Java class on another computer?


I am notdoing a school project, so please do not be alarmed. I am doing some private programming to brush up. My program, a program of type .java, creates a form that asks for the boundaries and quantities of a lottery-drawing activity and acts on the generating based on the input.

Here's my problem. On the WIndows 2000 computer where I coded the program, shows itself, perfectly. That's only half the story. When I tried to put it on another computer, the program shows a blank window; it compiles and runs, but it shows a blank window. Now, I do consider version numbers to be factors, so I will provide the versions and ask for confirmation if those are the root of the evil.

On my original computer, which is Windows 2000, the version is 1.6.0_31-b05. The other computer, which is Windows 7 dual-booted with Linux Mint 17.2, is running 1.8.0_60-b27 and 1.8.0_00 respectively.

My program is not finished yet, but I'll worry about that later. What I'm hoping to do now is to get the program, such as it is, to run on the platforms of all my computers. Since Java is known for its portability, I expect it to run on all my computers. Is that a misconception?

Anyways, here's the code:

//Import class libraries
import javax.swing.*;              
import javax.swing.JOptionPane;              
import java.awt.*;     
import java.awt.event.*;

public class Lotterygui     //Begin class
{
    //VARIABLES FOR DATA COLLECTION
    private JTextField lowerRange;  //Lowest number
    private JTextField higherRange; //Highest number
    private JTextField quantity;    //How many numbers to generate
    private JTextArea displayArea;  //What to display when the program is in use

    //ADD WARNING CONSTANT FOR INVALID INPUT
    private final String WARNING = "Please fill out valid data "
                                   + "and not leave anything out. "
                                   + "Also,do not enter any " 
                                   + "zeroes.";    

    public Lotterygui() 
    {
        //GUI CONFIGURATION

        //Frame settings
        JFrame jfrFrame = new JFrame("Lottery Program");
        jfrFrame.setSize(300,400);
        jfrFrame.setLocationRelativeTo (null);
        jfrFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jfrFrame.setVisible(true);
        jfrFrame.setResizable(false);

        //Panel to hold the user input controls in place
        JPanel jplInputs = new JPanel();
        jplInputs.setLayout(new GridLayout(4, 2));

        //CREATE INPUT CONTROLS

        //Lowest range
        JLabel jlblLowerRange = new JLabel("Lowest");
        lowerRange = new JTextField();

        //Highest range
        JLabel jlblHigherRange = new JLabel("Highest");
        higherRange = new JTextField();

        //Quantity
        JLabel jlblQuantity = new JLabel("Quantity");
        quantity = new JTextField();

        //Buttons and their respective action associations

        //Generate numbers button
        JButton jbtnGenerate = new JButton("Generate");
        ActionListener alGenerate = new listenGenerate();
        jbtnGenerate.addActionListener(alGenerate);

        //Reset all values button
        JButton jbtnReset = new JButton("Reset");
        ActionListener alReset = new listenReset();
        jbtnReset.addActionListener(alReset);        

        //ADD CONTROLS TO FORM
        jplInputs.add(jlblLowerRange);
        jplInputs.add(lowerRange);
        jplInputs.add(jlblHigherRange);
        jplInputs.add(higherRange);
        jplInputs.add(jlblQuantity);
        jplInputs.add(quantity);
        jplInputs.add(jbtnGenerate);
        jplInputs.add(jbtnReset);

        //CREATE DISPLAY AREA AND ADD

        //The display area used for showing generated numbers
        displayArea = new JTextArea();
        displayArea.setLineWrap(true);
        displayArea.setText(WARNING);

        //The control that sets autoscrolling for the display area
        JScrollPane jspDisplayArea = new JScrollPane(displayArea);
        jfrFrame.add(jspDisplayArea);

        //Add the JPanels to the window
        jfrFrame.add(jplInputs, BorderLayout.NORTH);
        jfrFrame.add(jspDisplayArea);            
    }//END lotteryGUI constructor

//MAIN Method
public static void main (String[] args)
{
    //CALL UP lotteryGUI CLASS
    new Lotterygui();
}//END Main method

//GENERATE BUTTONS ACTION    
private class listenGenerate implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        //DECLARE VARIABLES
        int low;        //Lowest number
        int high;       //Highest number
        int qty;        //How many numbers

        try //Monitor the input of above variables in the form
        {
            low =  Integer.parseInt(lowerRange.getText());
            high = Integer.parseInt(higherRange.getText());
            qty = Integer.parseInt(quantity.getText());                
        }
        catch (NumberFormatException nfe)
        {
            //RESET ALL FORM VALUES
            reset();

            //RESET VARIABLE VALUES
            low = 0;
            high = 0;
            qty = 0;                
        }//END format errors try-catch

        //CHECK IF PROGRAM CAN CONTINUE
        if (low != 0 || high != 0 || qty != 0) //If valid
        {
            //Action pending
            displayArea.setText("Generate here - incomplete");
        }
        else    //If there are more one or more errors in the input
        {
            //ISSUE WARNING
            JOptionPane.showMessageDialog(null, WARNING);

        }//END IF continue CHECK
    }//END actionPerformed method
}//END listenGenerate class

I've been looking up and down at the code. Can this that I did not reference any of the layouts outlined in import? I know it's not JPanel as I did try that can still the problem existed. Anything that will help me will be appreciated. Thank you.


Solution

  • You're calling

    jfrFrame.setVisible(true);
    

    first and then adding a bunch of components to the JFrame, and that's backwards and can result in a GUI that does not render components until it is resized or minimized and restored. In fact try this -- run your program, then minimize the blank GUI and restore it, and I'll bet you'll see your components.

    I suggest that you swap this order around -- call jfrFrame.setVisible(true); last after adding everything to the GUI.