| Tips and Tricks for JTree, JList and JCombobox Part I |
| Java Concepts | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Written by Phoenix | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Monday, 02 June 2008 13:06 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
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.
![]()
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.
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. 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. 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.
Only registered users can write comments!
Powered by !JoomlaComment 3.22
3.22 Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved." |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Last Updated ( Wednesday, 11 June 2008 17:25 ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||














