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.

[369 byte] By [ignignokt84a] at [2007-11-26 19:59:36]
# 1

> I want the functionality of a text field with the look of a combo box

What do you mean by "look of a combo box"

Are you talking about:

a) the font

b) the foreground color

c) the background color

d) the arrow button

e) the border

You can't just use the "look" of a combo box for a text field. You need to customize the text field.

camickra at 2007-7-9 22:56:25 > top of Java-index,Desktop,Core GUI APIs...
# 2
By "look" I mean the border/button (no popup needed). I understand that I can't just take the look from the JComboBox, but I have been unable to determine how I can customize the JTextField to look like the JComboBox.
ignignokt84a at 2007-7-9 22:56:25 > top of Java-index,Desktop,Core GUI APIs...
# 3
Maybe you could do that with JSpinner.
Franziska at 2007-7-9 22:56:25 > top of Java-index,Desktop,Core GUI APIs...
# 4
a) Create a panel with a BorderLayout.b) add the text field to the centerc) add a BasicArrowButton to the Eastd) remove the border from the text field and add it to the panel
camickra at 2007-7-9 22:56:25 > top of Java-index,Desktop,Core GUI APIs...
# 5

Not the best solution since it wouldn't look like a combo under almost any core / 3rd party LAF - the createArrowButton() is in most cases overriden to return LAF-specific button.

Which brings me to a real question - if it should look like a combo, why it can't be a combo? What should the button on the text field do?

kirillga at 2007-7-9 22:56:25 > top of Java-index,Desktop,Core GUI APIs...
# 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.).

ignignokt84a at 2007-7-9 22:56:25 > top of Java-index,Desktop,Core GUI APIs...
# 7
Correct me if i'm wrong:You want to replace the list pop up of JComboBox with JDialog?
Icycoola at 2007-7-9 22:56:25 > top of Java-index,Desktop,Core GUI APIs...
# 8
That is correct.
ignignokt84a at 2007-7-9 22:56:25 > top of Java-index,Desktop,Core GUI APIs...
# 9
So why not a text field and a ... button right next to it?
kirillga at 2007-7-9 22:56:25 > top of Java-index,Desktop,Core GUI APIs...
# 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();

}

});

}

}

Michael_Dunna at 2007-7-9 22:56:25 > top of Java-index,Desktop,Core GUI APIs...
# 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.

ignignokt84a at 2007-7-9 22:56:25 > top of Java-index,Desktop,Core GUI APIs...