09 | 02 | 2010
Main Menu
Affiliates
Who's Online
We have 28 guests online
Alexa
Tips and Tricks for JTree, JList and JCombobox Part I
Java Concepts
Written by Phoenix   
Monday, 02 June 2008 13:06

Rating 3.0/5 (9 votes)

In terms of usability, swings Jtree, JList and JCombobox are little dumb. Every swing developer would have wanted to have more usability features in JTree, JList and JCombobox which are found in their native counterparts.

1) Continuation tips in JTree and JList.
2) Horizontal Scrollbar in a JCombobox.
3) Wider Combocox Popup.
4) Resizable Combobox popup.
5) Searchable Combobox.
In this article we shall see how these limitations can be overcome with very little code.


Continuation Tips in JTrees and JLists: - Most developers would have wondered why the Jtrees in java do not provide continuation tips like those provided by the native Trees.

 

 

Continuation tips are different from tooltips in that the continuation tips are displayed instantly when the mouse is moved over an item and are displayed in the same place as the node in the tree. Tooltips on the other hand are displayed when the mouse is paused over a node for some time and are displayed below the mouse pointer. Moreover, continuation tips are not displayed if the item is filly visible. I searched on the net for a while on how to make view tips available for JTrees ans JLists when I finally landed on Tim Boudreau's Blog. Tim Boudreau is a developer at Java workimg for the netbeans platform and has coded viewtips for JTrees and JLists, the code for which can be found here. To provide viewtips for a JTree or JList , you just have to call.


ViewTooltips.register (theComponent);

There was a problem with that source. The Class had default access and the register and the unregister methods were also with default access. So, I modified the source (made the class and the above two methods public) and tried providing my trees with the view tips. They Worked. Also, the original view tips gave NullPointerException when there was no horizontal scrollbar.

Test Code for testing the Modified View Tips in a JList


DefaultListModel model = new DefaultListModel();

model.add(0,"Firstttttttttttttttt Itemmmmmmmmmmmmm");

model.add(1,"Seconddddddddddddddd Itemmmmmmmmmmmmm");

model.add(2,"Thirdddddddddddddddd Itemmmmmmmmmmmmm");

JList myList = new JList(model);

// Register the Component with ViewTips

ViewTooltips.register(myList);

add(new JScrollPane(myList));

super.init();

 

 

Test Code for testing the Modified View Tips in a JTree

DefaultMutableTreeNode root = new DefaultMutableTreeNode("The Roooooooooooooooooooooot");

DefaultMutableTreeNode child1 = new DefaultMutableTreeNode("Firrrrrrrrrrrst Chiiiiiild");

DefaultMutableTreeNode child2 = new DefaultMutableTreeNode("Seconddddddddddddddd Chiiiiiild");

DefaultMutableTreeNode child3 = new DefaultMutableTreeNode("Thirdddddddddddddddd Chiiiiiild");

root.add(child1);

child1.add(child2);

child2.add(child3);

JTree myTree = new JTree(root);

// Register the Component with ViewTips

ViewTooltips.register(myTree);

add(new JScrollPane(myTree));

 

 

 

You can also provide view tips for items in a combobox. Just define your own combobox UI as given in the below code.

public class MyComboUI extends BasicComboBoxUI
{
protected ComboPopup createPopup()
{
return new MyComboPopup(comboBox);
}
}
private class MyComboPopup extends BasicComboPopup
{
public MyComboPopup(JComboBox combo)
{
super(combo);
// Register the Component with ViewTips
ViewTooltips.register(list);
}
}
 

Then set this MyComboUI as the UI for your combo box.

 

myCombo.setUI(new MyComboUI());

 

 

 


Alternatively you can use the FlexiComboBox provided in the downloads section at the end of the article which provides viewtips, horizontal scrolling and a wider popup.

 

 

Horizontal Scrollbar in a JCombobox: - The problem with JComboboxes is that the popup is only as wide as the combo itself. If the combo has some entries that are very long, those entries will not be completely visible. There are two ways to make such long items visible. We can either make the popup horizontally scrollable or we can make the popup wider than the combo box. Again, you have to write your own combobox UI ,override the createScroller() method in the BasicComboPopup class and return a new JscrollPane with both horizontal and vartical scrollbars.

 

