-2

I want to make my JButton called enter function as enter for my JTextField. So like if I wear to press on the enter button it would put what I wrote down in JTextField in the the JLabel.

Here's my code:

package Main_Config;

import java.awt.*;

import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;

public class SET_UP extends JFrame {

    private JPanel contentPane;
    private JTextField textField;
    private JLabel consol;
    public static Dimension size = new Dimension(800, 700);


    /**
     * Launch the application.
     */
    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    SET_UP frame = new SET_UP();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public SET_UP() {

        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(370, 70, 0, 0);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        setSize(size);
        setLocationRelativeTo(null);

        textField = new JTextField();
        textField.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String input = textField.getText(); 
                consol.setText(input);
            }
        });
        textField.setBounds(10, 452, 243, 20);
        contentPane.add(textField);
        textField.setColumns(10);

        JButton enter = new JButton("Enter");
        enter.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {}
        });

        enter.setBounds(253, 452, 89, 20);
        contentPane.add(enter);

        JLabel consol = new JLabel("");
        consol.setBounds(0, 483, 335, 189);
        contentPane.add(consol);

        JButton btnNewButton = new JButton("New button");
        btnNewButton.setBounds(352, 451, 200, 23);
        contentPane.add(btnNewButton);

        JButton btnNewButton_1 = new JButton("New button");
        btnNewButton_1.setBounds(584, 451, 200, 23);
        contentPane.add(btnNewButton_1);

        JButton btnNewButton_2 = new JButton("New button");
        btnNewButton_2.setBounds(0, 0, 89, 23);
        contentPane.add(btnNewButton_2);
    }
}
Unihedron
  • 10,902
  • 13
  • 62
  • 72
user3024659
  • 17
  • 1
  • 2
  • Please check out the tutorials on [how to use JButtons](http://docs.oracle.com/javase/tutorial/uiswing/components/button.html) (link now provided). It's all to be found there for your learning pleasure. Then if still stuck, please tell us all about exactly what confuses you, and how your code doesn't work or what errors or exceptions it shows. – Hovercraft Full Of Eels Nov 23 '13 at 23:10
  • Finally, your code is without any indentations making it all left justified and almost impossible to read, understand and debug. Please re-format your posted code by giving it proper indentations, usually 3 spaces per block, and making sure that all code on the same block is on the same indentation level. Your cooperation in this would be greatly appreciated and will likely improve your chances of getting a decent and prompt answer. – Hovercraft Full Of Eels Nov 23 '13 at 23:13
  • Sorry for my format its just that it wont let me summit with out doing this and thank you for your answer I will make chore to read the java tutorials.But I stile need help so any and all answer are good – user3024659 Nov 23 '13 at 23:30
  • Seriously though. All you've posted is GUI framework code, and a button with an empty action listener. You could at least show us your attempt to solve this. Again, the tutorials are good resources that will likely show you all you need to know for that first attempt. Check them out. Try it. Try anything. Voting to close. – Hovercraft Full Of Eels Nov 23 '13 at 23:36
  • Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). But put GUIs aside for the moment while you learn the basics of the language. – Andrew Thompson Nov 23 '13 at 23:54

1 Answers1

1

I want to make my JButton called enter function as enter for my JTextField.

You added an ActionListener to the text field, but now you need to share the ActionListener with the text field and the button.

So your code would be something like:

ActionListener al = new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        String input = textField.getText(); 
        consol.setText(input);
    }
};

...

textField.addActionListener(al);
enter.addActionListener(al);

Now if focus is on the test field and you use the Enter key the ActionListener is invoked. Or if you click on the "Enter" button, the same ActionListener is invoked.

Unihedron
  • 10,902
  • 13
  • 62
  • 72
camickr
  • 321,443
  • 19
  • 166
  • 288