943,947 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 40760
  • Java RSS
Aug 3rd, 2005
0

Java GUI JOptionPane.showMessageDialog

Expand Post »
Hiya, I am using following Java file which creates a GUI, in the input field if i put any character why dont I see the JOptionPane.showMessageDialog pop up window as mentioned in the code?

Any clues?

Thx
Rony


//import statements
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

import java.text.DecimalFormat;



/**
* Sediment Yield MUSLE
* Shahriar Shams
* class description
*/
public class SedimentYield1 extends JFrame implements ActionListener
{

double V, Qp, K, LS, C, P;
String outputSY;
boolean goodInput = true;
double SY;

JPanel SYPanelInput = new JPanel();
JPanel SYPanelCentre = new JPanel();
JPanel SYPanelOutput = new JPanel();

private JButton jbtCalculate = new JButton ("Calculate");
private JButton jbtClear = new JButton ("Clear");


//Input

JLabel Vlbl = new JLabel("Volume of Runoff in acre-ft, V");
JTextField Vtf = new JTextField(8);

JLabel Qplbl = new JLabel("Peak Flow per Storm Event in cfs, Qp");
JTextField Qptf = new JTextField(8);

JLabel Klbl = new JLabel("Soil Erodibility Factor, K");
JTextField Ktf = new JTextField(8);

JLabel LSlbl = new JLabel("Soil Loss Ratio, LS");
JTextField LStf = new JTextField(8);

JLabel Clbl = new JLabel("Cropping Factor, C");
JTextField Ctf = new JTextField(8);

JLabel Plbl = new JLabel("Practice Factor, P");
JTextField Ptf = new JTextField(8);



//ouput
JLabel SYlbl = new JLabel("Sediment Yield per storm in tons");
JTextField SYtf = new JTextField("Will be computed");


public SedimentYield1()

{



SYPanelInput.setLayout(new GridLayout(6,2));
SYPanelInput.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
SYPanelCentre.setLayout(new GridLayout(1,2));
SYPanelCentre.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
SYPanelOutput.setLayout(new GridLayout(1,2));
SYPanelOutput.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));



//adding Components

SYPanelInput.add(Vlbl);
SYPanelInput.add(Vtf);

SYPanelInput.add(Qplbl);
SYPanelInput.add(Qptf);

SYPanelInput.add(Klbl);
SYPanelInput.add(Ktf);

SYPanelInput.add(LSlbl);
SYPanelInput.add(LStf);

SYPanelInput.add(Clbl);
SYPanelInput.add(Ctf);

SYPanelInput.add(Plbl);
SYPanelInput.add(Ptf);

//output added

SYPanelCentre.add(SYlbl);
SYPanelCentre.add(SYtf);



// adding buttons

SYPanelOutput.add(jbtCalculate);
jbtCalculate.addActionListener(this);
SYPanelOutput.add(jbtClear);
jbtClear.addActionListener(this);

String strV = Vtf.getText();
String strQp = Qptf.getText();
String strK = Ktf.getText();
String strLS = LStf.getText();
String strC = Ctf.getText();
String strP = Ptf.getText();


try
{

V = Double.parseDouble(strV);


}

catch (NumberFormatException nfeDouble)
{

JOptionPane.showMessageDialog(null, "You need to enter a\nnumber", "Input Error", JOptionPane.ERROR_MESSAGE);
goodInput = false;

}
try
{


Qp = Double.parseDouble(strQp);


}

catch (NumberFormatException nfeDouble)
{

JOptionPane.showMessageDialog(null, "You need to enter a\nnumber", "Input Error", JOptionPane.ERROR_MESSAGE);
goodInput = false;

}
try
{


K = Double.parseDouble(strK);


}

catch (NumberFormatException nfeDouble)
{

JOptionPane.showMessageDialog(null, "You need to enter a\nnumber", "Input Error", JOptionPane.ERROR_MESSAGE);
goodInput = false;

}
try
{


LS = Double.parseDouble(strLS);

}

catch (NumberFormatException nfeDouble)
{

JOptionPane.showMessageDialog(null, "You need to enter a\nnumber", "Input Error", JOptionPane.ERROR_MESSAGE);
goodInput = false;

}
try
{


C = Double.parseDouble(strC);


}

catch (NumberFormatException nfeDouble)
{

JOptionPane.showMessageDialog(null, "You need to enter a\nnumber", "Input Error", JOptionPane.ERROR_MESSAGE);
goodInput = false;

}
try
{


P = Double.parseDouble(strP);

}

