Array of JTextFields

If I have an array of JTextFields e.g. tf = new JTextField[i] for 1<i<46, how can I get the cursor to automatically tab to the next field once a string of numbers has been inputted to one of them. I'm trying to design a Scan Frame for barcodes and I want to be able to scan the first one and automatically be able to scan the next and so on
thanks
I can show you code if you need it

You can set actionListener to forcus on the JTextField[0] and make a function to do the action and then use grabforcus to move cursor to the next JTextField. For example
JTextField exam [] = new JTextField[3];
the actionperform function is here
if(e.source == exam[0])
     dosomething;
     exam[1].grabforcus;//you may check the spelling for sure
and so on for the next array index.
Hope solving your problem.

Similar Messages

  • Array of JPanel and Array of JTextField?

    Hi,
    I try to create array of jpanel and inizialize it in "for" , but when run it give me this error message:
    "Exception in thread "main" java.lang.NullPointerException"
    at the row: "panInsCantante.add(panInsNCC);"
    why?
    (ps. i tried in another code to create array of jtextfield and it give me same error message..)
    here part of code :
          JPanel [] panInsCantante = new JPanel[3];
          for(int i=0;i<=N;i++){
               JPanel panInsNCC= new JPanel();
               panInsNCC.setLayout( new GridLayout(3,1) );
               panInsNCC.add(campoTesto1);
               panInsNCC.add(campoTesto2);
               panInsNCC.add(campoTesto3);
               //panInsCantante[i] = new JPanel();
               //panInsCantante.setLayout( new GridLayout(1,1) );
         panInsCantante[i].add(panInsNCC);

    a question (theoric...)A VERY important question I may add.
    what's the difference between ..
    this instance :
    JPanel [] panInsCantante = new
    JPanel[3];This declares and initializes the array itself, nothing more. No JPanels have been initialized as yet, just the array. It's as if you have now built the shelves to hold the books, but you have no books up there yet...
    and this instance of panInsCantante? :
    panInsCantante[i] = new JPanel();and this initializes each JPanel in the array as you loop through the array. .... and now you have filled the shelves with the books and can use the books.
    You will need them both.
    In all likelihood, the reason for your confusion is that all of your previous arrays were arrays of primative types: int, double, float, and char. In this situation, you don't need to go through the step of initializing the variable.
    But now you are faced for the first time with an array of reference type, an array of Objects. This is a different situation and requires the steps that we have gone through.
    Message was edited by:
    petes1234

  • Add mouselisteners to an array of JTextField objects

    Hi. I am writing a program to emulate a calendar and I though I would do it for each day I would have a JTextField hold the contents of the field. A user could set the event for that time by clicking on the corresponding textfield. I'm still working on saving to file and whatnot, but my question is how can I add motionlisteners to an array of JTextFields?
    Here is what I've written so far:
    for(JTextField c: event)
                   c.addMouseListener(new MouseAdapter()
                   public void mousePressed(MouseEvent ev)
                        String in = JOptionPane.showInputDialog("Please Enter event for this time");
                        c.setText(in);
              }I run into I immediate error regarding setting the contents of c within an innerclass. I know it wouldn't be an issue if c was defined as a private variable, but I don't know if that is permitted for the for each loop. Also I get another error I'm not sure of, something about this can only work if the source level is 5.0 which I don't know what that means.
    also outside of this method I've set the object event:
    private JTextField[] event = new JTextField[168];any input would be appreciated.

    When you have an inner class, you can only access variables that are declared final. So, this should work:
    for (int i=0; i <168; i++)
              final JTextField tempEvent = event;
              event[i].addMouseListener(new MouseAdapter()
                   public void mousePressed(MouseEvent ev)
                        String in = JOptionPane.showInputDialog("Please Enter event for this time");
                        tempEvent.setText(in);

  • Initialise a array of JTextFields

    I need to access an array of JTextfeilds publically because I need to reference it within a different method. But the problem is that I can only initialise it later because it depends on user input.
    How can I set it up publically but initialise the size later.
    for example
    constructor:
    public JTextField text[] = null;
    public main method()
    initialise(int R)
    public void initialise(int p)
    text[] = new JTextField[p];
    }

    public class BrokenDesign
      public JTextField[] fields;
      public BrokenDesign()
        fields = new JTextField[0];
      public void onUserInputInitialiseXJTextFields(int x)
        fields = new JTextField[x];
        for (int k = 0; k < x; k++) {
          fields[k] = new JTextField();
    }

  • Custom Document + Array of JTextFields

    Hi,
    I read through the tutorial and managed to get a Custom Document going for my JTextFields (of which i am using quite a few =))
    To cut my code down and aid getting data back from the textfields, I changed them from a page of declarations to a couple of grouped arrays.
    However, now that I have done that, whenever i change the text in one of the textfields, the text in all the textfields in the array change...
    I'm assuming it's something to do with the super.insertString(offs, str, a); that is being called from the Custom Document, but I'm not sure how to fix it up...
    Any help would be most appreciated.
    Cheers
    Andy

    whenever i change the text in one of the textfields, the text in all the textfields in the array change...This means that each text field is using the same document. It's hard to help since you don't post any code. Your code should look something like:
    textField1 = new JTextField()
    textField1.setDocument( new CustomDocument() );
    textField2 = new JTextField()
    textField2.setDocument( new CustomDocument() );

  • Arrays with JTextField

    JTextField[] textfields = { JTextField inTroop  = new JTextField( 7 ), JTextField inTank  = new JTextField( 7 ) }; im getting an identifier expected error cxan someone point out what i am doing wrong please?

    is there anyway that i could put on a identifier ? sort of like
    JTextField[] textfields = {intank =  new JTextField( 7 ), introop = new JTextField( 7 ) };

  • Listening to MouseEvents in an array of JTextFields

    // BlockWerteFeld is a JTextFiled
    private BlockWerteFeld wertedArray[]= {
            null,null,null,null,null,null,
            null,null,null,null,null,null,
            null,null,null,null,null,null,
            null,null,null,null,null,null
    for ( int i = 0 ; i <= GESAMT_SUMME ; i++ ){
                werteArray = new BlockWerteFeld(
    "tfWert" + i,
    i * Jobs.BLOCK_HOEHE,
    Jobs.BLOCK_LINKS+ Jobs.BLOCK_BREIT
    // some more settings
    werteArray [i].addMouseListener(eventHandler);
    panelBlock.add( werteArray[i]);
    private EventHandler eventHandler = new EventHandler();
    // inner Class
    class EventHandler implements
    java.awt.event.ActionListener,
    java.awt.event.KeyListener,
    java.awt.event.MouseListener{
    public void mouseEntered(MouseEvent e) {
    for ( int i = 0; i < werteArray.length; i++ ){
    if ( e.getComponent() =??
    // more Events handled
    How can I know, that "werteArray " has been mouseEntered, and which element ?
    TIA Hanns

    Hi,
    You have already added the listener (you did that in the first loop).
    Your second loop should have this
    for ( int i = 0; i < werteArray.length; i++ ){
       if ( e.getComponent()  = werteArray) {
    //werteArray[i] is the source
    Kaj

  • Jtextfield and array

    Hi ALL
    I'm trying to create a Jtextfield but i also want to have an array on the Textfield, is their a simple way of doing this. I have tried but failed so can anyone help please.
    wen i try i get nopointerexception (frustrating)

    yea i tried
    int i;
    for(i=0; i < array.length; i++)
    JTextField[] array = new JTextField;
    don't compile                                                                                                                                                                                                                                               

  • JTextField array help

    Hey guys,
    I'm working on a java GUI right now and I've come up with a problem, hopefully you guys can help.
    I have a page that contains 50 text fields in 2 columns, so instead of making 50 lines of text field code, I decided to make an array of text fields and a for loop to cycle through each one (setting sizes, colors, editable, etc). Now the problem I'm running into is I will later down the road like to setText in these but it will be very cumbersome to have to call them by the arrayName[index #]. Just so I can better express what I mean heres is how I have to call each text field right now:
    arrayName[0].setText("1");
    arrayName[1].setText("2");
    This is how I'd like to be able to call each field:
    1stTextField.setText("1");
    2ndTextField.setText("2");
    Is there anyway I can make it so I can make an array of JTextFields and still be able to call an individual text field inside the array by name? If not, is there a way to point to another variable such as making
    1stTextField = arrayName[0] ;
    2ndTextField = arrayName[1];
    Thanks for the help guys.

    50 text fields in 2 columns... why not just use a JTable?
    Is there anyway I can make it so I can make an array of JTextFields and still be able to call an individual text field inside the array by name?Try a HashMap
    is there a way to point to another variable such as making
    1stTextField = arrayName[0] ;
    2ndTextField = arrayName[1]; Yes, exactly as you coded it up

  • JTextField array

    I�m using an array of jTextFields within a pane and I want to know which of this jTextFields was edited. I used the getSource method but I cannot interpret the data since I don�t get the index of the jTextField.

    Or, if you really do want to know which field was editted, here's one possible solution
    import javax.swing.*;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.awt.*;
    public class JTextFieldTester extends JFrame implements ActionListener{
        JTextField[] fields;
        public JTextFieldTester() {
            JPanel mainPane = (JPanel) getContentPane();
            mainPane.setLayout(new GridLayout(5,0,5,5));
            fields = new JTextField[5];
            for (int i = 0 ; i < 5 ; i++){
                fields[i] = new JTextField(20);
                fields.setName(""+i);
    fields[i].addActionListener(this);
    mainPane.add(fields[i]);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    pack();
    setLocationRelativeTo(null);
    setVisible(true);
    public void actionPerformed(ActionEvent e) {
    Component comp = (Component) e.getSource();
    if ( comp instanceof JTextField) {
    System.out.println(comp.getName());
    public static void main(String[] args) {
    new JTextFieldTester();
    This depends on user hitting enter after editting but you can do simular things with other types of events. The major point is that by assigning a name to each field you can rapidly determine which field fired the event. This is probably far more efficient for very large arrays of tields.
    Good Luck
    DB

  • Why cann't setText to a JTextField??

    Need your help!
    I produce an array of JTextField and add it to a JPanel.
    But I can only set text when initialized.
    In other place,even I call setText function, it doesn't work.
    Who know the reason??
    thanks in advance!!
    hgs.

    the code is like this:
    public class JFrame {
    JPanel contentPane;
    JPanel panel1 = new JPanel();
    JTextField jt = new JTextField(20);
    JPanel panel2 = new JPanel();
    MyClass class = new MyClass();
    public JFrame(){
    panel1.add(jt);
    panel2.add(class);
    contentPane = (JPanel)this.getContentPane();
    contentPane.add(panel1);
    contentPane.add(panel2);
    class MyClass{
    public MyClass(){
    // here if I call setText,It will work
    // jt.setText("test");
    public otherFunction(){
    // here ,it dosen't work
    jt.setText("test"); // it throw no exception when running

  • JTextField data reading problem

    Hello,
    I am attempting to construct a gui in which I parse data from a file into 3 different arrays, one is pure numbers the other two are strings of variables describing the data for a specific index of the data in the first array.
    I pass the raw data (all the numbers) to a plotting function, which plots 3 graphs at a time and is placed in a JLayeredPane. Over the plot I have a bar (place marker) with an associated mouselistener which calculates which index of the raw data array the bar is over as the bar is dragged over the plot.
    Beside the plots are two Jpanels that contain the data of the other two arrays. I only want to show the variable name and the data associated with index that is calculated from the bar/mouselistener described above.
    I designed this using JTextFields. I did not want to use JTable, because I only want to show the one column of data at a time.
    My problem is when I try to create an array of JTextFields, only a small portion of the data is passed. I want to use to 80 variables, but will only accept 12.
    for(int m=0;m<dvnum+1;m++){
         System.out.println("diagvar["+m+"][0]= "+diagvar[m][0]);
    for(int m=1;m<dvnum+1;m++){
         diaglabelField[m-1] = new JTextField(diagvar[m][0]);
         System.out.println("diaglabelfield= "+diagvar[m][0]);
         diagconfidenceField[m-1] = new JTextField(diagvar[m][1]);
         diagfieldPane.add(diaglabelField[m-1]);
         diagfieldPane.add(diagconfidenceField[m-1]);
    }where dvnum is the number of variables. The output from above gives a list of all the variable names, then a list of only those that were accepted into the JTextFields.
    Is there a limit as to how many JTextFields I can use? Or an easier way of doing this? If I use a smaller data set (under 15) it works, over that it only accepts 12 or so.
    I was thinking JTable could be used, but I would have to pass and build a JTable every time I wanted to look at a different data set for the new index.

    That's a long post, but I couldn't figure out from it just what your problem is. I'm guessing that your panel with the JTextFields only has room for 12 of them, and it doesn't scroll to show the others (from 13 to 80).
    If I'm right, I would suggest you don't use a bunch of JTextFields to show this data. And since there's only one column of data, you don't really need a JTable either. A JList would work just fine, assuming you don't need to update the data. Put it in a JScrollPane and you'll be able to use as many entries as you like.

  • JTextField [] and MouseEntered

    Hello
    I know this type of question has been asked in the forums before, but might is kind of different...i think.
    I have an array of JTextFields and i want each of there color to turn to GRAY when the mouse is on top of them.
    So if mouse is on top of TextField1 then only TextField1 turns GRAY.
    my Current mouseEntered() method is this
    public void mouseEntered(MouseEvent me)
                 for (int i=0; i < nasdaqTFs.length; i++){
                nasdaqTFs.setEditable(false);
              nasdaqTFs[i].setBackground(Color.GRAY);
    This, obviously, turns ALL the TextFields to GRAY when the cursor is in the Panel. So cursor can be anywhere and they all turn GRAY.
    So the question is...how do i find out when the Cursor is on the Specific TextField.

    Learning from earlier today This time i did write a Short Example of what i want done.
    Maybe this time i didnt do a bad job at it
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class Listener extends JFrame implements MouseListener
         JTextField t;
         JTextField t1;
         public Listener ()
              super ("test");
              Container c = getContentPane();
              c.setLayout(new BorderLayout());
              t = new JTextField(4);
              c.add(t, BorderLayout.NORTH);
              t1 = new JTextField (4);
              c.add(t1, BorderLayout.SOUTH);
              addMouseListener(this);
              setVisible(true);
              setSize(300,300);
              MouseMotionListener don = new MouseMotionAdapter(){
              public void mouseEntered(MouseEvent me) {
                   System.out.println("Enterd)");
                 me.getComponent().setBackground(Color.GRAY);
              t.addMouseMotionListener(don);
              t1.addMouseMotionListener(don);
         public static void main (String args[])
              Listener l = new Listener ();
           public void mouseEntered(MouseEvent me)
             /*JTextField tf = (JTextField) me.getSource();
             tf.setBackground(Color.GREEN);*/
                me.getComponent().setBackground(Color.GREEN);
           public void mouseExited(MouseEvent me)
             /*JTextField tf = (JTextField) me.getSource();
             tf.setBackground(Color.white);*/
                me.getComponent().setBackground(Color.WHITE);
           public void mousePressed(MouseEvent me){}
           public void mouseReleased(MouseEvent me){}
           public void mouseClicked(MouseEvent me){}
    }This GUI wont change colors of TF's either. However when i move around the GUI i see somethin Green flickering!

  • JTextField Problem

    Hi
    I have the following problem with JTextField:
    I have a class which builds up an array of JTextFields.I have another class which takes the array and lays the JTextFields out on a JFrame. It also controls the size of the JTextFields. Sometimes the text is longer than the size of the TextField and by looking at it the user dose not know if there is more text. The problem is he cannot see that the text is truncated ie no ... at the end of the text.
    I have to use JTextfields as it would take too much code change to use anything else.
    Any one any suggestions
    Thanks
    B

    Hi,
    I hope having understood your problem.
    I assume there that you know the string written in each textfield.
    You can construct your textfields such as :
    JTextField jT = new JTextField("MyKnownText");Then, you can do the following :
    public class Test
        public static main(String[] args)
            // Your main
            TextField[] array = // ... Your array of textfields.
            setSameComponentSize(Arrays.asList(array));
         * Resize with the same maximum size all specified components.
         * @param sComponents The components to be resized
        public static void setSameComponentSize(List sComponents)
            // compute the maximum dimension
            // and get only components
            // The dimension to which resize all components.
            Dimension lMaxDim = new Dimension();
            // The dimension to compare.
            Dimension lCompSize;
            // The component to resize.
            JComponent lComp;
            for (Iterator lIter = sComponents.iterator(); lIter.hasNext();)
                lComp = (JComponent) lIter.next();
                if (lComp != null)
                    lCompSize = lComp.getPreferredSize();
                    if (lCompSize.width > lMaxDim.width)
                        lMaxDim.width = lCompSize.width;
                    if (lCompSize.height > lMaxDim.height)
                        lMaxDim.height = lCompSize.height;
            // set the same size for all components
            for (Iterator lIter = sComponents.iterator(); lIter.hasNext();)
                lComp = (JComponent) lIter.next();
                if (lComp != null)
                    lComp.setPreferredSize(lMaxDim);
                    lComp.setMaximumSize(lMaxDim);
                    lComp.setMinimumSize(lMaxDim);
    }This code will set all the textfields to the same size that will be the maximum preferred size of the textfields.
    I do not test it but I hope it will be helpful for you.
    Stephane.

  • Access dynamically created JTextField

    i need to create an unknown number of JTextField in my GUI, and this number depends on a user-input value.
    Subsequently, i will need to read the strings from the JTextField. This is how I dynamically created my JTextField:
    while (i < num-1) {
    rangePanel.add(new JTextField("0")); // 0 is merely a default value
    how can i access the created JTextFields?
    thanx

    Is this the preferred style?
    I currently have an array of JTextFields but am having real grief (input on screen not availble fro getText(), etc.), code fragment:
    JTextField [] tf = new JTextField[10];
    for (i=0, i<10; i++) {
    ft[i] = new JTextField(15);
    user enters data on screen...
    String str = tf[0].getText() returns blank. Also <tab>ing jumps between first position and where ever mouse originally landed in field.

Maybe you are looking for

  • IBD Updation at the time of return Delivery

    Hello We are using PO and IBD process But after return delivery to the vendors the IBD is not getting updated, becasue of which we have to create the fresh PO insted of makeing use of old open PO. how this can be corrected Regards Niti Narayan

  • No playback in PP CS5.0.3, worked fine several weeks ago...

    I am unable to get a timeline to playback in PP CS5.0.3 with either existing or new projects (Win 7) on any source video, whether out of my Canon 5D2, Sony CX550, or simple AVI. The last time I used PP was several weeks ago and it was working fine th

  • PSI CALL - Project Update from PSI Cancelled 50%

    Hi : I created a psi call to update to update assignment actual work for project server 2007.  It had been running fine for the last few years.   I have a project if I looked at the back end data for this project the assignment actual work cost had b

  • Multi-camera capture with timecode

    I recently directed a 4 camera shoot with all cameras sync'd using timecode. I intend to edit the final product using Final Cut Studio 2. However, for now I only have iMovie and Final Cut Express HD. Is it possible to capture this project with timeco

  • Admin password.....can it be reset?

    I have an iMac that we recently put a password on.  My husband also thought to try out the parental controls to deter our 4 children from spending so much time on the computer.  Now that we see what it does and want to remove it (because the PC is on