Results 1 to 4 of 4
  1. #1
    birdofprey is offline Member
    Join Date
    Feb 2008
    Posts
    3
    Rep Power
    0

    Default implementing a button instead.

    Java Code:
    private JTextField Entry() {
    		Entry = new JTextField();
    		Entry.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				middleInitialEntry.setText(TextEntered);
    					System.out.println(TextEntered);
    			}
    		});
    		return Entry;
    	}
    With a code like this, how would one implement a button to set the data and output it out? instead of the default hitting enter?

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    JTextField entry;
    JTextField middleInitialEntry;
    
    private JTextField getEntry() {
        entry = new JTextField();
    }
    
    private JButton getButton() {
        JButton button = new JButton("make it happen");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String text = entry.getText();
                middleInitialEntry.setText(text);
                System.out.println(text);
            }
        });
        return button;
    }
    Or you could do both let the action be generated by both the button and the textField.
    Java Code:
    private JTextField getEntry() {
        entry = new JTextField();
        entry.addActionListener(al);
    }
    
    private JButton getButton() {
        JButton button = new JButton("make it happen");
        button.addActionListener(al);
        return button;
    }
    
    private ActionListener al = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String text = entry.getText();
            middleInitialEntry.setText(text);
            System.out.println(text);
        }
    };

  3. #3
    birdofprey is offline Member
    Join Date
    Feb 2008
    Posts
    3
    Rep Power
    0

    Default

    for the first part.

    Java Code:
    private JTextField getEntry() {
             entry = new JTextField();
    }
    This wouldn't work, in my case, because JTextField isn't returning "JTextField"

    Because the way my code is written where all the JTextField, JButton .. etc.. are added like this "JContentpane.add(getButton);".

    What do you think?

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class OneOption implements ActionListener {
        JTextField entryField;
        JLabel label;
    
        public void actionPerformed(ActionEvent e) {
            String text = entryField.getText();
            label.setText(text);
        }
    
        private JPanel getContent() {
            JPanel panel = new JPanel(new BorderLayout());
            panel.add(getLabel(), "North");
            panel.add(getEntry());
            panel.add(getButton(), "South");
            return panel;
        }
    
        private JLabel getLabel() {
            label = new JLabel();
            label.setPreferredSize(new Dimension(100,30));
            label.setHorizontalAlignment(JLabel.CENTER);
            return label;
        }
    
        private JPanel getEntry() {
            entryField = new JTextField(12);
            entryField.addActionListener(this);
            JPanel panel = new JPanel(new GridBagLayout());
            panel.add(entryField, new GridBagConstraints());
            return panel;
        }
    
        private JPanel getButton() {
            JButton button = new JButton("make it happen");
            button.addActionListener(this);
            JPanel panel = new JPanel();
            panel.add(button);
            return panel;
        }
    
        public static void main(String[] args) {
            JFrame f = new  JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(new OneOption().getContent());
            f.setSize(400,200);
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        }
    }

Similar Threads

  1. Implementing Interface
    By mew in forum New To Java
    Replies: 4
    Last Post: 02-16-2010, 03:33 PM
  2. Implementing an interface
    By bugger in forum Advanced Java
    Replies: 1
    Last Post: 01-09-2008, 01:35 PM
  3. Implementing more than one interfaces
    By eva in forum New To Java
    Replies: 2
    Last Post: 12-24-2007, 09:50 AM
  4. Implementing and Extending together
    By eva in forum New To Java
    Replies: 2
    Last Post: 12-24-2007, 09:49 AM

Posting Permissions

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