DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 9 of 9

Thread: setting tab index

  1. #1
    Join Date
    Jul 2007
    Posts
    7

    setting tab index

    Hello!

    In Java, what command that we can use to set tab index? For example, I have 4 JTextFields as inputs, and I only want to press TAB button in my keyboard in order to move it down.

    Thank you guys!

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    The press of a key will generate a KeyEvent. Create a listener which listens to the events and, if the TAB key is pressed, then switches focus to the "next" of your textfields.

    The Java Tutorial has a "How to Write a Key Listener" entry in the Swing trail:

    http://java.sun.com/docs/books/tutor...ylistener.html

  3. #3
    Join Date
    Jul 2007
    Posts
    7

    thanks..

    Well, I didnt know that it would be that hard just to set my tab index.

    A.

  4. #4
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    I would not characterize it as "hard" ... but that's a matter of perception, I guess. How else do you suggest we intercept the OS's hardware handler?

  5. #5
    Join Date
    Jul 2007
    Posts
    7
    I am very new to JAVA programming; thus, I dont have any suggestions. I actually could press TAB button and the cursor moved from one JTextField to another. But, the problem is that I have another JTextFields which are not "editable;" I want to skip these JTextFields.

    Also, I want to make ENTER key works, but it always gives me error. Same as disabling them. My instructor said that rather than putting error box whenever user put string inputs, it would be better if we use key listener to disable all the words (e.g. a, s, d, f, g, h, j, etc,) and only enabling the numbers (1,2,3,4,5,6,7,8,9,0).

    I used so many error box, I'm afraid that he would take points off from me. I have the lowest mark on yesterday's quiz; so, I have to get full points in the project.

    Sorry for the late reply, I was sleeping. I am in another side of the world, so we have different time zone. I would never ever take online classes anymore while I'm taking a summer off hahaha....

    THANK YOU

    A

  6. #6
    Join Date
    Jan 2007
    Location
    India
    Posts
    12

    Post Re : setting tab index

    Quote Originally Posted by adityatan
    I am very new to JAVA programming; thus, I dont have any suggestions. I actually could press TAB button and the cursor moved from one JTextField to another. But, the problem is that I have another JTextFields which are not "editable;" I want to skip these JTextFields.

    Also, I want to make ENTER key works, but it always gives me error. Same as disabling them. My instructor said that rather than putting error box whenever user put string inputs, it would be better if we use key listener to disable all the words (e.g. a, s, d, f, g, h, j, etc,) and only enabling the numbers (1,2,3,4,5,6,7,8,9,0).

    I used so many error box, I'm afraid that he would take points off from me. I have the lowest mark on yesterday's quiz; so, I have to get full points in the project.

    Sorry for the late reply, I was sleeping. I am in another side of the world, so we have different time zone. I would never ever take online classes anymore while I'm taking a summer off hahaha....

    THANK YOU

    A
    Hi Adityan

    You could implement the KeyListener for the purpose of skipping and the same could be used for detecting the key presses. So you could program to skip if the key pressed is not a number.

    For more support/clarity you can paste here the code wrapped by the code tags, for presenting the problem/exceptions much accurately.
    It could help increase the possibility of getting precise answers.
    With Regards
    Anand

  7. #7
    Join Date
    Jul 2007
    Posts
    7

    keyListener

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.*;
    import java.text.*;

    public class FutureAnnuity extends JFrame
    {
    // JButton to initiate calculations for Future Annuity Balance, Total of Payments, and Total Interest Earned
    private JButton calculateJButton;

    public FutureAnnuity()
    {
    createUserInterface();
    }

    public void createUserInterface()
    {

    Container contentPane = getContentPane();
    contentPane.setBackground( Color.WHITE );
    contentPane.setLayout( null );

    calculateJButton = new JButton();
    contentPane.add( calculateJButton );
    calculateJButton.addActionListener(

    new ActionListener() // annonymous inner class
    {
    // event handler called when calculateJButton is pressed
    public void actionPerformed( ActionEvent event )
    {
    calculateJButtonActionPerformed( event );
    }

    } // end annonymous inner class

    ); // end call to addActionListener

    setTitle( "Future Annuity - Project 1" ); // set title bar text
    setSize( 390, 398 ); // set window size
    setVisible( true ); // display window

    } // end method createUserInterface


    // calculate the Future Annuity Balance, Total of Payments, and Total Interest Earned
    private void calculateJButtonActionPerformed( ActionEvent event )
    {
    annuity();

    } // end method calculateJButtonActionPerformed

    public static void main ( String[] args)
    {
    FutureAnnuity application = new FutureAnnuity();
    application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

    } // end method main

    } // end class FutureAnnuity

    annuity() is my calculation. I will provide it in the next page.

    In annuity(), you can see that there are a lot of try and catch, since i want to validate
    input values. if you can tell me where to put the key listener for disabling non-number pads

    I want the code to process calculation when we pressed ENTER when calculateJButton is focus.

    I skipped so many procedures here. So dont worry about them.

    Thank you!

    Aditya

  8. #8
    Join Date
    Jul 2007
    Posts
    7

    key listener

    Thanks ananth

    I really want to give the code to you; however, this is an open forum, I can't give it right away, because this is a site recomended by my teacher. My code could be copied by my other classmates who may visited the site. Please contact me at rc_kola2000@yahoo

  9. #9
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    Do you want to validate as you go? That seems wasteful. Wouldn't it be more meaningful to wait to validate completed input? That is what is nice with Pattern and Matcher - you take the input stream, if it matches the requirements of the pattern you submit and process (convert to float or whatever you're going to do), if it does not match you invite the user to try again. In this manner you are checking programmatically, with a "while" or "if" loop rather than counting on try/catch to do this for you.

    As to your navigation keypresses - there is no way around it. You'll just have to establish KeyListeners for your containers to respond to enter or tab or whatever other key you want to provide functionality for [that's one of the great parts of programming: YOU have the power to provide the functionality YOU design rather than being dependent on what others might expose to you ... but the responsibility is that YOU have to provide it].

Similar Threads

  1. tab index problem
    By Matrix.net in forum ASP.NET
    Replies: 2
    Last Post: 05-01-2007, 01:37 PM
  2. Replies: 3
    Last Post: 12-18-2006, 01:24 AM
  3. Setting toolbox tab for component
    By eldiener in forum .NET
    Replies: 2
    Last Post: 08-24-2006, 10:07 AM
  4. Replies: 2
    Last Post: 07-19-2005, 06:09 AM
  5. Getting the Index of a Request.Form key
    By THarlow in forum Web
    Replies: 0
    Last Post: 02-16-2005, 01:30 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center