Wednesday, March 12, 2008

JComboBox with Horizontal ScrollBar and Substance L&F

Sometimes we need ad Horizontal Scrollbar in JComboBox in JAVA. Due to the long text we need to display in ComboBox and we are having a limits size for same to display.

So, here is solution.

Instead of creating JComboBox now you need to create the object of following class and it will solve your problem.

Note: Here example with Substance L&F. you not need to use that if you not using Substance L&F. so remove the imports and a method named createArrowButton() from following code..


import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.plaf.basic.BasicComboBoxUI;
import javax.swing.plaf.basic.BasicComboPopup;
import javax.swing.plaf.basic.ComboPopup;

// Only for Substance L&F else no need to include following imports..
import org.jvnet.substance.SubstanceComboBoxUI;
import org.jvnet.substance.combo.SubstanceComboBoxButton;
import org.jvnet.substance.theme.SubstanceTheme;
import org.jvnet.substance.utils.SubstanceCoreUtilities;

/**
*
* @author Shirin, khushal
*/
public class HScrollJComboBox extends JComboBox {

public HScrollJComboBox() {
super();
setUI(new HScrollJComboBoxUI());
}//end of default constructor
public class HScrollJComboBoxUI extends BasicComboBoxUI {

/**
* If you are using the Substance L&F than and only than use following methods
* else your comboBox will Work fine without following method.
*
* @return JButton
*/
@Override
protected JButton createArrowButton() {

SubstanceTheme theme = comboBox.isEnabled() ? SubstanceCoreUtilities.getActiveTheme(comboBox, true) : SubstanceCoreUtilities.getDefaultTheme(comboBox, true);
return new SubstanceComboBoxButton(comboBox, SubstanceComboBoxUI.getArrowIcon(theme, SubstanceCoreUtilities.getPopupFlyoutOrientation(comboBox)));

}

/**
*
* @return ComboPopup with Horizontal Scrollbar and
* Vertical whenever it needed
*/
@Override
protected ComboPopup createPopup() {

BasicComboPopup popup = new BasicComboPopup(comboBox) {

protected JScrollPane createScroller() {
return new JScrollPane(list, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
}//end of method createScroller
};
return popup;
}//end of method createPopup
}//end of inner class myComboUI
}




5 comments:

Bruno said...

Thanks a lot this is simple, and perfect ! I used it with Netbeans and I only had to change the generated code for the creation ("custom creation") :

replaced :
jComboBoxTypeSP = new javax.swing.JComboBox();

by :
jComboBoxTypeSP = new HScrollJComboBox();


The declaration is unchanged :
private javax.swing.JComboBox jComboBoxTypeSP;

sheela said...

Great thoughts you got there, believe I may possibly try just some of it throughout my daily life.
Devops Training in Bangalore
Microsoft azure training in Bangalore
Power bi training in Chennai

Unknown said...

I believe there are many more pleasurable opportunities ahead for individuals that looked at your site.
Best Devops Training in pune
Devops Training in Bangalore
Power bi training in Chennai

priya said...

Appreciating the persistence you put into your blog and detailed information you provide
Data Science course in kalyan nagar
Data Science course in OMR
Data Science course in chennai
Data science course in velachery
Data science course in jaya nagar
Data Science interview questions and answers
Data science course in bangalore

Maneesha said...

Excellent Blog! I would like to thank for the efforts you have made in writing this post. I am hoping the same best work from you in the future as well. I wanted to thank you for this websites! Thanks for sharing. Great websites!
data analytics courses in hyderabad

Contributors