Help in resolving errors in a simple glazed list

I am trying to incorporate a simple glazed list into my main program by doing a sample side code to understand how it works, modify it to suit my what I want and finally incorporate it in my main code.  Once this works I will substitute a ResultSet of customer names from an SQLite database.  Here is what I have done and needs help.  I'v added a Panel p, combobox cb2 (auto-completion), JTextField tf to JFrame frame.  User must select a name from cb2; it cannot skip to the next field tf without selecting a name.  When the user after skipping through finally selects the name I want to make tf = to cb2 and print it in that field.  Please help me.  Below is an explanation of the code I have added:-
Snippet ID=8238859 // my modification
Snippet ID=8238860 // the original downloaded simpleglazedlist at your suggestion
You will ned " glazedlists-1.8.0 .jar"  I tried to upload the file " glazedlists-1.8.0 " but your add file feature in your website does not support .jar even .jar embedded in a .zip file.  I got it from your suggestions at
http://www.java2s.com/Code/Jar/g/glazedlists.htm

package autocompletion;

import java.awt.BorderLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.*;

import ca.odell.glazedlists.GlazedLists;
import ca.odell.glazedlists.swing.AutoCompleteSupport;

//*Sample auto-completion for a simple model with GlazedLists. @author Kirill Grouchnikov

public class SimpleGlazedList2 {
	private JComboBox cb2;
	private JPanel p;
	private JTextField tf;

	public SimpleGlazedList2() {	
		// to add after this works a ResultSet customer name from SQLite database
		Object[] elements =new Object[]{"Ester","Jordi","Jordina","Jorge","Sergi"};
		
		// Install auto-completion support.
		AutoCompleteSupport support = AutoCompleteSupport.install(
		cb2, GlazedLists.eventListOf(elements));
		cb2.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				SwingUtilities.invokeLater(new Runnable() {
					public void run() {
						// cb2 cannot be empty; if empty stay on that field
						if(cb2.getSelectedItem().equals(null)) {
							return ;
						} else {	// if item selected make JTextField tf = to cb2 
							tf.equals(cb2.getSelectedItem()) ;
						}
					}
				});
			}
		});
	}

	public static void main(String[] args) throws Exception {
		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				SimpleGlazedList2 sample = new SimpleGlazedList2();
				JFrame frame = new JFrame("Simple Glazed Lists");
				JPanel p = new JPanel();
				JComboBox cb2 = new JComboBox();
				JTextField tf = new JTextField();
				p.add(cb2);
				frame.add(p);
//				frame.add(sample, BorderLayout.CENTER);
				frame.setSize(300, 200);
				frame.setLocationRelativeTo(null);
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame.setVisible(true);
			}
		});
	}
}

Open in new window

package autocompletion;

import java.awt.BorderLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.*;

import ca.odell.glazedlists.GlazedLists;
import ca.odell.glazedlists.swing.AutoCompleteSupport;

//*Sample auto-completion for a simple model with GlazedLists. @author Kirill Grouchnikov

public class SimpleGlazedLists extends JPanel {
	private JComboBox comboBox;
	
	public SimpleGlazedLists() {
		Object[] elements =new Object[]{"Ester","Jordi","Jordina","Jorge","Sergi"};
		this.comboBox = new JComboBox();
		this.add(this.comboBox);
		
		// Install auto-completion support.
		AutoCompleteSupport support = AutoCompleteSupport.install(
		this.comboBox, GlazedLists.eventListOf(elements));
		
		System.out.println("Is editable - " + this.comboBox.isEditable()+ ". Surprise!");

		this.comboBox.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				SwingUtilities.invokeLater(new Runnable() {
					public void run() {
						System.out.println("Selected '"
								+ SimpleGlazedLists.this.comboBox
										.getSelectedItem() + "'");
					}
				});
			}
		});
	}

	public static void main(String[] args) throws Exception {
		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				SimpleGlazedLists sample = new SimpleGlazedLists();
				JFrame frame = new JFrame("Simple Glazed Lists");
				frame.add(sample, BorderLayout.CENTER);
				frame.setSize(300, 200);
				frame.setLocationRelativeTo(null);
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame.setVisible(true);
			}
		});
	}
}

Open in new window

Vanavah EdwardsAsked:
Who is Participating?
 
CEHJConnect With a Mentor Commented:
I don't know what you've done there - you don't need to depart much from what you first had. Try
import java.awt.BorderLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.*;

import ca.odell.glazedlists.GlazedLists;
import ca.odell.glazedlists.swing.AutoCompleteSupport;

//*Sample auto-completion for a simple model with GlazedLists. @author Kirill Grouchnikov

public class SimpleGlazedLists extends JPanel {
	private JComboBox comboBox;
	private JTextField tf;
	
	public SimpleGlazedLists() {
		Object[] elements =new Object[]{"Ester","Jordi","Jordina","Jorge","Sergi"};
		tf = new JTextField(10);
		add(tf);
		comboBox = new JComboBox();
		add(comboBox);
		
		// Install auto-completion support.
		AutoCompleteSupport support = AutoCompleteSupport.install(
		this.comboBox, GlazedLists.eventListOf(elements));
		
		System.out.println("Is editable - " + this.comboBox.isEditable()+ ". Surprise!");

		this.comboBox.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				SwingUtilities.invokeLater(new Runnable() {
					public void run() {
						System.out.println("Selected '"
								+ SimpleGlazedLists.this.comboBox
										.getSelectedItem() + "'");
						Object selected = comboBox.getSelectedItem();
						if (selected != null) {
						    tf.setText(selected.toString());
						}
					}
				});
			}
		});
	}

	public static void main(String[] args) throws Exception {
		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				SimpleGlazedLists sample = new SimpleGlazedLists();
				JFrame frame = new JFrame("Simple Glazed Lists");
				frame.add(sample, BorderLayout.CENTER);
				frame.setSize(300, 200);
				frame.setLocationRelativeTo(null);
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame.setVisible(true);
			}
		});
	}
}

