-
Trying to get two textBoxes and two JComboButtons to work with three dropdown boxes.
I am not getting any compiling errors except warnings but the layout is working like it should. Using my text book I can create a JFrame with the panel and text boxes and I can create the JFrame with ComboBoxes it just doesn't tell me how to do them both at the same time. Any help would be appreciated. I don't want you to just give me the code which most here don't but if you have any suggested locations where this can be read so I can learn it that would be more helpful.
Code :
/* Chapter 7
*
*
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
public class TripCalc extends JFrame implements ActionListener
{
//construct components
JLabel fuelPrompt = new JLabel("Fuel Costs");
JComboBox fuelCombo = new javax.swing.JComboBox();
JTextPane fuelPane = new JTextPane();
JLabel beginningPrompt = new JLabel("Starting Location");
JComboBox startCombo = new javax.swing.JComboBox();
JTextPane startPane = new JTextPane();
JLabel endPrompt = new JLabel("Ending Location");
JComboBox endCombo = new javax.swing.JComboBox();
JTextPane endPane = new JTextPane();
// Panel Creation
JPanel firstRow = new JPanel();
JPanel secondRow = new JPanel();
// Panel for Fields and Buttons
JPanel fieldPanel = new JPanel();
JPanel buttonPanel = new JPanel();
// Construct Labels and TextBoxes
JLabel Miles = new JLabel("Miles planning to travel");
JTextField distance = new JTextField(10);
JLabel Fuelcost = new JLabel("What is the price per gallon/litre?");
JTextField fuelprice = new JTextField(5);
JButton submitButton = new JButton("Submit");
//create the content pane
public Container createContentPane()
{
//populate the JCombobox
fuelCombo.addItem("Leaded");
fuelCombo.addItem("Unleaded");
fuelCombo.addItem("Super Unleaded");
fuelCombo.addItem("Diesel");
fuelCombo.addActionListener(this);
fuelCombo.setToolTipText("Click on the type of fuel you will be using");
startCombo.addItem("Minneapolis");
startCombo.addItem("Duluth");
startCombo.addItem("Bemidji");
startCombo.addItem("Rochester");
startCombo.addItem("Detroit Lakes");
startCombo.addItem("St.Cloud");
startCombo.addActionListener(this);
startCombo.setToolTipText("Click on your starting City.");
endCombo.addItem("Daytona");
endCombo.addItem("Darlington");
endCombo.addItem("Las Vegas");
endCombo.addItem("Talladega");
endCombo.addItem("Richmond");
endCombo.addItem("Chicago");
endCombo.addActionListener(this);
endCombo.setToolTipText("Click on your starting City.");
//construct and populate the north panel
JPanel northPanel = new JPanel();
northPanel.setLayout(new FlowLayout());
northPanel.add(fuelPrompt);
northPanel.add(fuelCombo);
northPanel.add(beginningPrompt);
northPanel.add(startCombo);
northPanel.add(endPrompt);
northPanel.add(endCombo);
//createContainer and set attributes
Container f = getContentPane();
setLayout(new BorderLayout(20,20));
fieldPanel.setLayout(new GridLayout(4,2));
FlowLayout rowSetup = new FlowLayout(FlowLayout.LEFT,4,2);
firstRow.setLayout(rowSetup);
secondRow.setLayout(rowSetup);
buttonPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
add(northPanel,BorderLayout.NORTH);
setLocation(200,200);
return f;
}
//main method executes at run time
public static void main(String args[])
{
TripCalc f = new TripCalc();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(f.createContentPane());
f.setTitle("Trip Calculator");
f.setSize(800,300);
f.setVisible(true);
}
// event to process user clicks
public void actionPerformed(ActionEvent e)
{
String arg = e.getActionCommand();
}
}
-
Re: Trying to get two textBoxes and two JComboButtons to work with three dropdown boxes.
Quote:
how to do them both at the same time
Can you explain what your problem is?
Are you trying to build a GUI displaying multiple components like textboxes and comboboxes?
First you need to have a design of where they all go.
Then use JPanels to hold different combinations of the components and add those JPanels to the JfFrame where you want them shown.
See the tutorial: Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)
-
Re: Trying to get two textBoxes and two JComboButtons to work with three dropdown boxes.
I've gotten to the text fields are there but no button or anything else.
Code :
/* Chapter 7
*
*
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
public class TripCalc extends JFrame implements ActionListener
{
//construct components
JLabel fuelPrompt = new JLabel("Fuel Costs");
JComboBox fuelCombo = new javax.swing.JComboBox();
JTextPane fuelPane = new JTextPane();
JLabel beginningPrompt = new JLabel("Starting Location");
JComboBox startCombo = new javax.swing.JComboBox();
JTextPane startPane = new JTextPane();
JLabel endPrompt = new JLabel("Ending Location");
JComboBox endCombo = new javax.swing.JComboBox();
JTextPane endPane = new JTextPane();
// Panel Creation
JPanel firstRow = new JPanel();
JPanel secondRow = new JPanel();
// Panel for Fields and Buttons
JPanel fieldPanel = new JPanel();
JPanel buttonPanel = new JPanel();
// Construct Labels and TextBoxes
JLabel Miles = new JLabel("Miles planning to travel");
JTextField distance = new JTextField(10);
JLabel Fuelcost = new JLabel("What is the price per gallon/litre?");
JTextField fuelprice = new JTextField(5);
JButton submitButton = new JButton("Submit");
//create the content pane
public Container createContentPane()
{
//populate the JCombobox
fuelCombo.addItem("Leaded");
fuelCombo.addItem("Unleaded");
fuelCombo.addItem("Super Unleaded");
fuelCombo.addItem("Diesel");
fuelCombo.addActionListener(this);
fuelCombo.setToolTipText("Click on the type of fuel you will be using");
startCombo.addItem("Minneapolis");
startCombo.addItem("Duluth");
startCombo.addItem("Bemidji");
startCombo.addItem("Rochester");
startCombo.addItem("Detroit Lakes");
startCombo.addItem("St.Cloud");
startCombo.addActionListener(this);
startCombo.setToolTipText("Click on your starting City.");
endCombo.addItem("Daytona");
endCombo.addItem("Darlington");
endCombo.addItem("Las Vegas");
endCombo.addItem("Talladega");
endCombo.addItem("Richmond");
endCombo.addItem("Chicago");
endCombo.addActionListener(this);
endCombo.setToolTipText("Click on your starting City.");
//construct and populate the north panel
JPanel northPanel = new JPanel();
northPanel.setLayout(new FlowLayout());
northPanel.add(fuelPrompt);
northPanel.add(fuelCombo);
northPanel.add(beginningPrompt);
northPanel.add(startCombo);
northPanel.add(endPrompt);
northPanel.add(endCombo);
JPanel southPanel = new JPanel();
southPanel.setLayout(new FlowLayout());
southPanel.add(distance);
southPanel.add(fuelprice);
//createContainer and set attributes
Container f = getContentPane();
setLayout(new BorderLayout(40,40));
fieldPanel.setLayout(new GridLayout(2,1));
FlowLayout rowSetup = new FlowLayout(FlowLayout.LEFT,2,1);
firstRow.setLayout(rowSetup);
secondRow.setLayout(rowSetup);
buttonPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
add(northPanel,BorderLayout.NORTH);
add(southPanel,BorderLayout.CENTER);
setLocation(200,200);
return f;
}
//main method executes at run time
public static void main(String args[])
{
TripCalc f = new TripCalc();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(f.createContentPane());
f.setTitle("Trip Calculator");
f.setSize(800,300);
f.setVisible(true);
}
// event to process user clicks
public void actionPerformed(ActionEvent e)
{
String arg = e.getActionCommand();
}
}
-
Re: Trying to get two textBoxes and two JComboButtons to work with three dropdown boxes.
Where do you add any buttons to a container?
-
Re: Trying to get two textBoxes and two JComboButtons to work with three dropdown boxes.
I am trying to create an applet that will show drop down boxes that display fuel types (have that working) along with 6 cities to start from and destinations (got those also working). I am to have 2 text fields where I can input Miles and Cost per gallon along with a submit button and clear button. I've got the Submit Button and Clear button to display properly. I also have the text boxes, but I am working on getting the text labels to show up. My existing code below.
Code :
/* Chapter 7
*
*
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
public class TripCalc extends JFrame implements ActionListener
{
//construct components
JLabel fuelPrompt = new JLabel("Fuel Type");
JComboBox fuelCombo = new javax.swing.JComboBox();
JTextPane fuelPane = new JTextPane();
JLabel beginningPrompt = new JLabel("Starting Location");
JComboBox startCombo = new javax.swing.JComboBox();
JTextPane startPane = new JTextPane();
JLabel endPrompt = new JLabel("Ending Location");
JComboBox endCombo = new javax.swing.JComboBox();
JTextPane endPane = new JTextPane();
// Panel Creation
JPanel firstRow = new JPanel();
JPanel secondRow = new JPanel();
// Panel for Fields and Buttons
JPanel fieldPanel = new JPanel();
JPanel buttonPanel = new JPanel();
// Construct Labels and TextBoxes
JLabel Miles = new JLabel("Miles planning to travel");
JTextField distance = new JTextField(10);
JLabel Fuelcost = new JLabel("What is the price per gallon/litre?");
JTextField fuelprice = new JTextField(5);
JButton submitButton = new JButton("Submit");
JButton clearButton = new JButton("Clear Fields");
//create the content pane
public Container createContentPane()
{
//populate the JCombobox
fuelCombo.addItem("Leaded");
fuelCombo.addItem("Unleaded");
fuelCombo.addItem("Super Unleaded");
fuelCombo.addItem("Diesel");
fuelCombo.addActionListener(this);
startCombo.addItem("Minneapolis");
startCombo.addItem("Duluth");
startCombo.addItem("Bemidji");
startCombo.addItem("Rochester");
startCombo.addItem("Detroit Lakes");
startCombo.addItem("St.Cloud");
startCombo.addActionListener(this);
endCombo.addItem("Daytona");
endCombo.addItem("Darlington");
endCombo.addItem("Las Vegas");
endCombo.addItem("Talladega");
endCombo.addItem("Richmond");
endCombo.addItem("Chicago");
endCombo.addActionListener(this);
//construct and populate the north panel
JPanel northPanel = new JPanel();
northPanel.setLayout(new FlowLayout());
northPanel.add(fuelPrompt);
northPanel.add(fuelCombo);
northPanel.add(beginningPrompt);
northPanel.add(startCombo);
northPanel.add(endPrompt);
northPanel.add(endCombo);
JPanel southPanel = new JPanel();
southPanel.setLayout(new FlowLayout());
southPanel.add(distance);
southPanel.add(fuelprice);
firstRow.add(Miles);
firstRow.add(Fuelcost);
southPanel.add(submitButton);
southPanel.add(clearButton);
//createContainer and set attributes
Container f = getContentPane();
setLayout(new BorderLayout(40,40));
fieldPanel.setLayout(new GridLayout(10,1));
buttonPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
add(northPanel,BorderLayout.NORTH);
add(southPanel,BorderLayout.SOUTH);
setLocation(200,200);
return f;
}
//main method executes at run time
public static void main(String args[])
{
TripCalc f = new TripCalc();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(f.createContentPane());
f.setTitle("Trip Calculator");
f.setSize(800,300);
f.setVisible(true);
}
// event to process user clicks
public void actionPerformed(ActionEvent e)
{
String arg = e.getActionCommand();
}
}
-
Re: Trying to get two textBoxes and two JComboButtons to work with three dropdown boxes.
Quote:
I am trying to create an applet
The posted code is an application not an applet. Applets execute in a browser.
Quote:
working on getting the text labels to show up
Can you explain what the problem is when you execute the code? I see some labels when I compile and execute the code.
-
Re: Trying to get two textBoxes and two JComboButtons to work with three dropdown boxes.
If i'm understanding your question correctly i've added them to the center.Panel and south.Panel and then added those panels to the layout in the container.
--- Update ---
Quote:
Originally Posted by
Norm
The posted code is an application not an applet. Applets execute in a browser.
Can you explain what the problem is when you execute the code? I see some labels when I compile and execute the code.
For the text boxes I want a label that says "How many miles do you plan to drive" and the other for "Fuel Price".
I get labels for the ComboBoxes and the buttons, but not for my text boxes.
-
Re: Trying to get two textBoxes and two JComboButtons to work with three dropdown boxes.
Quote:
I get labels for the ComboBoxes and the buttons, but not for my text boxes.
What are the variable names for the labels that are not showing?
-
Re: Trying to get two textBoxes and two JComboButtons to work with three dropdown boxes.
I believe you are referring to JTextField distance and fuelprice?
-
Re: Trying to get two textBoxes and two JComboButtons to work with three dropdown boxes.
I was asking for the names of the variables for the labels that are not showing so I can find them in the source and see where you haven't added them to the GUI that is being shown.
-
Re: Trying to get two textBoxes and two JComboButtons to work with three dropdown boxes.
Code :
JLabel Miles = new JLabel("Miles planning to travel");
JTextField distance = new JTextField(10);
JLabel Fuelcost = new JLabel("What is the price per gallon/litre?");
JTextField fuelprice = new JTextField(5);
I would like the labels to have those questions for the text and if I understand the variables are fuelprice and distance to later use in my calculation code.
-
Re: Trying to get two textBoxes and two JComboButtons to work with three dropdown boxes.
Where do you add those two JLabels to the GUI that is showing? Look at where you have added other components that are showing to see if you have added the missing labels in a way that they are added to the GUI that is showing. If they aren't connected to the GUI they won't show.
-
Re: Trying to get two textBoxes and two JComboButtons to work with three dropdown boxes.
Quote:
Originally Posted by
JamesdTurnham
Code :
/* Chapter 7
*
*
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
public class TripCalc extends JFrame implements ActionListener
{
//construct components
JLabel fuelPrompt = new JLabel("Fuel Type");
JComboBox fuelCombo = new javax.swing.JComboBox();
JTextPane fuelPane = new JTextPane();
JLabel beginningPrompt = new JLabel("Starting Location");
JComboBox startCombo = new javax.swing.JComboBox();
JTextPane startPane = new JTextPane();
JLabel endPrompt = new JLabel("Ending Location");
JComboBox endCombo = new javax.swing.JComboBox();
JTextPane endPane = new JTextPane();
// Panel Creation
JPanel firstRow = new JPanel();
JPanel secondRow = new JPanel();
// Panel for Fields and Buttons
JPanel fieldPanel = new JPanel();
JPanel buttonPanel = new JPanel();
// Construct Labels and TextBoxes
JLabel Miles = new JLabel("Miles planning to travel" );
JTextField distance = new JTextField(5);
JLabel Fuelcost = new JLabel("What is the price per gallon/litre?" );
JTextField fuelprice = new JTextField(3);
JLabel Total = new JLabel("Your total cost for the trip will be" );
JTextField totalcost = new JTextField(6);
JButton submitButton = new JButton("Submit");
JButton clearButton = new JButton("Clear Fields");
//create the content pane
public Container createContentPane()
{
//populate the JCombobox
fuelCombo.addItem("Leaded");
fuelCombo.addItem("Unleaded");
fuelCombo.addItem("Super Unleaded");
fuelCombo.addItem("Diesel");
fuelCombo.addActionListener(this);
startCombo.addItem("Minneapolis");
startCombo.addItem("Duluth");
startCombo.addItem("Bemidji");
startCombo.addItem("Rochester");
startCombo.addItem("Detroit Lakes");
startCombo.addItem("St.Cloud");
startCombo.addActionListener(this);
endCombo.addItem("Daytona");
endCombo.addItem("Darlington");
endCombo.addItem("Las Vegas");
endCombo.addItem("Talladega");
endCombo.addItem("Richmond");
endCombo.addItem("Chicago");
endCombo.addActionListener(this);
//construct and populate the north panel
JPanel northPanel = new JPanel();
northPanel.setLayout(new FlowLayout());
northPanel.add(fuelPrompt);
northPanel.add(fuelCombo);
northPanel.add(beginningPrompt);
northPanel.add(startCombo);
northPanel.add(endPrompt);
northPanel.add(endCombo);
JPanel southPanel = new JPanel();
southPanel.setLayout(new FlowLayout());
southPanel.add(submitButton);
southPanel.add(clearButton);
JPanel centerPanel = new JPanel();
centerPanel.setLayout(new FlowLayout());
centerPanel.add(Miles);
centerPanel.add(distance);
centerPanel.add(Fuelcost);
centerPanel.add(fuelprice);
centerPanel.add(Total);
centerPanel.add(totalcost);
//createContainer and set attributes
Container f = getContentPane();
setLayout(new BorderLayout(40,40));
fieldPanel.setLayout(new GridLayout(1,1));
buttonPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
add(northPanel,BorderLayout.NORTH);
add(southPanel,BorderLayout.SOUTH);
add(centerPanel,BorderLayout.CENTER);
setLocation(200,200);
return f;
}
//main method executes at run time
public static void main(String args[])
{
TripCalc f = new TripCalc();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(f.createContentPane());
f.setTitle("Trip Calculator");
f.setSize(800,300);
f.setVisible(true);
}
// event to process user clicks
public void actionPerformed(ActionEvent e)
{
String arg = e.getActionCommand();
}
}
Ok. I've got this part all figured out now and the display is doing what I want it to do.
Now if your wiling I need to code this so that when I select the various drop downs it will display my total costs. First I have to get the distance for all 6 drop down options and do all the calculations for that. I just may need a little help with certain aspects of it.
Thanks so much for the help so far.
-
Re: Trying to get two textBoxes and two JComboButtons to work with three dropdown boxes.
Quote:
when I select the various drop downs it will display my total costs.
Can you explain the steps the code takes or should take and what the problems are?
-
Re: Trying to get two textBoxes and two JComboButtons to work with three dropdown boxes.
It's supposed to calculate the total miles traveled by cost per gallon either manually input into the text field or using the dropdown for vehicle type. In essence user selects drop down for vehicle type and MPG is selected then drop down for fuel type it will auto fill the cost per gallon and the miles would be from starting location to ending location OR the person can enter that in manually with miles to travel and cost per gallon with vehicle type being used in the dropdown box.
-
Re: Trying to get two textBoxes and two JComboButtons to work with three dropdown boxes.
The steps the program needs to take could be:
Gather the data needed
compute the results
display the results
-
Re: Trying to get two textBoxes and two JComboButtons to work with three dropdown boxes.
Quote:
Originally Posted by
Norm
The steps the program needs to take could be:
Gather the data needed
compute the results
display the results
I've got this part figured out from a previous assignment. What I haven't figured out is when I select say Bemidji to Darlington the coding to indicate that Bemidji was selected as the start and Darlington was selected as the ending location.
-
Re: Trying to get two textBoxes and two JComboButtons to work with three dropdown boxes.
Quote:
when I select say Bemidji to Darlington the coding to indicate that Bemidji was selected as the start and Darlington was selected as the ending location.
What happens when you select each of those items? Are the listeners called so that they can save what was selected?
Or when do you determine what was selected?
-
Re: Trying to get two textBoxes and two JComboButtons to work with three dropdown boxes.
the listeners are in the code, but that's the part I don't know is what or how to get the listener to "hear" what is being selected. I presume I add the code to this part "public void actionPerformed(ActionEvent e)
{
String arg = e.getActionCommand();
}" but that's the part that has me currently stumped.
-
Re: Trying to get two textBoxes and two JComboButtons to work with three dropdown boxes.
In the listener you get a reference to the component that caused the event from the object passed to the listener. Cast that object to the type of component it is: say a combobox and then you can call its methods to see what was done, like what was selected.
See the tutorial: http://docs.oracle.com/javase/tutori...nts/index.html