A
A
Andrey Frolov2015-03-23 18:29:39
Java
Andrey Frolov, 2015-03-23 18:29:39

How to pull data from JFormattedTextField?

There is a dialog with two JComboBox and JFormattedTextField. How to pull what I enter into JFormattedTextField? I've tried different options, but it doesn't work.

public class FindProvider extends JDialog implements PropertyChangeListener {
    Vector<String> city = new Vector<String>();
    JPanel findProvider;
    JComboBox city_box;
    JComboBox fuel_box;
    JFormattedTextField price;
    JButton buttonOK;
    String result_size;
    String result_fuel;
    String result_city;

    public String getResult_size() {
        return result_size;
    }
    
    public String getResult_city() {
        return result_city;
    }

    public String getResult_fuel() {
        return result_fuel;
    }

    public FindProvider() {
        findProvider = new JPanel();
        setContentPane(findProvider);
        setModal(true);
        getRootPane().setDefaultButton(buttonOK);

        buttonOK = new JButton();
        buttonOK.setText("OK");
        buttonOK.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onOK();
            }
        });


        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onCancel();
            }
        });


        MainGUI.select("SELECT city_name FROM city", city);
        city_box = new JComboBox(city);
        city_box.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JComboBox box = (JComboBox)e.getSource();
                result_city = (String) city_box.getSelectedItem();
            }
        });

        String[] fuels = {"Нефть", "Газ горючий", "Газоконденсат"};
        fuel_box = new JComboBox(fuels);
        fuel_box.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JComboBox box = (JComboBox)e.getSource();
                result_fuel = (String) fuel_box.getSelectedItem();
            }
        });

        MaskFormatter  formatter = null;
        try{
            formatter = new MaskFormatter("######");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        price = new JFormattedTextField(formatter);
        price.addPropertyChangeListener(this);

        Box box = Box.createVerticalBox();
        box.add(fuel_box);
        box.add(Box.createVerticalStrut(10));
        box.add(city_box);
        box.add(Box.createVerticalStrut(10));
        box.add(new JLabel("Объем:"));
        box.add(price);
        box.add(buttonOK);

        findProvider.add(box);
    }
    private void onOK() {
// add your code here

        dispose();
    }

    private void onCancel() {
// add your code here if necessary
        dispose();
    }

    public void propertyChange(PropertyChangeEvent e) {
        Object source = e.getSource();
        if (source == price) {
            result_size = price.getText();
            System.out.println(result_size);
        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Timur, 2015-03-23
@inham

price.addKeyListener(new KeyListener() {

      @Override
      public void keyTyped(KeyEvent e) {
      }

      @Override
      public void keyReleased(KeyEvent e) {
        System.out.println("Price text: " + price.getText());
      }

      @Override
      public void keyPressed(KeyEvent e) {
      }
    });

Something like this :)
PS: And type JComboBox.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question