how to make a jDialog pop up?

E

Eileen

hey everyone, im doing my science fair project. and my project
calculates the cost of a internal conbustion engine vehicle and an
electric car in different time intervals.

and now i made a jDialog box so when i click run button, it should pop
up the jDialog box, so ppl can input some integers and the program
will use them in the calculation. so how can i make this thing pop up?

thank you so much.
 
M

Mark Space

Eileen said:
hey everyone, im doing my science fair project. and my project
calculates the cost of a internal conbustion engine vehicle and an
electric car in different time intervals.

and now i made a jDialog box so when i click run button, it should pop
up the jDialog box, so ppl can input some integers and the program
will use them in the calculation. so how can i make this thing pop up?

Well first, the Java tutorial is a great resource.

<http://java.sun.com/docs/books/tutorial/uiswing/components/dialog.html>

Next, your basic plan of attack is to add an ActionListener to the
button. That's how you make a button do anything at all. The action
listener should show the dialog, and set the result back to the main window.

Here's a simple example:


class MyInputDialog implements ActionListener
{
MyFrame mainJFrame;

public MyInputDialog( MyFrame output ) {
mainJFrame = output;
}

public void actionPerformed( ActionEvent e )
{
String s = JOptionPane.showInputDialog(
mainJFrame,
"Please input some numbers",
"Your numbers please",
JOptionPane.PLAIN_MESSAGE );
double d;
if( s != null && s.length() > 0 ) {
d = Double.valueOf( s );
mainJFrame.setInput( d );
}
}
}

class MyFrame extends JFrame
{
JTextArea text;
JButton input;

public MyFrame()
{
text = new JTextArea();
input = new JButton( "Click me!");
add( new JScrollPane( text ) );
add( input, BorderLayout.SOUTH );
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
pack();
setSize( 400, 300 );
setLocationRelativeTo( null );
}

public void setInput( double d ) {
text.append( String.valueOf( d ) + "\n" );
}

public void addInputButtonListener( ActionListener a ) {
input.addActionListener( a );
}

private static void createAndShowGui() {
MyFrame mine = new MyFrame();
mine.addInputButtonListener( new MyInputDialog( mine ) );
mine.setVisible( true );
}

public static void main( String... args )
{
javax.swing.SwingUtilities.invokeLater( new Runnable() {
public void run() {
createAndShowGui();
} });
}

}
 
R

Roedy Green

and now i made a jDialog box so when i click run button, it should pop
up the jDialog box, so ppl can input some integers and the program
will use them in the calculation. so how can i make this thing pop up?

see http://mindprod.com/jgloss/jdialog.html
--
Roedy Green Canadian Mind Products
http://mindprod.com

"Here is a point of no return after which warming becomes unstoppable
and we are probably going to sail right through it.
It is the point at which anthropogenic (human-caused) warming triggers
huge releases of carbon dioxide from warming oceans, or similar releases
of both carbon dioxide and methane from melting permafrost, or both.
Most climate scientists think that point lies not far beyond 2°C (4°F) C hotter."
~ Gwynne Dyer
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,781
Messages
2,569,615
Members
45,296
Latest member
HeikeHolli

Latest Threads

Top