Trying to change add method to work as a button

Question!

I'm trying to make this code work as an add button instead of just pressing enter with a text field.

  ActionListener cmdLis = new CmdTextListener();

     cmdTextField.addActionListener(cmdLis);

  public void actionPerformed(ActionEvent evt)
             {
               String cmdStr = cmdTextField.getText();
               Scanner sc = new Scanner(cmdStr);
               String cmd = sc.next();

if (cmd.equals("add"))
               {
                   int value = sc.nextInt();
                   binTree.add(value);

                   if(view != null)
                       remove(view);
                   view = binTree.getView();
                   add(view);

                   pack();
                   validate();
                   cmdResultTextField.setText(" ");

               }

So i tried to do it like this but its doing nothing when i push the button its not even picking up the button push

  if (e.getSource() == addButton)
               {
                   //int value = Integer.parseInt(cmd);

                   int value = Integer.parseInt(cmdStr);
                   binTree.add(value);

                   if(view != null)
                       remove(view);
                   view = binTree.getView();
                   add(view);

                   pack();
                   validate();
                   cmdResultTextField.setText("Added "+ value);

               }


Answers

I guess you want to use simple GUI instead of console to input some values. Your code is incomplete - it is not clear what exactly you would like to achieve but I made some example for you which can be helpful.

public class Main extends JFrame {

    private JTextField textField;
    private JButton button;

    // private YourBinaryTree;

    public Main() {

        textField = new JTextField(5);

        button = new JButton("add to binary tree");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                String read = textField.getText();
                Integer number = Integer.parseInt(read);
                // binTree.add(number);
            }
        });

        button.setPreferredSize(new Dimension(200, 200));

        getContentPane().setLayout(new FlowLayout());
        getContentPane().add(textField);
        getContentPane().add(button);
        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        new Main();
    }
}


Depends on how accurate you want the speedtest to be, the larger the file the more accurate it is. But this can be a big annoyance/data hog for the user.

The big number appears because it's in bytes, to get Mb/s you need to do Your result / 1024^2.



Disable attribute only associates button and input filelds, the thing you want to do here is , event.preventDefault . It will work like disable, but it will depend upon your coding, if you want to show it disabled, you have to add add via jquery



This video can help you solving your question :)
By: admin