Results 1 to 2 of 2
Thread: Regarding Text Field
- 06-05-2008, 07:35 PM #1
Member
- Join Date
- May 2008
- Posts
- 29
- Rep Power
- 0
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
- 06-05-2008, 11:01 PM #2
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
-
get numeric value from a text field
By Lehane_9 in forum New To JavaReplies: 2Last Post: 06-14-2008, 03:19 AM -
Unsupported Content-Type: text/html Supported ones are: [text/xml]
By luislopezco in forum Advanced JavaReplies: 0Last Post: 05-26-2008, 04:26 PM -
[SOLVED] How to check what type of value entered in text field
By Renegade85 in forum New To JavaReplies: 2Last Post: 04-28-2008, 10:26 AM -
Waste Space & Text Field
By Gajesh Tripathi in forum AWT / SwingReplies: 2Last Post: 12-01-2007, 07:44 AM -
Final field question
By derrickD in forum Advanced JavaReplies: 1Last Post: 04-28-2007, 10:37 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks