Results 1 to 2 of 2
  1. #1
    adeeb is offline Member
    Join Date
    May 2008
    Posts
    29
    Rep Power
    0

    Default Regarding Text Field

    Hi,
    I have added two text fields and a button on a panel which is set with cardlayout now i want to store characters in two different strings.
    And when i enter the text in the fields and click the button i should move to second card.
    Only i need is getting the data from the text fields and comparing it.
    The rest i can handle.
    So please some one help

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

    Default

    For more on these kind of things see Trail: Creating a GUI with JFC/Swing.
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class GettingText implements ActionListener {
        JTextField hi = new JTextField(12);
        JTextField lo = new JTextField(12);
        JPanel panel;
    
        public void actionPerformed(ActionEvent e) {
            String hiText = hi.getText();
            String loText = lo.getText();
            boolean equal = hiText.equals(loText);
            int compare = hiText.compareTo(loText); // see String api
            System.out.printf("hiText = %s  loText = %s  " +
                              "equal = %b  compare = %d%n",
                               hiText, loText, equal, compare);
            CardLayout cards = (CardLayout)panel.getLayout();
            cards.next(panel);
            // or
    //        cards.show(panel, "blue");
        }
    
        private JPanel getContent() {
            CardLayout cards = new CardLayout();
            panel = new JPanel(cards);
            panel.add("data", getComponentPanel());
            panel.add("blue", getBluePanel());
            return panel;
        }
    
        private JPanel getComponentPanel() {
            JButton button = new JButton("jump");
            button.addActionListener(this);
            // Optionally, you can also add the ActionListener
            // to the two textFields which will generate an
            // ActionEvent when you press the enter/return key.
            hi.addActionListener(this);
            lo.addActionListener(this);
            JPanel panel = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.insets = new Insets(5,0,5,0);
            gbc.weighty = 1.0;
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            panel.add(hi, gbc);
            panel.add(lo, gbc);
            panel.add(button, gbc);
            return panel;
        }
    
        private JPanel getBluePanel() {
            JPanel panel = new JPanel();
            panel.setBackground(Color.blue);
            return panel;
        }
    
        public static void main(String[] args) {
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(new GettingText().getContent());
            f.setSize(400,400);
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        }
    }

Similar Threads

  1. get numeric value from a text field
    By Lehane_9 in forum New To Java
    Replies: 2
    Last Post: 06-14-2008, 03:19 AM
  2. Replies: 0
    Last Post: 05-26-2008, 04:26 PM
  3. Replies: 2
    Last Post: 04-28-2008, 10:26 AM
  4. Waste Space & Text Field
    By Gajesh Tripathi in forum AWT / Swing
    Replies: 2
    Last Post: 12-01-2007, 07:44 AM
  5. Final field question
    By derrickD in forum Advanced Java
    Replies: 1
    Last Post: 04-28-2007, 10:37 PM

Posting Permissions

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