2010-04-12 14 views
5

extendí JDialog para crear un diálogo personalizado donde el usuario deberá rellenar algunos campos: dialog http://www.freeimagehosting.net/uploads/3d4c15ed9a.jpgRecuperar la entrada introducida en un JDialog

¿Cómo debo recuperar los datos introducidos?

Se me ocurrió una solución que funciona. Imita JOptionPane pero la forma en que hago se ve feo a mí, porque de los campos estáticos involucrados ... Aquí es más o menos mi código:

public class FObjectDialog extends JDialog implements ActionListener { 
    private static String name; 
    private static String text; 
    private JTextField fName; 
    private JTextArea fText; 
    private JButton bAdd; 
    private JButton bCancel; 

    private FObjectDialog(Frame parentFrame) { 
     super(parentFrame,"Add an object",true); 
     // build the whole dialog 
     buildNewObjectDialog(); 
     setVisible(true); 
    } 

    @Override 
    public void actionPerformed(ActionEvent ae) { 
     if(ae.getSource()==bAdd){ 
      name=fName.getText(); 
      text=fText.getText(); 
     } 
     else { 
      name=null; 
      text=null; 
     } 
     setVisible(false); 
     dispose(); 
    } 

    public static String[] showCreateDialog(Frame parentFrame){ 
     new FObjectDialog(parentFrame); 
     String[] res={name,text}; 
     if((name==null)||(text==null)) 
      res=null; 
     return res; 
    } 
} 

Como ya he dicho, que funciona correctamente, pero supongo que eso podría plantear graves problemas de concurrencia ...

¿existe una forma más limpia de hacer eso? ¿Cómo se hace en JOptionPane?

+0

¿Qué aspecto y sensación utilizas? –

+1

@Martijn Courteaux: Nimbus (http://stackoverflow.com/questions/2616448/im-tired-of-jbuttons-how-can-i-make-a-nicer-gui-in-java) ;-) –

Respuesta

10

Si hago esto, yo siempre funciona así:

FObjectDialog fod = new FObjectDialog(this); 
fod.setLocationRelativeTo(this); // A model doesn't set its location automatically relative to its parent 
fod.setVisible(true); 
// Now this code doesn't continue until the dialog is closed again. 
// So the next code will be executed when it is closed and the data is filled in. 
String name = fod.getName(); 
String text = fod.getText(); 
// getName() and getText() are just two simple getters (you still have to make) for the two fields their content 
// So return textField.getText(); 

Espero que esto ayude!
PD: ¡Su programa se ve genial!

+0

Ooooooooh por supuesto ! No sé por qué me quedé atrapado en esos campos estáticos ... En mi opinión, el objeto fue destruido después de deshacerse de la ventana, pero en realidad no lo es. Gracias ! –

1

Si tiene la intención de mostrar varios cuadros de diálogo al mismo tiempo, entonces tiene problemas de concurrencia, de lo contrario. Sin embargo, deshacerse de todo lo estático haría el diseño más limpio, más seguro y más fácil de probar. Simplemente controle la creación y la presentación del diálogo desde el código de llamada y no necesita ningún elemento estático.

Cuestiones relacionadas