Results 1 to 2 of 2
  1. #1
    thelegitxp is offline Member
    Join Date
    Mar 2014
    Posts
    1
    Rep Power
    0

    Default How to print something from a JButton to a textfield?

    I am having difficulties making a JButton post to a textfield. What I'm talking about clicking a JButton, like the JButton says 1 and you click it, it will print a 1 to the textfield. My code:
    package main;

    import java.awt.BorderLayout;

    public class Password extends JFrame implements ActionListener {

    /**
    *
    */
    private static final long serialVersionUID = 4079832897376331464L;
    private JPanel contentPane;
    private JTextField textField;

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

    try {
    UIManager.setLookAndFeel(UIManager.getSystemLookAn dFeelClassName());
    } catch (Exception e) {
    e.printStackTrace();
    System.out.println("Sorry, no native look for you :(");
    }

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

    /**
    * Create the frame.
    */
    public Password() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 750, 520);

    JMenuBar menuBar = new JMenuBar();
    setJMenuBar(menuBar);

    JMenu mnMenu = new JMenu("Menu");
    menuBar.add(mnMenu);

    JMenuItem mntmAddIds = new JMenuItem("Add IDs");
    mnMenu.add(mntmAddIds);

    JMenuItem mntmEditIds = new JMenuItem("Edit IDs");
    mnMenu.add(mntmEditIds);

    JMenuItem mntmDeleteIds = new JMenuItem("Delete IDs");
    mnMenu.add(mntmDeleteIds);

    JMenuItem mntmExit = new JMenuItem("Exit");
    mnMenu.add(mntmExit);

    JMenu mnHelp = new JMenu("Help");
    menuBar.add(mnHelp);

    JMenuItem mntmFaq = new JMenuItem("FAQ");
    mnHelp.add(mntmFaq);

    JMenuItem mntmCredits = new JMenuItem("Credits");
    mnHelp.add(mntmCredits);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    textField = new JTextField();
    textField.setBackground(Color.WHITE);
    textField.setFont(new Font("Tahoma", Font.PLAIN, 22));
    textField.setBounds(130, 85, 400, 55);
    contentPane.add(textField);
    textField.setColumns(10);

    JButton btnNewButton = new JButton("Enter");
    btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 18));
    btnNewButton.setBounds(575, 85, 80, 55);
    contentPane.add(btnNewButton);

    JButton btnNewButton_1 = new JButton("1");
    btnNewButton_1.setFont(new Font("Tahoma", Font.PLAIN, 20));
    btnNewButton_1.setBounds(135, 190, 75, 50);
    contentPane.add(btnNewButton_1);

    JButton button = new JButton("2");
    button.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
    }
    });
    button.setFont(new Font("Tahoma", Font.PLAIN, 20));
    button.setBounds(290, 190, 75, 50);
    contentPane.add(button);

    JButton button_1 = new JButton("3");
    button_1.setFont(new Font("Tahoma", Font.PLAIN, 20));
    button_1.setBounds(445, 190, 75, 50);
    contentPane.add(button_1);

    JButton button_2 = new JButton("4");
    button_2.setFont(new Font("Tahoma", Font.PLAIN, 20));
    button_2.setBounds(135, 280, 75, 50);
    contentPane.add(button_2);

    JButton button_3 = new JButton("5");
    button_3.setFont(new Font("Tahoma", Font.PLAIN, 20));
    button_3.setBounds(290, 280, 75, 50);
    contentPane.add(button_3);

    JButton button_4 = new JButton("7");
    button_4.setFont(new Font("Tahoma", Font.PLAIN, 20));
    button_4.setBounds(135, 370, 75, 50);
    contentPane.add(button_4);

    JButton button_5 = new JButton("8");
    button_5.setFont(new Font("Tahoma", Font.PLAIN, 20));
    button_5.setBounds(290, 370, 75, 50);
    contentPane.add(button_5);

    JButton button_6 = new JButton("9");
    button_6.setFont(new Font("Tahoma", Font.PLAIN, 20));
    button_6.setBounds(445, 370, 75, 50);
    contentPane.add(button_6);

    JButton button_7 = new JButton("6");
    button_7.setFont(new Font("Tahoma", Font.PLAIN, 20));
    button_7.setBounds(445, 280, 75, 50);
    contentPane.add(button_7);

    Label label = new Label("Please Enter Password");
    label.setAlignment(Label.CENTER);
    label.setFont(new Font("Tahoma", Font.BOLD, 24));
    label.setBounds(130, 31, 400, 55);
    contentPane.add(label);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub

    }
    }

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,618
    Rep Power
    5

    Default Re: How to print something from a JButton to a textfield?

    For each of you JButtons, set the actionListener to this. Then in your action performed method, access the textField. You can use getText and setText methods to add the appropriate number (from the button text name).

    Also, here are some recommendations based on good programming practices.

    1. Don't use absolute positioning. Use a layout manager.
    2. You don't need to save rerferences to the buttons once they are created and added to the GUI, so just create them in a loop.
    3. Don't extend JFrame. Extend JPanel and add the buttons to the panel and add the panel to the frame.

    You can also add the menus in a loop referencing an array of Strings for menu names.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. textField and Jbutton Question
    By Luwine in forum New To Java
    Replies: 1
    Last Post: 06-28-2013, 09:45 PM
  2. Replies: 1
    Last Post: 01-15-2013, 06:44 PM
  3. Javax Print Attribute for Selection Print Range
    By rsawatzky in forum AWT / Swing
    Replies: 0
    Last Post: 04-26-2012, 12:14 AM
  4. ActionListener for JButton after changing Button to JButton
    By ravi.joshi53 in forum Java Applets
    Replies: 2
    Last Post: 10-07-2011, 07:35 AM
  5. Print the text file and print preview them
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:04 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •