I have a program that figures out what the total cost for gas is on trip. I now have another piece made where you enter the trip data to save it to a file. How do I make the main program call this new window so the user can enter and save the trip data?

Does your class which is going to be imported belong to a package? If so, you need to use import in order to use the class. If it does not, you simply place the class's .java or .class file in the same folder of the caller class file.

What I have is a programm that calculates the cost of a trip and when the user is done, I want the user to have the option to enter and save the trip info in a new window. I was looking at using a JOptionPane box to ask yes or no and if I get a yes, the new wondow opens up and if it is a no, the program closes.

You are talking about GUI then. Instead of popping up a new window each request, how about repaint your current window interface (frame) with a new panel? So your application would have a frame as the main display (window), any component inside of the frame can be removed/added for any new content you want to display/work with. The JOptionPane box is just a way to get an input from a user. It has nothing much to do with your display or using other classes inside your program. What do you have so far? Are you implementing it in an Applet or a program with GUI?

Edited 4 Years Ago by Taywin

I thought about adding this code to what I have but I want the buttons to be in the same frame and when I do that, only the button shows and the rest of the fields are gone. If I can fix that problem, I would add this code to the original code. I am implementing JFrame.

How did you add components to your frame? If you are adding multiple panels to a frame, the last panel would be the one on the top which is being displayed. The rest will be hiding. You need to think about how you are you going to display each panel and tie them with your event listener. How do you add a button to the panel by the way?

Right now I have two field panels that are placed in the north and center area and have a button panel that is placed in the south . I tried adding my buttons in the field panels but when I do, all I get is the button. Here is the code that sets up the look ok my panel:

