Modify appearance of JTextField
Hello all,
I am wondering if it is possible to modify only the appearance of a JTextField so that it looks like a JComboBox - I want the functionality of a text field with the look of a combo box. This is simply for aesthetic value. I have tried to find references on how to do this but have been unsuccessful so far. Any help would be greatly appreciated.
# 6
As of now I have a JTextField with a MouseListener which displays a JDialog containing a table of selectable values when the JTextField is clicked (or, more specifically, released - to account for movement within the box). I have considered using a JComboBox for this application but have had no luck finding a way to disable the popup menu from being displayed when the box is clicked. So I thought it might be possible to build/modify the appearance of the JTextField to that of a JComboBox.
I attempted camickr's suggestion of using a JPanel with a JTextField and BasicArrowButton, but determined it to be unsatisfactory due to the number of procedures which would need to be overridden (for listeners, enabled, tooltiptext, etc.).
# 10
>> You want to replace the list pop up of JComboBox with JDialog?
> That is correct.
can't see a real benefit.
anyway, this might be one way
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
class Testing
{
String[] cities = {"London","Madrid","New York","Paris","Rome","Sydney","Tokyo"};
JComboBox cbo = new JComboBox(cities);
JFrame f;
public void buildGUI()
{
cbo.setUI(new MyUI());
f = new JFrame();
f.getContentPane().add(cbo);
f.pack();
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
class MyUI extends javax.swing.plaf.metal.MetalComboBoxUI
{
protected javax.swing.plaf.basic.ComboPopup createPopup()
{
javax.swing.plaf.basic.BasicComboPopup popup =
(javax.swing.plaf.basic.BasicComboPopup)super.createPopup();
popup.setPreferredSize(new Dimension(0,0));
return popup;
}
protected JButton createArrowButton()
{
JButton btn = super.createArrowButton();
btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
new ComboDialog();
}
});
return btn;
}
}
class ComboDialog
{
public ComboDialog()
{
final JList list = new JList(cities);
final JDialog d = new JDialog(f,true);
d.getContentPane().add(new JScrollPane(list));
d.pack();
d.setLocationRelativeTo(f);
list.addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent lse){
if(lse.getValueIsAdjusting() == false){
cbo.setSelectedItem(list.getSelectedValue());
d.dispose();
}
}
});
d.setVisible(true);
}
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable(){
public void run(){
new Testing().buildGUI();
}
});
}
}
# 11
Thanks Michael_Dunn, I was able to use the code you supplied as a template to create an extension of JComboBox that now displays my JDialog instead of the popup menu.
> can't see a real benefit.
The benefit for me is:
I have a long list of items to be chosen. In the JDialog I've created there is a JTable of these items and their associated information (description, value, etc.), as well as well as a filter. This is used for selecting values in other sections of my class, so I wanted to maintain uniformity and use it here too.
And the JComboBox "look" was simply a desired feature.