Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-17-2008, 05:26 AM
Member
 
Join Date: Feb 2008
Posts: 3
birdofprey is on a distinguished road
implementing a button instead.
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?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 02-17-2008, 07:40 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,266
hardwired is on a distinguished road
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.
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); } };
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 02-17-2008, 10:09 PM
Member
 
Join Date: Feb 2008
Posts: 3
birdofprey is on a distinguished road
for the first part.

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?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 02-18-2008, 05:21 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,266
hardwired is on a distinguished road
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); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Implementing an interface bugger Advanced Java 1 01-09-2008 03:35 PM
Implementing more than one interfaces eva New To Java 2 12-24-2007 11:50 AM
Implementing and Extending together eva New To Java 2 12-24-2007 11:49 AM
Implementing Interface mew New To Java 2 12-06-2007 11:09 PM
Implementing more than one Interfaces JavaForums Java Blogs 0 12-06-2007 08:42 PM


All times are GMT +3. The time now is 03:16 PM.


VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org