//COnstruct a panel for each row
    JPanel firstRow = new JPanel();
    JPanel secondRow = new JPanel();
    JPanel thirdRow = new JPanel();
    JPanel fourthRow = new JPanel();
    JPanel fifthRow = new JPanel();
    JPanel sixthRow = new JPanel();
    JPanel seventhRow = new JPanel();
    JPanel eighthRow = new JPanel();
    JPanel ninthRow = new JPanel();
    JPanel tenthRow = new JPanel();


    //Construct a panel for the fields and button
    JPanel fieldPanelOne = new JPanel();
    JPanel fieldPanelTwo = new JPanel();
    JPanel buttonPanelOne = new JPanel();
    JPanel imagePanel = new JPanel();

    //Construct labels and texts boxes
    JLabel beginningLocationLabel = new JLabel("Beginning Location: ");
        JComboBox cityBox = new JComboBox(location);
    JLabel destinationLabel = new JLabel("Destination: ");
        JComboBox cityBoxTwo = new JComboBox(locationTwo);
    JLabel milesLabel = new JLabel("Trip Distance: ");
        JTextField milesField = new JTextField(10);
    JLabel vehicleSizeLabel = new JLabel("Vehicle Size: ");
        JComboBox vehicleBox = new JComboBox(vehicle);
    JLabel fuelLabel = new JLabel("Fuel Type: ");
        JComboBox fuelBox = new JComboBox(fuelPrice);
    JLabel submitLabel = new JLabel("           Press the Submit Button");
    JLabel milesTwoLabel = new JLabel("Trip Distance: ");
        JTextField milesTwoField = new JTextField(10);
    JLabel milesPerGallonLabel = new JLabel("Vehicle MGP: ");
        JTextField milesPerGallonField = new JTextField(10);
    JLabel costPerGallonLabel = new JLabel("Cost Of Fuel Per Gallon: ");
        JTextField costPerGallonField = new JTextField(10);
    JLabel calcLabel = new JLabel("           Press the Calculate button");
    ImageIcon image = new ImageIcon("C:\\Users\\Broc\\Desktop\\School\\Rasmussen\\2012\\Fall 2012\\Java 1\\Week 9\\car.gif");
        JLabel imageLabel = new JLabel(image);


    // Construct button
    JButton calcButton = new JButton("Calculate");
    JButton submitButton = new JButton("Submit");
    JButton clearButton = new JButton("Clear");

    public static void main(String[] args)
    {
        //set the look of the interface
        try
        {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
        }
        catch(Exception e)
        {
            JOptionPane.showMessageDialog(null,"The Traveler's Gasoline Calculator could not set the Look and Feel.","Error", JOptionPane.INFORMATION_MESSAGE);
        }

        TravelerGasolineCalculator f = new TravelerGasolineCalculator();
        f.setSize(450,400);
        f.setTitle("Traveler's Gasoline Calculator");
        f.setResizable(true);
        f.setLocation(200,200);
        f.setVisible(true);
    }

    public TravelerGasolineCalculator()
    {
        Container c = getContentPane();
        c.setLayout((new BorderLayout()));
        fieldPanelOne.setLayout(new GridLayout(6,2));
        fieldPanelTwo.setLayout(new GridLayout(4,2));
        FlowLayout rowSetup = new FlowLayout(FlowLayout.LEFT,5,3);
            firstRow.setLayout(rowSetup);
            secondRow.setLayout(rowSetup);
            thirdRow.setLayout(rowSetup);
            fourthRow.setLayout(rowSetup);
            fifthRow.setLayout(rowSetup);
            sixthRow.setLayout(rowSetup);
            seventhRow.setLayout(rowSetup);
            eighthRow.setLayout(rowSetup);
            ninthRow.setLayout(rowSetup);
            tenthRow.setLayout(rowSetup);
        buttonPanelOne.setLayout(new FlowLayout(FlowLayout.CENTER));

        //Add Fields to rows
        firstRow.add(beginningLocationLabel);
        firstRow.add(cityBox);

        secondRow.add(destinationLabel);
        secondRow.add(cityBoxTwo);

        thirdRow.add(milesLabel);
        thirdRow.add(milesField);

        fourthRow.add(vehicleSizeLabel);
        fourthRow.add(vehicleBox);

        fifthRow.add(fuelLabel);
        fifthRow.add(fuelBox);

        sixthRow.add(submitLabel);

        seventhRow.add(milesTwoLabel);
        seventhRow.add(milesTwoField);

        eighthRow.add(milesPerGallonLabel);
        eighthRow.add(milesPerGallonField);

        ninthRow.add(costPerGallonLabel);
        ninthRow.add(costPerGallonField);

        tenthRow.add(calcLabel);


        //Adds rows to panel
        fieldPanelOne.add(firstRow);
        fieldPanelOne.add(secondRow);
        fieldPanelOne.add(thirdRow);
        fieldPanelOne.add(fourthRow);
        fieldPanelOne.add(fifthRow);
        fieldPanelOne.add(sixthRow);
        fieldPanelTwo.add(seventhRow);
        fieldPanelTwo.add(eighthRow);
        fieldPanelTwo.add(ninthRow);
        fieldPanelTwo.add(tenthRow);

        //adds button to panel
        buttonPanelOne.add(submitButton);
        buttonPanelOne.add(calcButton);
        buttonPanelOne.add(clearButton);

        imagePanel.add(imageLabel);

        //Adds panels to frame
        c.add(fieldPanelOne, BorderLayout.NORTH);
        c.add(fieldPanelTwo, BorderLayout.CENTER);
        c.add(buttonPanelOne, BorderLayout.SOUTH);
        c.add(imagePanel, BorderLayout.EAST);

        //adds funtion to buttons
        calcButton.addActionListener(this);
        submitButton.addActionListener(this);
        clearButton.addActionListener(this);

        //adds function to dropdown lists
        vehicleBox.addActionListener(this);
        fuelBox.addActionListener(this);
        cityBox.addActionListener(this);
        cityBoxTwo.addActionListener(this);
This question has already been answered. Start a new discussion instead.