Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Help me...its urgent...its simple but no idea

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    3
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Angry Help me...its urgent...its simple but no idea

    im using netbeans ide

    AutoSuggest.java... this class file is valid

    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;
     
    public class AutoSuggest extends JPanel{
        private final JTextField tf;
        private final JComboBox combo = new JComboBox();
        private final Vector<String> v = new Vector<String>();
        public AutoSuggest() {
            super(new BorderLayout());
            combo.setEditable(true);
            tf = (JTextField) combo.getEditor().getEditorComponent();
            tf.addKeyListener(new KeyAdapter() {
                    public void keyTyped(KeyEvent e) {
                   EventQueue.invokeLater(new Runnable() {
                public void run() {
                    String text = tf.getText();
                            if(text.length()==0) {
                        combo.hidePopup();
                        setModel(new DefaultComboBoxModel(v), "");
                    }else{
                        DefaultComboBoxModel m = getSuggestedModel(v, text);
                        if(m.getSize()==0 || hide_flag) {
                              combo.hidePopup();
                            hide_flag = false;
                        }else{
                            setModel(m, text);
                            combo.showPopup();
                        }
                    }
                }
            });
                }
                    public void keyPressed(KeyEvent e) {
                   String text = tf.getText();
             int code = e.getKeyCode();
                 if(code==KeyEvent.VK_ENTER) {
                if(!v.contains(text)) {
                    v.addElement(text);
                    Collections.sort(v);
                    setModel(getSuggestedModel(v, text), text);
                }
                hide_flag = true; 
            }else if(code==KeyEvent.VK_ESCAPE) {
                hide_flag = true; 
            }else if(code==KeyEvent.VK_RIGHT) {
                for(int i=0;i<v.size();i++) {
                    String str = v.elementAt(i);
                    if(str.startsWith(text)) {
                        combo.setSelectedIndex(-1);
                        tf.setText(str);
                        return;
                    }
                }
            }
                }
          });
            String[] countries = {"Afghanistan", "Albania", "Algeria", "Andorra", "Angola","Argentina"
    ,"Armenia","Austria","Bahamas","Bahrain", "Bangladesh","Barbados", "Belarus","Belgium",
    "Benin","Bhutan","Bolivia","Bosnia & Herzegovina","Botswana","Brazil","Bulgaria",
    "Burkina Faso","Burma","Burundi","Cambodia","Cameroon","Canada", "China","Colombia",
    "Comoros","Congo","Croatia","Cuba","Cyprus","Czech Republic","Denmark", "Georgia",
    "Germany","Ghana","Great Britain","Greece","Hungary","Holland","India","Iran","Iraq",
    "Italy","Somalia", "Spain", "Sri Lanka", "Sudan","Suriname", "Swaziland","Sweden",
    "Switzerland", "Syria","Uganda","Ukraine","United Arab Emirates","United Kingdom",
    "United States","Uruguay","Uzbekistan","Vanuatu","Venezuela","Vietnam",
    "Yemen","Zaire","Zambia","Zimbabwe"};
              for(int i=0;i<countries.length;i++){
                      v.addElement(countries[i]);
              }
            setModel(new DefaultComboBoxModel(v), "");
            JPanel p = new JPanel(new BorderLayout());
            p.setBorder(BorderFactory.createTitledBorder("AutoSuggestion Box"));
            p.add(combo, BorderLayout.NORTH);
            add(p);
            setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
            setPreferredSize(new Dimension(300, 150));
        }
        private boolean hide_flag = false;
           private void setModel(DefaultComboBoxModel mdl, String str) {
            combo.setModel(mdl);
            combo.setSelectedIndex(-1);
            tf.setText(str);
        }
    private static DefaultComboBoxModel getSuggestedModel(java.util.List<String> list, String text) {
            DefaultComboBoxModel m = new DefaultComboBoxModel();
            for(String s: list) {
                if(s.startsWith(text)) m.addElement(s);
            }
            return m;
        }
        }

    this runCombo.java is created using gui... im goin to add the class function to this combobox in runCombo.java
    i dont know how to simplify it... hope moderator will help me

    public class runCombo extends javax.swing.JFrame {
     
        /** Creates new form runCombo */
        public runCombo() {
            initComponents();
        }
     
         /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
     
            cBox = new javax.swing.JComboBox();
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
     
            cBox.setEditable(true);
            cBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "a" }));
     
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(30, 30, 30)
                    .addComponent(cBox, 0, 260, Short.MAX_VALUE)
                    .addGap(37, 37, 37))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(19, 19, 19)
                    .addComponent(cBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(18, Short.MAX_VALUE))
            );
     
            pack();
        }// </editor-fold>                        
     
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {        
     
            /* Create and display the form */
            java.awt.EventQueue.invokeLater(new Runnable() {
     
                public void run() {
                    runCombo runCombo = new runCombo();
                    runCombo.setVisible(true);                
                }
            });
        }
        // Variables declaration - do not modify                     
        private javax.swing.JComboBox cBox;
        // End of variables declaration                   
    }

    now to make the runCombo combobox to implements the class in AutoSuggest.java
    please help me... i need urgent answer... please help me moderator and admin...
    please send me your email here... i will post u the src folder...zurich_fredo@yahoo.com
    Last edited by zurich91; January 24th, 2012 at 09:59 PM.


  2. #2
    Junior Member
    Join Date
    Sep 2011
    Posts
    3
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help me...its urgent...its simple but no idea

    please figure it out... its urgent

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,166
    My Mood
    Hungover
    Thanks
    141
    Thanked 596 Times in 511 Posts

    Default Re: Help me...its urgent...its simple but no idea

    Please see the link in my signature on asking questions the smart way, as well as this: How to Program (stuck on homework? READ THIS BEFORE POSTING)

    If you do not have time to ask an actual question, use highlight tags, create an SSCCE, or even tell us what the actual problem is, why would you expect somebody else to put their time in? We're all doing this for free, in our spare time, and it's extremely rude to just dump your code here and mark it urgent.

    Clean this post up using the resources I just gave you, and we'll go from there.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. The Following User Says Thank You to KevinWorkman For This Useful Post:

    zurich91 (January 24th, 2012)

Similar Threads

  1. No idea how to fix it.
    By Hammer67 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 13th, 2011, 10:41 AM
  2. Need urgent help regarding java word wrap function.. URGENT
    By coldice in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 16th, 2011, 05:43 AM
  3. Need urgent help in assignment of JAVA, any idea suggestion plz
    By aesthete in forum Java Theory & Questions
    Replies: 2
    Last Post: January 6th, 2011, 04:58 AM
  4. No idea what to do for this
    By jwb4291 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 18th, 2010, 07:16 PM
  5. urgent simple help for a very simple program
    By albukhari87 in forum Java Applets
    Replies: 4
    Last Post: June 5th, 2010, 03:43 PM