Open in new window

0
 
CEHJCommented:
You don't say what the error is- please post full stack trace if an exception
0
 
Vanavah EdwardsAuthor Commented:
The code would not run because in line 52 of my code (which I forgot to add is commented out it comes up as an error on cb2.add(sample) which reads -
The method add(Component) in the type Container is not applicable for the arguments (SimpleGlazedList2)

cb2.add(sample) is to replace commented line 53 // frame.add(sample, BorderLayout.CENTER);
I get the same error on "add"  on that line.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
Vanavah EdwardsAuthor Commented:
This works well but partially solves the problem.  Bear with me!  
(1) Your combobox skips the field which must be a manditory selection not empty.  
(2) I have to call the combobox cb2 where yours uses thos.combobox
(3) I have to add cb2 to my panel p while yours uses extends JPanel and put the components on the frame.  
(4) Your combobox is not mandatory and tabs to textfield on an empty field.  The name should only be printed after a name is selected and the tab key is press.
0
 
Vanavah EdwardsAuthor Commented:
I would like to incorporate this in my main or would it be better to call this class separately just for the auto-selection of combobox only and then pass the class variable back to my main.  In that way I would not have to worry about panel and coding for incorporatioin.  However, how can I call this as a separate class from my main.
0
 
CEHJCommented:
>>or would it be better to call this class separately just for the auto-selection of combobox only

Definitely. It would be better as a separate unit, in a JPanel. As you've already got it
0
 
Vanavah EdwardsAuthor Commented:
I will accept your advice.  I am almost finish.
How can I make the combobox manditory?
How can I make the tf field print only after the selection is made and I press tab?
Can you please adjust your code?
0
 
CEHJCommented:
You can't ultimately prevent the user moving off a component, but just make sure there's a selection:
import ca.odell.glazedlists.GlazedLists;
import ca.odell.glazedlists.swing.AutoCompleteSupport;

import java.awt.BorderLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.*;


//*Sample auto-completion for a simple model with GlazedLists. @author Kirill Grouchnikov
public class SimpleGlazedLists extends JPanel {
    private JComboBox comboBox;
    private JTextField tf;

    public SimpleGlazedLists() {
        Object[] elements = new Object[] {
                "Ester", "Jordi", "Jordina", "Jorge", "Sergi"
            };
        tf = new JTextField(10);
        add(tf);
        comboBox = new JComboBox();
        add(comboBox);

        // Install auto-completion support.
        AutoCompleteSupport support = AutoCompleteSupport.install(this.comboBox,
                GlazedLists.eventListOf(elements));

        System.out.println("Is editable - " + this.comboBox.isEditable() +
            ". Surprise!");

        comboBox.setSelectedIndex(0);
        echoComboInTextField();
        this.comboBox.addItemListener(new ItemListener() {
                public void itemStateChanged(ItemEvent e) {
                    SwingUtilities.invokeLater(new Runnable() {
                            public void run() {
                                System.out.println("Selected '" +
                                    SimpleGlazedLists.this.comboBox.getSelectedItem() +
                                    "'");
                                echoComboInTextField();
                            }
                        });
                }
            });
    }

    private void echoComboInTextField() {
        Object selected = comboBox.getSelectedItem();

        if (selected != null) {
            tf.setText(selected.toString());
        }
    }

    public static void main(String[] args) throws Exception {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    SimpleGlazedLists sample = new SimpleGlazedLists();
                    JFrame frame = new JFrame("Simple Glazed Lists");
                    frame.add(sample, BorderLayout.CENTER);
                    frame.setSize(300, 200);
                    frame.setLocationRelativeTo(null);
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setVisible(true);
                }
            });
    }
}

Open in new window

0
 
Vanavah EdwardsAuthor Commented:
snippet #. . . 46 shows an example of a simple glazed list from
http://today.java.net/pub/a/today/2007/07/19/adding-auto-completion-to-swing-comboboxes.html
snippet #. . . 45 is my modification but cant get it to work.  The code we use earlier is too cumbersome.  This is only a 4 or so lines of code can you resolve it.  I am getting error with this in this.combobox and add in line 26

package autocompletion;

import java.awt.BorderLayout;
import java.awt.Component;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

import ca.odell.glazedlists.GlazedLists;
import ca.odell.glazedlists.swing.AutoCompleteSupport;

public class SimpleGlazedListVE {

	public static void main(String[] args) {
		Object[] elements = new Object[] { "Ester", "Jordi", "Jordina",
		        "Jorge", "Sergi" };
		this.comboBox = new JComboBox();
		AutoCompleteSupport support = AutoCompleteSupport.install(
		        this.comboBox, GlazedLists.eventListOf(elements));
		System.out.println("Is editable - " + this.comboBox.isEditable()
		        + ". Surprise!");
		
		JFrame f = new JFrame();
		JPanel p = new JPanel();
		f.add(support, BorderLayout.CENTER);
		f.pack();
		f.setVisible(true);
		
	}
}

Open in new window

Object[] elements = new Object[] { "Ester", "Jordi", "Jordina", "Jorge", "Sergi" };
this.comboBox = new JComboBox();

AutoCompleteSupport support = AutoCompleteSupport.install(
        this.comboBox, GlazedLists.eventListOf(elements));

System.out.println("Is editable - " + this.comboBox.isEditable() + ". Surprise!");

Open in new window

0
 
CEHJCommented:
You've gone backwards. Making standalone code which is not reusable is not going to help you. You need a self-contained JPanel subclass
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.