Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Displaying comboboxes in tables



On Dec 24, 2007, at 11:43 AM, J Diaz wrote:

OK, the main problem was that the editable combobox Aqua look sucks (IMHO) ,so I made:

c.setEditable(true); //as I need a general combobox that would accept anything to display
c.setSelectedItem(value); //set value, only valid if editable (or possible values fixed ...)
c.setEditable(false); //non editable


And it is acceptable (for me). And, anyway, my comboboxes are not editable ...

(Never felt confortable answering to myself, but may be interesting to others ...)
I was tired of seeing JTable text fields that when you touch them become anything in Swing ...

Perhaps for more uniformity, you could try something like this:

	class TableComboBoxRenderer implements TableCellRenderer {
		DefaultCellEditor renderer = new DefaultCellEditor(new JComboBox());

		Color co = new Color(180, 180, 255);
		Color co2 = new Color(255, 0, 255);

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
JComboBox combo = (JComboBox)renderer.getTableCellEditorComponent(table, value, isSelected, row, column);

combo.removeAllItems();
combo.addItem(value);
combo.setSelectedItem(value);
combo.setOpaque(true);


			if (isSelected) {
				combo.setForeground(table.getSelectionForeground());
				combo.setBackground(table.getSelectionBackground());
			} else {
				combo.setForeground(co2);
				combo.setBackground(co);
			}

			return combo;
		}
	}

What this will do is provide you with the same double-headed arrows that native Cocoa table-cell popups have. Then you can take the resulting component, and then customize the foreground and background however you like. This also has the additional benefit of providing a consistent appearance between the editor and the renderer, and it looks reasonable in other look and feels besides Aqua.

It's true that the default JComboBox appearance really doesn't belong inside of table cells, but this is one of those parts of Swing which is hard to match the native platform with the existing Renderer/Editor model.

Hope this helps,
Mike Swingler
Java Frameworks Engineer
Apple Inc.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/java-dev/email@hidden

This email sent to email@hidden
References: 
 >Re: Displaying comboboxes in tables (From: J Diaz <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.