public class ScrollComboUI extends BasicComboBoxUI
{
protected ComboPopup createPopup()
{
return new ScrollComboPopup(comboBox);
}
}
private class ScrollComboPopup extends BasicComboPopup
{
public ScrollComboPopup(JComboBox combo)
{
super(combo);
ViewTooltips.register(list);
}
protected JScrollPane createScroller()
{
return new JScrollPane(list,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
}
}

Then set this ScrollComboUI as the UI for your combo box.

 

myCombo.setUI(new ScrollComboUI());

 

Alternatively you can use the FlexiComboBox provided in the downloads section at the end of the article which provides viewtips, horizontal scrolling and a wider popup.

Wider Combo Box Popup: - You can also make the popup of your combo box wider by writing your own. You should override the getSize() and the doLayout() methods in JCombobox as given below.

public Dimension getSize()
{
Dimension dim = super.getSize();
if (!layingOut&&popupWidth!=0)
dim.width = popupWidth;
return dim;
}
public void doLayout()
{
try
{
layingOut = true;
super.doLayout();
} finally
{
layingOut = false;
}
}

 
 

 

Alternatively you can use the FlexiComboBox provided in the downloads section at the end of the article which provides viewtips, horizontal scrolling and a wider popup.
That’s it in this part of the article. Resizable and searchable combobox will be explained in the second part.

Flexi Combo:- I have developed a library called Flexi Combo with all the above features integrating the ViewTips code corrected by me, FlexiC ombo Library Can be Downloaded here.

Binaries

Source

Java Doc

 

 

 

 

 



Add this page to your favorite Social Bookmarking websites
Reddit! Del.icio.us! JoomlaVote! Google! Live! Facebook! StumbleUpon! Yahoo! Free social bookmarking plugins and extensions for Joomla! websites!
Comments
Search
Collin Fagan  - Very cool     |24.14.188.xxx |2008-06-02 21:01:36
This is great stuff, you should think about contributing this code to the Java
Tutorials Community Project (https://javatutorials.dev.java.net/). I'm sure they
would be excited to have it.
Anonymous   |83.35.211.xxx |2008-06-05 11:46:47
aaaaaa
ggrrr  - yo   |78.86.161.xxx |2008-06-06 02:31:43
whaddup
test  - af   |195.200.164.xxx |2008-06-06 05:14:16
asghg
abdulla  - issues with right-to-left application   |194.170.126.xxx |2008-06-10 05:01:03
I have tested it with right-to-left JTree/JList and I found that it add one
character i.e. you will see two same characters in the border. In addition the
position of the tooltip is one pixel up (if I'm not wrong).

It is difficult for
me to modify it.
mw  - Wider Combo Box Popup   |217.6.37.xxx |2008-06-12 03:23:16
You can code your wider ComboBox-Popup without the need to specify the
width:
Code:
	@Override
public Dimension getSize() {
Dimension dim =
super.getSize();
if (!layingOut)
dim.width =
Math.max(dim.width, getPreferredSize().width);
return dim;
}
Abe  - developer   |189.140.152.xxx |2008-12-30 13:06:06
Hi guys,
I-ve a question, i'm doing a jtree based app, but I want to add some
Autofocus when mouse is on the tree functionality, like in eclipse europa, the
problem is that i've added the mouse listeners to the jtree, but it doesn't seem
to respond very well, so I was wondering at which level should I add these mouse
listeners to correctly perform foucusing.
Khoa  - Thank you   |123.26.93.xxx |2009-03-14 20:34:44
Very thank you posted this part. I having problem about popup of comboxbox.It
very good
Dave Lorde  - Nice code - a couple of issues   |91.135.11.xxx |2009-04-13 14:33:15
Very useful code - thanks

However, there is a problem in the
FlexiComboPopup that causes an assertion failure in the
ViewTooltips.unregister method when run with assertions enabled. To
fix it, the FlexiComboPopup should check that the list is registered
before deregistering. A boolean flag will do, or, better still,
ViewTooltips could be given an 'isRegistered' method.

Another
problem is that only mouse selections are handled, but it would be
nice to show the tooltips for keyboard selections too. This can be done for
JLists/Combos using a ListSelectionListener as below:
Code:
public void valueChanged(ListSelectionEvent e) {
  JComponent comp =
(JComponent) e.getSource();
  if (!(comp instanceof JList)) {

return;
  }
JList list = (JList)comp;
Point p
= list.indexToLocation(list.getSelectedIndex());
 ... carry on as in
Dave Lorde  - Oops - text and code mangled...   |91.135.11.xxx |2009-04-13 14:36:18
That last code line should read '... carry on as in the mouseMoved
method'.

I'll leave the JTree ListSelectionListener to someone else
Only registered users can write comments!

3.22 Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved."

Last Updated ( Wednesday, 11 June 2008 17:25 )
 
Bottom Ad
Your Ad Here