Solved

Need to display a JSeparator in a JComboBox

Posted on 2008-10-02
5
912 Views
Last Modified: 2013-12-17
I use the following comboBox to display a set of strings to the operator, that will
grey out the third selection (see "Text Line 3", false)...
My problem is that the separator does not display correctly - it seems to produce the ToString()
results in the line of text. What can I do to display the horizontal separator in the comboBox list?

Thanks

Object[] items = { 	new ComboItem<String>(new String("Text Line 1"), true),
					new ComboItem<String>(new String("Text Line 2"), true), 
					new ComboItem<JSeparator>(new JSeparator(), true),
					new ComboItem<String>(new String("Text Line 3"), false), 
				
				
		selectionComboBox = new JComboBox(items);
		selectionComboBox.setRenderer(new ComboRenderer());
		selectionComboBox.addActionListener(new ComboListener(selectionComboBox));
		selectionComboBox.setBounds(140, 101, 254, 25);
		selectionComboBox.setMaximumRowCount(14);
		getContentPane().add(selectionComboBox);
		
		// CellRenderer
		class ComboRenderer extends JLabel implements ListCellRenderer {
		private static final long serialVersionUID = 1L;
 
		public ComboRenderer() {
			setOpaque(true);
			setBorder(new EmptyBorder(1, 1, 1, 1));
		}
 
		public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
				boolean cellHasFocus) {
			if (isSelected) {
				setBackground(list.getSelectionBackground());
				setForeground(list.getSelectionForeground());
			} else {
				setBackground(list.getBackground());
				setForeground(list.getForeground());
			}
			if (!((CanEnable) value).isEnabled()) {
				setBackground(list.getBackground());
				setForeground(UIManager.getColor("Label.disabledForeground"));
			}
			setFont(list.getFont());
			setText((value == null) ? "" : value.toString());
			return this;
		}
	}
 
	/**
	 * Handles the combo box selection
	 *
	 */
	class ComboListener implements ActionListener {
		JComboBox combo;
		Object currentItem;
 
		ComboListener(JComboBox combo) {
			this.combo = combo;
			combo.setSelectedIndex(0);
			currentItem = combo.getSelectedItem();
		}
 
		public void actionPerformed(ActionEvent e) {
			Object tempItem = combo.getSelectedItem();
			if (!((CanEnable) tempItem).isEnabled()) {
				combo.setSelectedItem(currentItem);
			} else {
				currentItem = tempItem;
			}
		}
	}
 
	/**
	 * The item displayed by the combo box
	 *
	 * @param <T>
	 */
	class ComboItem<T> implements CanEnable {
		T obj;
		boolean isEnable;
 
		ComboItem(T obj, boolean isEnable) {
			this.obj = obj;
			this.isEnable = isEnable;
		}
 
		ComboItem(T obj) {
			this(obj, true);
		}
 
		public boolean isEnabled() {
			return isEnable;
		}
 
		public void setEnabled(boolean isEnable) {
			this.isEnable = isEnable;
		}
 
		public String toString() {
			return obj.toString();
		}
 
		public T getValue() {
			return obj;
		}
	}

Open in new window

0
Comment
Question by:dbCnc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 22633653
>> My problem is that the separator does not display correctly - it seems to produce the ToString()

Sure. That's what you told it to do:

setText((value == null) ? "" : value.toString());
0
 

Author Comment

by:dbCnc
ID: 22635643
xxynk,

Thanks for the reply - but it's still not obvious to me how to display the separator as a line.

==>Sure. That's what you told it to do:
==>setText((value == null) ? "" : value.toString());

Perhaps I can test the obj T in ComboItem, and do something different from what is returned in line 93,
but I want a solid line to appear - of the correct length for the displayed ComboBox.

Could you elaborate?

Thanks again...
0
 
LVL 37

Accepted Solution

by:
zzynx earned 500 total points
ID: 22677209
0
 

Author Closing Comment

by:dbCnc
ID: 31504684
ZZYNX,

The first solution was exactly what I was looking for. I had gone through quite a few examples from that site - but never found that one.

I added my enable/disable code to the listeners, and I'm all set...

Thanks Again
0
 
LVL 37

Expert Comment

by:zzynx
ID: 22679280
Thanx 4 axxepting
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question