Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-29-2007, 11:55 PM
Member
 
Join Date: Jul 2007
Posts: 13
uncopywritable is on a distinguished road
Dialog Box
Is there some more experienced Java programmer out there who could help me with this?

I want to make a simple dialog box that creates a text field and 3 buttons: "Cancel", "Clear" and "Ok". I want the "Cancel" button to exit the program, the "Clear" button to reset the string in the text field, and I want the "OK" button to check if there is any text in the text field and only close the dialog box and proceed to the actual program if this is true.

I have absolutely no idea how to do this and I can't find any examples that actually get strings from input dialog boxes.

I haven't been doing this very long! Any help I could get would be very much appreciated.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-30-2007, 01:05 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
Dialog example
Code:
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class DialogExample implements ActionListener { JDialog dialog; JTextField textField; public void actionPerformed(ActionEvent e) { String ac = e.getActionCommand(); if(ac.equals("CANCEL")) System.exit(0); if(ac.equals("CLEAR")) textField.setText(""); if(ac.equals("OK")) { String text = textField.getText(); if(!text.equals("")) { dialog.dispose(); JOptionPane.showMessageDialog(null, "start your main app"); // For now we need this line. System.exit(0); } } } private JPanel getContent() { textField = new JTextField(16); String[] ids = { "Cancel", "Clear", "OK" }; JPanel panel = new JPanel(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.insets = new Insets(2,2,2,2); gbc.weightx = 1.0; gbc.weighty = 1.0; gbc.gridwidth = 3; panel.add(textField, gbc); gbc.gridy = 1; gbc.gridwidth = 1; for(int j = 0; j < ids.length; j++) { JButton button = new JButton(ids[j]); button.setActionCommand(ids[j].toUpperCase()); button.addActionListener(this); panel.add(button, gbc); } return panel; } private void launchDialog() { dialog = new JDialog(new Frame(), "dialog", false); dialog.addWindowListener(closer); dialog.getContentPane().add(getContent()); dialog.setSize(300,200); dialog.setLocation(200,200); dialog.setVisible(true); } public static void main(String[] args) { DialogExample example = new DialogExample(); example.launchDialog(); } private WindowListener closer = new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }; }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-30-2007, 01:42 PM
Member
 
Join Date: Jul 2007
Posts: 13
uncopywritable is on a distinguished road
Thankyou!
Thanks hardwired, now I understand!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Run As dialog and Plugin Help menelaosbgr Eclipse 0 03-25-2008 04:21 AM
Example - File Dialog Java Tip Java Tips 0 03-01-2008 11:11 PM
Example of SWT Dialog Java Tip Java Tips 0 01-09-2008 01:01 PM
SWT Dialog JavaForums Java Blogs 0 12-31-2007 05:53 PM
My Preference Dialog schuetzejanett Eclipse 3 08-10-2007 11:48 AM


All times are GMT +3. The time now is 05:57 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org