Как вытащить данные из JFormattedTextField?

Есть диалог с двумя JComboBox и JFormattedTextField. Как вытащить то, что я ввожу в JFormattedTextField? Разные варианты перепробовал, но никак не выходит.
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);
        }
    }
}
  • Вопрос задан
  • 181 просмотр
Решения вопроса 1
timych
@timych
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) {
			}
		});


Примерно так :)
PS: И типизируйте JComboBox.
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы
Bell Integrator Хабаровск
До 400 000 ₽
Bell Integrator Ульяновск
До 400 000 ₽
Bell Integrator Ижевск
До 400 000 ₽
07 мая 2024, в 11:27
1500 руб./в час
07 мая 2024, в 11:18
50000 руб./за проект
07 мая 2024, в 11:02
300 руб./за проект