Results 1 to 3 of 3

Thread: Dialog Box

  1. #1
    Join Date
    Jul 2007
    Posts
    13
    Rep Power
    0

    Default 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.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default Dialog example

    Java 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);
            }
        };
    }

  3. #3
    Join Date
    Jul 2007
    Posts
    13
    Rep Power
    0

Similar Threads

  1. Run As dialog and Plugin Help
    By menelaosbgr in forum Eclipse
    Replies: 0
    Last Post: 03-25-2008, 03:21 AM
  2. Example - File Dialog
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-01-2008, 10:11 PM
  3. Example of SWT Dialog
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-09-2008, 12:01 PM
  4. My Preference Dialog
    By schuetzejanett in forum Eclipse
    Replies: 3
    Last Post: 08-10-2007, 10:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •