Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > problem with autocomplete combobox

Reply
Thread Tools

problem with autocomplete combobox

 
 
Gowri
Guest
Posts: n/a
 
      06-17-2008
Hi all,

I've no idea if this is the right way to implement an auto complete
combobox but this one seems to work fine except for the fact that
every first time the model's contents completely change, the text
field and the popup are of different sizes.

How do I fix this?
Please find my code below:


import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.File;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import javax.swing.plaf.basic.BasicComboPopup;
import javax.swing.text.JTextComponent;

import org.jdom.Element;
import org.jdom.input.SAXBuilder;



public class MeshNameDialog{
private static MeshNameDialog mnd = null;
private JComboBox comboBox = new JComboBox();
private static ComboBoxModel model;
private static JTextComponent editor;
private static boolean hidePopupOnFocusLoss;

private JButton ok = new JButton("OK");
private JButton cancel = new JButton("Cancel");
private GridBagLayout layoutMgr = new GridBagLayout();
private GridBagConstraints gbc = new GridBagConstraints();
private static JFrame frame = new JFrame();
private JPanel buttonPanel = new JPanel();

static Object[] names = {"looooooooooooooong string","short string",
"crappy stringggg"};

private MeshNameDialog() {
frame.setSize(350, 120);

// create and show a window containing the combo box
frame.setLayout(layoutMgr);

gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(10, 10, 5, 10);
frame.getContentPane().add(comboBox, gbc);

ok.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
MeshAppConfig.setMeshName((String) comboBox.getEditor()
.getItem());
frame.setVisible(false);
}
});
buttonPanel.add(ok);

cancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.dispose();
System.exit(0);
}
});
buttonPanel.add(cancel);

gbc.gridx = 0;
gbc.gridy = 1;
gbc.insets = new Insets(3, 0, 3, 0);
frame.getContentPane().add(buttonPanel, gbc);

Dimension screen =
java.awt.Toolkit.getDefaultToolkit().getScreenSize ();
int x = (screen.width - frame.getWidth()) / 3;
int y = 2 * (screen.height - frame.getHeight()) / 5;
frame.setLocation(x, y);
frame.setTitle("Mesh Name Dialog");
}

public static MeshNameDialog getInstance() {
if ((null == mnd))
mnd = new MeshNameDialog();
mnd.init();
return mnd;
}

private void init() {
if (null == names) {
String[] tarr = { " " };
comboBox.setModel(new DefaultComboBoxModel(tarr));
} else {
comboBox.setModel(new DefaultComboBoxModel(names));
}
model = comboBox.getModel();
editor = (JTextComponent) comboBox.getEditor().getEditorComponent();
comboBox.setEditable(true);
comboBox.getEditor().setItem("");

editor.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent e) {
String textFieldContents = (String) comboBox.getEditor()
.getItem();
if (!Character.isIdentifierIgnorable(e.getKeyChar())
|| e.getKeyCode() == KeyEvent.VK_BACK_SPACE
|| e.getKeyCode() == KeyEvent.VK_DELETE) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
String item = (String) comboBox.getSelectedItem();
comboBox.getEditor().setItem(item);
comboBox.setPopupVisible(false);
return;
}

if (e.getKeyCode() == KeyEvent.VK_UP
|| e.getKeyCode() == KeyEvent.VK_DOWN) {
String item = (String) comboBox.getSelectedItem();
comboBox.repaint();
comboBox.getEditor().setItem(item);
comboBox.setPopupVisible(true);
return;
}

Object[] matchedStrings = getItems(textFieldContents);
if (matchedStrings != null) {
comboBox.setModel(new DefaultComboBoxModel(
matchedStrings));
comboBox.setPopupVisible(true);

} else {
comboBox.setModel(new DefaultComboBoxModel(names));
comboBox.setPopupVisible(false);
}
}
comboBox.getEditor().setItem(textFieldContents);
}
});
// Bug 5100422 on Java 1.5: Editable JComboBox won't hide popup when
tabbing out
hidePopupOnFocusLoss =
System.getProperty("java.version").startsWith(
"1.5");
editor.addFocusListener(new FocusAdapter() {
public void focusGained(FocusEvent e) {

}

public void focusLost(FocusEvent e) {
// Workaround for Bug 5100422 - Hide Popup on focus loss
if (hidePopupOnFocusLoss)
comboBox.setPopupVisible(false);
}
});

frame.setVisible(true);
}

private static Object[] getItems(String text) {
Vector v = new Vector();
for (int i = 0; i < names.length; i++) {
if (((String) names[i]).toLowerCase()
.startsWith(text.toLowerCase()))
v.add(names[i]);
}
if (v.size() > 0)
return v.toArray();
return null;
}

public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
MeshNameDialog.getInstance();
}
});
}
}


I'd really appreciate all the help I can get.

Regards,
Gowri
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Combobox Project: How to put 4 text items in the combobox christopher.m.lusardi@gmail.com Java 5 10-11-2012 09:29 PM
AutoComplete ComboBox in ASP.NET? RajW ASP .Net 4 09-06-2006 03:28 PM
Enable autocomplete although site set autocomplete to off Frank Dohrmann Firefox 1 03-31-2006 11:28 PM
databound autocomplete ComboBox rasta ASP .Net Web Controls 2 11-29-2004 06:48 PM
Autocomplete problem with IE6 Bob Computer Support 2 07-11-2004 01:39 PM



Advertisments