catch (NumberFormatException nfeDouble)
{

JOptionPane.showMessageDialog(null, "You need to enter a\nnumber", "Input Error", JOptionPane.ERROR_MESSAGE);
goodInput = false;

}


if (goodInput)
{

//Calculating Sediment Yield
SY = 95.0*(Math.pow((V*Qp),0.56))*K*LS*C*P;

//Create the format for the Celsius text field.
DecimalFormat twoDigits = new DecimalFormat("0.00");
outputSY = twoDigits.format(SY);


}







// JFrame.setDefaultLookAndFeelDecorated(true);

//2. Create the frame.
JFrame frame = new JFrame("Sedidement Yield from Watershed");

//3. Optional: What happens when the frame closes?
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//4. Create components and put them in the frame.
//...create emptyLabel...

frame.getContentPane().add(SYPanelInput, BorderLayout.NORTH);
frame.getContentPane().add(SYPanelCentre, BorderLayout.CENTER);
frame.getContentPane().add(SYPanelOutput, BorderLayout.SOUTH);
//5. Size the frame.
frame.setSize(new Dimension(500,280));
//6. Show it.
frame.setVisible(true);
}

public static void main(String[] args) {
SedimentYield SYframe = new SedimentYield();

}


public void actionPerformed(ActionEvent e) {


if (e.getSource()== jbtClear)
{

Vtf.setText("");
Qptf.setText("");
Ktf.setText("");
LStf.setText("");
Ctf.setText("");
Ptf.setText("");
SYtf.setText("Will be calculated");

}


if (e.getSource()== jbtCalculate)
{

System.out.println("Sediment Yield ="+outputSY);
SYtf.setText(outputSY);

}


}
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rony is offline Offline
4 posts
since Jul 2005
Aug 3rd, 2005
0

Re: Java GUI JOptionPane.showMessageDialog

I'm not going to read a thousand lines of unformated code.
Use code tags and post only the relevant bits.

Most likely you never attached the code that shows the message to the component where you want it shown.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Aug 3rd, 2005
0

Re: Java GUI JOptionPane.showMessageDialog

It never throws the exception?
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Aug 3rd, 2005
0

Re: Java GUI JOptionPane.showMessageDialog

Hi everyone,

It does not pop up because there was no exception thrown as you option pane only shows up if there are exceptions or errors much like your below code

Java Syntax (Toggle Plain Text)
  1. try
  2. {
  3. //some code
  4. }
  5.  
  6. catch (NumberFormatException nfeDouble)
  7. {
  8.  
  9. JOptionPane.showMessageDialog(null, "You need to enter a\nnumber", "Input Error", JOptionPane.ERROR_MESSAGE);
  10. goodInput = false;
  11. }

I hope this helps you

Yours Sincerely

Richard West
Reputation Points: 25
Solved Threads: 10
Practically a Master Poster
freesoft_2000 is offline Offline
623 posts
since Jun 2004
Nov 23rd, 2010
0
Re: Java GUI JOptionPane.showMessageDialog
develop a program to accept a sentence then display the word in reverse form. take note that a sentence should start w/ a capital letters and ends w/ a period. Further , count the total number of words and total number A's - Z's and a's - z's implement using JOptionPane


sample

input : you're cool.


output:


cool you're.

total: 2

A: 0 a=1
B:0 b=0
.... ...
........
.. ....
Z=0 z=0
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bolzjohn is offline Offline
1 posts
since Nov 2010
Nov 24th, 2010
0
Re: Java GUI JOptionPane.showMessageDialog
Click to Expand / Collapse  Quote originally posted by bolzjohn ...
develop a program to accept a sentence then display the word in reverse form. take note that a sentence should start w/ a capital letters and ends w/ a period. Further , count the total number of words and total number A's - Z's and a's - z's implement using JOptionPane


sample

input : you're cool.


output:


cool you're.

total: 2

A: 0 a=1
B:0 b=0
.... ...
........
.. ....
Z=0 z=0
Start a new thread, show some effort and use the split(" ") method of the String class. Print the array that it returns:
Java Syntax (Toggle Plain Text)
  1. String input = "you're cool";
  2. String [] arr = input.split(" ");
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,259 posts
since Dec 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: ComboBox
Next Thread in Java Forum Timeline: Help making a tester class





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC