0

how can i make the OK/X button impossible to click if user dont type number in the paginainicial and paginafinal textFields?

The Dialog

           final JTextField paginainicial = new JTextField();
           final JTextField paginafinal = new JTextField();
           Object[] message = {
                "Número da Primeira Folha: ", paginainicial,
                "Número Última Folha: ", paginafinal,
                "Pedido de: " + connection.getInetAddress().getHostName(),


            };
           Object[] options = {"OK"};

           int option = JOptionPane.showOptionDialog(null,
                          message,"Dados da Certidão",
                          JOptionPane.PLAIN_MESSAGE,
                          JOptionPane.QUESTION_MESSAGE,
                          null,
                          options,
                          options[0]);








      //  int option = JOptionPane.showConfirmDialog(null, message, "Dados da Certidão", JOptionPane.OK_CANCEL_OPTION);
        ocupado = "1";

        if (option == JOptionPane.OK_OPTION) {
            String primeirafolha = paginainicial.getText();
            String ultimafolha = paginafinal.getText();
            metodos metodosBD = new metodos();
            metodosBD.atualizafolha(primeirafolha, ultimafolha, caminhodoarquivo);
            System.out.println("Dados inseridos.");
            ocupado = "0";
            JOptionPane.showMessageDialog(null, "Certidão Finalizada");
        }else {
            metodos metodosBD = new metodos();
            metodosBD.removedadosBD(caminhodoarquivo);
            File certfile = new File(caminhodoarquivo);
            certfile.delete();
            System.out.println("Certidão Cancelada.");}

i tryed if text, but of course the if is after the OK press, so dont work :(

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
user2582318
  • 1,607
  • 5
  • 29
  • 47
  • You could take a look at [this example](http://stackoverflow.com/questions/14334931/disable-ok-button-on-joptionpane-dialog-until-user-gives-an-input/14335083#14335083) and [this example](http://stackoverflow.com/questions/14591089/joptionpane-passing-custom-buttons/14591165#14591165) which is an extension to the first – MadProgrammer Oct 09 '13 at 00:49

2 Answers2

3

I would create a custom-built modal JDialog for this. You could use DocumentListeners that are added to your text components to enable and disable the JButtons on the dialog.


Edit
You state:

hummm, in the way it is, can a create a KeyListenner to the textfields to check if the typed is number??? if its number, make the OK button visibile, but how can i access the button? its not a Jbutton

It is a JButton if you follow my suggestions above and make your own JDialog and don't use a JOptionPane. And no, you should almost never use a KeyListener with Swing applications. If you want to verify the input, use a JFormattedTextField or a DocumentFilter or an InputVerifier.


Edit 2
You state:

hummm, understand, i will read something about creating a modal Jdialog, thannk you :) this will work like "Jframe" isnt it? so i i will add components to it and will be possible to get Components events, property etc..

A JDialog is similar to a JFrame in that it's a top-level window, and like a JFrame you would load up a JPanel with all the components that it will display, then add the JPanel to the JDialog's contentPane, pack() it and set it visible, but it's different from a JFrame in that its constructors are different. You will need to pass in the parent window (here the main JFrame), a String title and a Dialog.ModalityType, likely APPLICATION_MODAL. Like a JOptionPane, once you set it to visible, all code from the calling code halts and waits for the dialog to no longer be visible prior to resuming.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • hummm, in the way it is, can a create a KeyListenner to the textfields to check if the typed is number??? if its number, make the OK button visibile, but how can i access the button? its not a Jbutton :| – user2582318 Oct 08 '13 at 21:08
  • hummm, understand, i will read something about creating a modal Jdialog, thannk you :) this will work like "Jframe" isnt it? so i i will add components to it and will be possible to get Components events, property etc.. – user2582318 Oct 08 '13 at 21:16
  • 1
    @user2582318: Please see Edit 2 to answer. – Hovercraft Full Of Eels Oct 08 '13 at 21:28
2

Read the section from the Swing tutorial on Preventing a Dialog From Closing for one approach.

camickr
  • 321,443
  • 19
  • 166
  • 288