1

It take a full weekend with stressful in trying to make a function that show dialog and could return value after click ok (like JOptionPane.showMessageDialog(xxx,"xxx"))? could anyone help me about writing the code ?

Thanks in advance

Makara

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Makara
  • 233
  • 1
  • 8
  • 23
  • 1
    Why not using the same ? – jmj Feb 03 '11 at 13:03
  • 1
    See also http://stackoverflow.com/questions/3002787 – trashgod Feb 03 '11 at 14:12
  • Much easier to get help if you show (include in the question) what you have done so far. – user85421 Feb 03 '11 at 15:13
  • I have a JFrame that show data of database(Ms.Access 2003) in JTable and I have a JDialog that will show if you click sort button(JFrame i wrote in one file and JDialog wrote in other). What I really want is to add the JDialog with JFrame(mdi form), after click button ok of JDialog, JDialog will close and return some SQL String to sort my data in JTable. – Makara Feb 06 '11 at 04:05

3 Answers3

4

Use a JOptionPane.showInputDialog().

Other alternatives are to hand a GUI control(s) to the JOptionPane.showMessageDialog() and query the state of the control(s) once it is closed, or use a JDialog.

If you spend more than 15 minutes trying to get a JOptionPane to do exactly as required, it is a good sign that a JOptionPane is not the class for the job.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Yeah, I thought on that way too. But the only problem is I don't know to coding ?? Could you show me a simple code ? – Makara Feb 03 '11 at 13:27
  • 2
    We won't code for you. Get some tutorials and start learning. – mdrg Feb 03 '11 at 13:33
2

Here you have a trivial approach. It is a class and not a function. If you want to construct it with just have two parameters, make an additional constructor that has the two parameters you need.

Here you can take a look at the real McCoy...

public class MyOwnJDialog extends javax.swing.JDialog {
    private String theMessage;

    public MyOwnJDialog(java.awt.Frame parent, boolean modal, String theMessage) {
        super(parent, modal);
        initComponents();
        this.theMessage = theMessage;
        jLabel1.setText(theMessage);
        setVisible(true);
    }


    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        getContentPane().add(jLabel1, java.awt.BorderLayout.CENTER);

        jButton1.setText("OK");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton1);

        getContentPane().add(jPanel1, java.awt.BorderLayout.SOUTH);
        pack();
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        dispose();
    }

    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;

}
Costis Aivalis
  • 13,680
  • 3
  • 46
  • 47
1
  1. Design what classes you will need and what you want the GUI to look like
  2. Create a skeleton of the classes and methods and start filling in the details.
  3. Create a method that could put this together and return the output.

Some useful items:

jzd
  • 23,473
  • 9
  • 54
  • 76