5 Replies - 794 Views - Last Post: 08 April 2011 - 07:02 PM Rate Topic: -----

#1 Bverly  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 41
  • Joined: 30-May 08

Question About Auto Fill

Posted 08 April 2011 - 12:24 AM

I am creating a program to store Product information. Anyone know how to create auto fill function on java??
example:
when i fill productID, the product's name should be filled automatically >,<?
Anyone has solution ??
Is This A Good Question/Topic? 0
  • +

Replies To: Question About Auto Fill

#2 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 286
  • View blog
  • Posts: 1,619
  • Joined: 15-May 08

Re: Question About Auto Fill

Posted 08 April 2011 - 05:11 AM

You more than likely would need some thread that automatically checks the current string in the textbox against the current products. That is a really interesting thing, i will look into it and see what i can come up with :)
Was This Post Helpful? 0
  • +
  • -

#3 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 286
  • View blog
  • Posts: 1,619
  • Joined: 15-May 08

Re: Question About Auto Fill

Posted 08 April 2011 - 05:53 AM

This is a pretty cool thing, at the moment i have just setup a very basic and inefficient way of doing things. Basically i have added a KeyListener interface on the JTextField and each time an ActionEvent is fired i just iterate a list and update a JLabel containing a suggestion of what the word may be. It is very ghetto and i will look at implementing something better.

I will just put this out there until such time as i address it further. It works somewhat, however is far from complete or perfect. Should help you get started.

/**
 *
 * @author DaneAU
 */

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class Main extends JFrame implements ActionListener, KeyListener {

    private LinkedList<String> words;
    private JButton btnOk;
    private JTextField txtInputField;
    private JLabel lblOutput;

    private Main() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new GridLayout(3,1,5,5));
        setSize(200,100);
        initComponents();
        this.add(lblOutput);
        this.add(txtInputField);
        this.add(btnOk);


        // Just add some words
        words = new LinkedList<String>();
        words.add("dream");
        words.add("dreamincode");
        words.add("in");
        words.add("code");
        words.add("isftw");

    }

    private void initComponents() {
        btnOk = new JButton("OK");
        btnOk.addActionListener(this);
        txtInputField = new JTextField();
        txtInputField.addActionListener(this);
        txtInputField.addKeyListener(this);
        lblOutput = new JLabel("Output Info");
    }

    public void actionPerformed(ActionEvent evt) {
        if( evt.getSource() == btnOk ) {

        }
        if( evt.getSource() == txtInputField ) {
            lblOutput.setText( txtInputField.getText());
        }
    }

    public void keyPressed(KeyEvent evt) {
        if(evt.getSource() == txtInputField) {
            String suggestion = "unknown word";
            String current = txtInputField.getText();

            for( String s : words ) {
                if( s.contains(current.subSequence(0, current.length()))) {
                    suggestion = "perhaps : " + s;
                    break;
                }
            }
            lblOutput.setText(suggestion);
        }
    }

    public void keyReleased(KeyEvent evt) {
        
    }

    public void keyTyped(KeyEvent evt) {
        
    }

    public static void main(String[] args) {
        Main myApp = new Main();
        myApp.setVisible(true);
    }

}

Was This Post Helpful? 1
  • +
  • -

#4 Bverly  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 41
  • Joined: 30-May 08

Re: Question About Auto Fill

Posted 08 April 2011 - 09:51 AM

Wow Awesome Job :D, i was thinking about key binding..it still inefficient to use.

thanks for the answer
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Question About Auto Fill

Posted 08 April 2011 - 06:58 PM

View PostDaneAU, on 08 April 2011 - 07:11 AM, said:

You more than likely would need some thread that automatically checks the current string in the textbox against the current products. That is a really interesting thing, i will look into it and see what i can come up with :)

Yash... what an horrible solution. There are better ways of doing that
Was This Post Helpful? 0
  • +
  • -

#6 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 286
  • View blog
  • Posts: 1,619
  • Joined: 15-May 08

Re: Question About Auto Fill

Posted 08 April 2011 - 07:02 PM

Yea i have come to realise that there are better ways indeed. It would be a pretty handy thing for my current application, so i will put up a snippet once i get it working right :) Don't use a thread or you could run into problems.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1