Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

graphics : prompt box

Status
Not open for further replies.

tyris

Programmer
Nov 2, 2000
311
FR
i'd like to do this, but i never used graphical components in java :

open a small box with a kind of textbox where the user enter a String and then click OK to perform a method. how is it doable ? what kind of methods should i use to make this msg box ? how is the valid button's action defined?

thanks

Best regards,
Elise, XML learning girl X-)
 
Hi,
U can use the JOptionPane class in javax.swing. It contains a static method called showInputDialog() which prompts the user to enter a string. on clicking the ok button the string is returned to the calling class.

The method signature is
public static String showInputDialog(Component parent,Object message)


Here is a sample piece of code to help u out

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

public class Show extends JFrame implements ActionListener
{
JPanel p;
JButton b;

// constructor
public Show()
{

// set the bounds
setBounds(50,50,200,100);
setVisible(true);
getContentPane().setLayout(null);

// make the button and panel
p = new JPanel();
p.setBounds(0,0,200,100);
p.setLayout(null);

b = new JButton("Clickme");
b.setBounds(0,0,80,40);
b.addActionListener(this);

// add the button to the panel
p.add(b);
// add the panel to the contentpane
getContentPane().add(p);

}

public void actionPerformed(ActionEvent ae)
{
// when the button is clicked
if(ae.getSource().equals(b))
{
// show the input dialog
String s = JOptionPane.showInputDialog(this,"Enter ur text");
// im just showing the string u entered // u can call any method also here..
System.out.println(s);
}
}

public static void main(String args[])
{
Show show = new Show();
}
}

The graphics is not very elegant but itll solve ur purpose..

hope this helps
Reddy316
 
i can't use javaw.swing because i can only use java 1.1
is there a simple possibility with for exemple java.awt ? Best regards,
Elise, XML learning girl X-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top