473,544 Members | 2,315 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

JButtons stopped performing actions

1 New Member
Hey guys...

Sorry to bother you with this, however, I am stumped. The project that I built - builds clean, and executes, however only one out of three buttons in working. Could someone take a look and see what is going on, because if I keep going around in circles, I am liable to delete the whole project and start again. (Deleted the old version last night when I couldn't figure it out.) It was 2 am, and I was exhausted.....

Thanks in advance.

Ian

import java.awt.Border Layout;
import java.awt.Dimens ion;
import java.awt.GridBa gConstraints;
import java.awt.GridBa gLayout;
//import java.awt.GridLa yout;
import java.awt.Insets ;
import java.awt.event. ActionEvent;
import java.awt.event. ActionListener;
import java.text.Decim alFormat;

import javax.swing.JBu tton;
import javax.swing.JFr ame;
import javax.swing.JLa bel;
import javax.swing.JOp tionPane;
import javax.swing.JPa nel;
import javax.swing.JSc rollPane;
import javax.swing.JTe xtArea;
import javax.swing.JTe xtField;


public class FuelCalc {
JFrame displayPane = new JFrame("Trip Report");
JLabel olabel = new JLabel("Odomete r Reading");
JTextField otext = new JTextField();
JLabel glabel = new JLabel("Gallons Filled");
JTextField gtext = new JTextField();
JLabel clabel = new JLabel("Cost for fill up");
JTextField ctext = new JTextField();
JButton quit = new JButton("Quit") ;
JButton enter = new JButton("Enter" );
JButton report = new JButton("Run Report");
JTextArea text = new JTextArea();

Double[][] FuelArray;
int fai = 0; /*declare variable to use within array to keep track of
current and prior entries*/


public FuelCalc(){

// backend
FuelArray = new Double[3][7];

// GUI
otext.setColumn s(10);
gtext.setColumn s(10);
ctext.setColumn s(10);
quit.addActionL istener(new quit());
enter.addAction Listener(new EnterListener() );
report.addActio nListener(new runreport());

BorderLayout border1 = new BorderLayout();
border1.setHgap (20);
border1.setVgap (20);
displayPane.get ContentPane().s etLayout(border 1);

JPanel panelA = new JPanel();
panelA.setLayou t(new GridBagLayout() );
GridBagConstrai nts gc1 = new GridBagConstrai nts();
gc1.insets = new Insets(10, 10, 10, 10);
gc1.gridx = 0;
gc1.gridy = 0;
panelA.add(olab el, gc1);
gc1.gridx = 1;
gc1.gridy = 0;
panelA.add(otex t, gc1);
gc1.gridx = 0;
gc1.gridy = 1;
panelA.add(glab el, gc1);
gc1.gridx = 1;
gc1.gridy = 1;
panelA.add(gtex t, gc1);
gc1.gridx = 0;
gc1.gridy = 2;
panelA.add(clab el, gc1);
gc1.gridx = 1;
gc1.gridy = 2;
panelA.add(ctex t, gc1);

JScrollPane scrollPane = new JScrollPane(tex t);
scrollPane.setV erticalScrollBa rPolicy(JScroll Pane.VERTICAL_S CROLLBAR_ALWAYS );
scrollPane.setP referredSize(ne w Dimension(500, 200));

JPanel center = new JPanel();
BorderLayout bl2 = new BorderLayout();
bl2.setHgap(20) ;
bl2.setVgap(20) ;
center.setLayou t(bl2);
center.add(pane lA, BorderLayout.WE ST);
center.add(scro llPane, BorderLayout.SO UTH);
displayPane.get ContentPane().a dd(center, BorderLayout.CE NTER);

JPanel right = new JPanel();
right.setLayout (new GridBagLayout() );
GridBagConstrai nts c = new GridBagConstrai nts();
c.gridy = 0;
c.insets = new Insets(10, 10, 10, 10);
right.add(enter , c);
c.gridy = 1;
right.add(repor t, c);
c.gridy = 2;
right.add(quit, c);
displayPane.get ContentPane().a dd(right, BorderLayout.EA ST);
}
public static void main(String[] args) {

FuelCalc gui = new FuelCalc();
gui.showcalc();

}

public void showcalc(){
displayPane.set DefaultCloseOpe ration(JFrame.E XIT_ON_CLOSE);
displayPane.pac k();
displayPane.set Visible(true);
}

public class quit implements ActionListener{
public void actionPerformed (ActionEvent a){
System.exit(0);
}
}



public class EnterListener implements ActionListener{
public void actionPerformed (ActionEvent e){
for(int i = 0; i<=fai-1; i++){
double mileage = FuelArray[i][0];
double gallons = 0;
double cost = 0;
double mileagetotal = FuelArray[fai - 1][3];
double fueltotal = FuelArray[fai - 1][4];
double costtotal = FuelArray[fai - 1][5];
double mpgavg = FuelArray[fai -1][6];

try {
mileage = Double.parseDou ble(otext.getTe xt());
} catch (NumberFormatEx ception e1) {
JOptionPane.sho wMessageDialog( null, "Please try again", "Invalid Input",JOptionP ane.ERROR_MESSA GE);
}

try {
gallons = Double.parseDou ble(gtext.getTe xt());
} catch (NumberFormatEx ception e1) {
JOptionPane.sho wMessageDialog( null, "Please try again", "Invalid Input",JOptionP ane.ERROR_MESSA GE);
}

try {
cost = Double.parseDou ble(ctext.getTe xt());
} catch (NumberFormatEx ception e1) {
JOptionPane.sho wMessageDialog( null, "Please try again", "Invalid Input",JOptionP ane.ERROR_MESSA GE);
}

if(mileage<=(Fu elArray[fai-1][0])){
JOptionPane.sho wMessageDialog( null, "The entered odometer reading must greater than the previous odometer reading",
"Invalid Input",JOptionP ane.ERROR_MESSA GE);
}
if(mileage <= 0){
JOptionPane.sho wMessageDialog( null, "The entered odometer reading must greater than zero",
"Invalid Input",JOptionP ane.ERROR_MESSA GE);
}
else{
// proceed

if(gallons<=0){
JOptionPane.sho wMessageDialog( null, "The wage must greater than 0",
"Invalid Input",JOptionP ane.ERROR_MESSA GE);
}
else{

// Write parsed values from textfields to array
FuelArray[fai][0] = mileage;
FuelArray[fai][1] = gallons;
FuelArray[fai][2] = cost;
//Update totals for mileage, fuel and cost
FuelArray[fai][3] = mileagetotal = mileage - FuelArray[fai-1][0];
FuelArray[fai][4] = fueltotal = gallons + FuelArray[fai-1][4];
FuelArray[fai][5] = costtotal = cost + FuelArray[fai-1][4];
FuelArray[fai][6] = mpgavg = FuelArray[fai][3] / FuelArray[fai][4];
fai++; // Advance row count in array to write next row on "EnterListe ner"

otext.setText(" ");
gtext.setText(" ");
ctext.setText(" ");
}
}
}
}
}
public class runreport implements ActionListener{
public void actionPerformed (ActionEvent b){
text.setText("" ); // clear previous report
String newline = "\n";

// for each entry...
for(int i = 0; i<=fai-1; i++){
double mileage = FuelArray[i][0];
double gallons = FuelArray[i][1];
double cost = FuelArray[i][2];
double mileagetotal = FuelArray[i][3];
double fueltotal = FuelArray[fai][4];
double costtotal = FuelArray[fai][5];
double mpgavg = FuelArray[fai][6];
double lowmpg = 25;
double highmpg = 35;

text.append("Od ometer= " + mileage + " Gallons= " + gallons + " Cost= $" + cost + newline);


DecimalFormat df = new DecimalFormat(" ######.##");

text.append(new line + newline + newline + "************** ************" + newline);
text.append("Tr ip Statistics");
text.append("To tal miles traveled= " + df.format(milea getotal) + newline);
text.append("Av erage Miles Per Gallon= " + df.format(mpgav g) + newline + newline);
text.append("To tal spent on gas: $" + df.format(costt otal) + newline);

if(mpgavg<lowmp g)
text.append("Yo ur miles per gallon is below average");
else if(mpgavg>lowmp g && mpgavg < highmpg)
text.append("Yo ur miles per gallon is average");
else if(highmpg >= mpgavg)
text.append("Yo ur miles per gallon is above average");
}
}
}
}
Attached Files
File Type: zip AAT1.zip (247.8 KB, 76 views)
Mar 21 '10 #1
0 1455

Sign in to post your reply or Sign up for a free account.

Similar topics

12
4379
by: Beer | last post by:
Hello Everyone, We have been getting the EventID 7031 errors ever since we installed SP 3. We have been searching for any possible solution, but not much luck. We are running Windows 2000 server with IIS 5.0 (No indexing services installed), and SP4. Every now and then, we have the 7031 for IIS Admim, SMPT and WWW services stopped...
3
5059
by: Clifford Schneider | last post by:
Hello, I have several SQL Server jobs that execute procedures written to run deletes and updates against tables in my data warehouse. Some jobs occasionally get stuck with the status "Performing Completion Actions" and can only be reset by restarting SQL Server or rebooting the server. I have seen one answer to this problem in a DTS...
1
2283
by: ammarton | last post by:
Hello all...I'm a bit new to working with Macros in Access so forgive me if the terminology I use is not accurate. To preface this, basically I am using a form on a replicated database so the end-user can filter on a specific report they want to see. This database was designed by my predecessor (of which he left no documentation) and I...
4
1577
by: Zeng | last post by:
Hello, I'm wondering if anybody has seen this problem. I basically need to cycle through ~30000 db rows to update the data, I load up the id of the rows I need first, put them into ArrayList, close the connection, then process through one record at a time, so there is no nested transaction. It normally take 1 hour or more, after about 45...
2
2009
by: Chris Fink | last post by:
I have the need to perform an atomic operation in two systems, a database and an ftp site. The process is two step; 1. insert a row into oracle, 2. delete a file from a remote ftp site. I need to make these steps atomic...either they both succeed or both fail. Any suggestions on how I create a transaction that bridges these two steps...
2
2440
by: Trevor | last post by:
Argh! This problem is driving me nuts! Can you help? In November of 2003, I installed a web service on Windows Server 2003 built in VB.NET for v1.1.4322 of the framework. It contains a timer (System.Timers.Timer) which has an interval of 24 hours. Actually, it reads a time like 2AM out of the config file, and calculates the time between...
9
3218
by: monomaniac21 | last post by:
hi all i want to use hyperlinks to 'load' content by changing the display of div tags. the problem i have is that unless i specify a href the anchor does not change the mouse pointer on hover even if display is set to block. it only look changes when there is a href there. but if i have a href there then when i click it will load the page,...
2
5814
by: JinFTW | last post by:
Hey guys I hate to bother you with this, but I'm having a problem with creating an arrayList of JButtons that will end up using an actionlistener. My original program was this: import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; public class KeyBorderPanel extends JPanel
1
2980
by: Leon Mayne | last post by:
Hello, I have a gridview that's bound to a generic list of business objects and am using the RowCommand event to capture user clicks on actions. This was working fine up until today, but now e.CommandArgument being passed into the RowCommand event handler is always coming back as an empty string and causing an invalid cast exception. The...
2
10118
by: Stratocaster | last post by:
Hello, and thank you for any help in advance. I need help determining if any commands exist in VB (Excel macro style) that can enable a user to select cells and run a macro which performs operations on those cells. I am a beginner and am not sure if this is possible. And if you'd really like to teach me a lesson. . . . Could this be...
0
7449
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7642
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7737
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5950
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5316
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4938
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3432
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
688
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.