0

I need some help about Java GUI. I want to add 10 numbers to memory from JTextField. And when it's done JButton must gonna be disable and program must show me a message dialog. How can I do this?

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Uygulama extends JFrame {
    private JPanel contentPane;
    private JTextField textField;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Uygulama frame = new Uygulama();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    public Uygulama() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 233, 140);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        textField = new JTextField();
        textField.setBounds(48, 13, 116, 22);
        contentPane.add(textField);
        textField.setColumns(10);
        JButton btnHesapla = new JButton("HESAPLA");
        btnHesapla.addActionListener(new ActionListener() {int counter = 0; 
        public void actionPerformed(ActionEvent arg0) {
            while(counter < 9) {
                counter++;
                if(counter == 10) {
                    buton.setEnabled(false);}
            }
        });
        btnHesapla.setBounds(58, 48, 97, 25);
        contentPane.add(btnHesapla);
    }
}

1 Answers1

1

Add an ActionListener to your JButton if using Swing, and inside that, you'll increment a global variable by one. Then you ask if the variable is == 10 and do yourButton.setEnabled(false) or yourButton.setEnabled(counter < 10) if you're not using an if. You already have your listener set up, so it's only a matter of adding a variable you'll increment inside it and a call to setEnabled of your button.

Here's a working example with an ArrayList used to store the numbers. No need to use a global count variable here because the size of the ArrayList already tells us how many numbers we have stored :-)

public class NumbersApplication extends JFrame {

    private JPanel contentPane;
    private JTextField textField;
    private List<Integer> numbers;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    NumbersApplication frame = new NumbersApplication();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public NumbersApplication() {
        numbers = new ArrayList<>();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 233, 140);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        textField = new JTextField();
        textField.setBounds(48, 13, 116, 22);
        contentPane.add(textField);
        textField.setColumns(10);
        JButton btnHesapla = new JButton("HESAPLA");
        btnHesapla.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int number = Integer.parseInt(textField.getText());
                numbers.add(number);
                btnHesapla.setEnabled(numbers.size() < 10);
                System.out.println(number + " has been added to memory.");
                if (numbers.size() == 10) {
                    System.out.println("Your numbers are: " + numbers);
                }
            }
        });
        btnHesapla.setBounds(58, 48, 97, 25);
        contentPane.add(btnHesapla);
    }
}
Morgan
  • 907
  • 1
  • 13
  • 35
  • I get the enable/disable stuff. But how can I save this 10 number at the memory without showing the user? – Uğur Hıdır May 11 '18 at 12:47
  • Oh, sorry, you mean save the 10 numbers the user typed. For that you need an `ArrayList`. Initialise it as a global variable like `List numbers = new ArrayList<>()` and inside your listener add the number from your field to it. – Morgan May 11 '18 at 12:53
  • I can't set the button disable. I edited the code could you check it out? – Uğur Hıdır May 11 '18 at 15:30
  • Done. I have added a working example. I highly recommend that you take the time to re-read some of the basics first before delving into Swing though :-) – Morgan May 11 '18 at 18:11