Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Controlling the scroll pane in JComboBox

Reply
Thread Tools

Controlling the scroll pane in JComboBox

 
 
Aaron Fude
Guest
Posts: n/a
 
      06-10-2008
Hi,

I'm having all sorts of problems controlling dimensions in Swing with
LayoutManagers. (Is there an article somewhere about this?)

Here's an example. I have a custom ComboBox that narrows down the
choices depending on the input. I implement ListCellRenderer, which:
if there is no input, returns a JLabel; if there is input, it returns
a panel with three labels with the middle one "highlighting" the
selection. You can see the demo here:

file:///C:/pg/InstantDemo/Projects/ComboBox.html

Question: How come the scroll bars appear for the labels but not for
the panels?

The panel uses a BoxLayout.

If you need more information, I'm happy to provide it.

Thanks!
 
Reply With Quote
 
 
 
 
Aaron Fude
Guest
Posts: n/a
 
      06-10-2008
My bad:

http://freeboundaries.com/ComboBox.html
 
Reply With Quote
 
 
 
 
Andrew Thompson
Guest
Posts: n/a
 
      06-10-2008
On Jun 10, 2:26*pm, Aaron Fude <(E-Mail Removed)> wrote:
> Hi,
>
> I'm having all sorts of problems controlling dimensions in Swing with
> LayoutManagers. (Is there an article somewhere about this?)


<http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html>

--
Andrew T.
 
Reply With Quote
 
Aaron Fude
Guest
Posts: n/a
 
      06-10-2008
Good suggestion, Pete! The code is below.

I welcome comments about all aspects of my code, but for now I'm
mostly interested in why there are no scroll bars when there is a
query and a panel is used for rendering...

Many thanks in advance,

Aaron

package pmg;

import java.awt.*;

import javax.swing.*;
import javax.swing.event.*;

import java.awt.event.*;
import java.util.ArrayList;

public class ChoiceGui extends JComboBox {
private ArrayList<String> myList = new ArrayList<String>();
private boolean mySettingItem = false;
private boolean myRespondingToChangedQuery = false;


public static void main(String[] args) {

ArrayList<String> choices = new ArrayList<String>();
choices.addAll(java.util.Arrays.asList(
"Hello;Hello, World!;A very looooooooooooooong String;Very
Slow;Bad dog, bad!".split(";")));

JFrame frame = new JFrame("Choice");
frame.getContentPane().add(new ChoiceGui(choices));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.pack();
frame.setVisible(true);
}

public ChoiceGui(ArrayList<String> choices) {

myList = choices;

setEditor(new Editor());
setRenderer(new Renderer3());
setBackground(Color.getHSBColor(1f/6, .2f, 1f));

this.setPreferredSize(new Dimension(150, 22));

for (int s = 0; s < myList.size(); s++)
this.addItem(myList.get(s));

this.setSelectedIndex(-1);

this.setEditable(true); // This is when the first setItem() is
called!
}


public class Editor implements ComboBoxEditor {
private JTextField myTextField = new JTextField();

public void addActionListener(ActionListener l)
{ myTextField.addActionListener(l); }
public void removeActionListener(ActionListener l) { }
public void setItem(Object item) {
if (myRespondingToChangedQuery)
return;

System.out.println("setItem called!!!");
mySettingItem = true;
if (item != null) {
myTextField.setText(item.toString());
myTextField.setCaretPosition(0);
}
else if (!myRespondingToChangedQuery) // The value of
SuperChoice changed for a different reason, so the query must be
reset.
myTextField.setText("");

mySettingItem = false;
}
public void selectAll() { myTextField.selectAll(); }
public Object getItem() { return null; }
public Component getEditorComponent() { return myTextField; }


public Editor() {
myTextField.getDocument().addDocumentListener(new
DocumentListener() {
public void dodo() {

javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
myRespondingToChangedQuery = true;

String q =
((JTextField)getEditor().getEditorComponent()).get Text();
removeAllItems();
for (int i = 0; i < myList.size(); i++)
if
(myList.get(i).toUpperCase().contains(q.toUpperCas e()))
addItem(myList.get(i));

setPopupVisible(true);

myRespondingToChangedQuery = false;

}
});
}

public void changedUpdate(DocumentEvent e) {
if (mySettingItem)
return;
dodo();
}
public void insertUpdate(DocumentEvent e) {
if (mySettingItem)
return;
dodo();
}
public void removeUpdate(DocumentEvent e) {
if (mySettingItem)
return;

dodo();
}
});

}
}
class Renderer3 implements ListCellRenderer {
JPanel panel = new JPanel();
JLabel label = new JLabel();
JLabel label1 = new JLabel();
JLabel label2 = new JLabel();
JLabel label3 = new JLabel();

public Renderer3() {
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));

panel.add(label1);
panel.add(label2);
panel.add(label3);

panel.setOpaque(true);
panel.setBackground(Color.RED);
Font font = label1.getFont();
label.setFont(new Font(font.getName(), Font.PLAIN,
font.getSize()-2));
label1.setFont(new Font(font.getName(), Font.PLAIN,
font.getSize()));
label2.setFont(new Font(font.getName(), Font.BOLD,
font.getSize()));
label3.setFont(new Font(font.getName(), Font.PLAIN,
font.getSize()));

label.setOpaque(true);
panel.setBackground(Color.getHSBColor(1f/6, .2f, 1f));
}

public Component getListCellRendererComponent(JList list, Object
value, int index, boolean isSelected, boolean cellHasFocus) {
String str = (value!=null)?value.toString():"null";
String q =
((JTextField)getEditor().getEditorComponent()).get Text();
if (q.length() != 0 &&
str.toUpperCase().indexOf(q.toUpperCase()) != -1) {
int start = str.toUpperCase().indexOf(q.toUpperCase());
label1.setText(str.substring(0, start));
label2.setText(str.substring(start, start + q.length()));
label3.setText(str.substring(start + q.length()));
panel.setBackground(isSelected?
list.getSelectionBackground():list.getBackground() );
panel.setForeground(isSelected?
list.getSelectionForeground():list.getForeground() );

return panel;
}
else {
Font font = label.getFont();
label.setFont(new Font("Dialog", Font.PLAIN, 12));
label.setBackground(isSelected?
list.getSelectionBackground():list.getBackground() );
label.setForeground(isSelected?
list.getSelectionForeground():list.getForeground() );
label.setText(" " + str);
return label;
}

}
}

}
 
Reply With Quote
 
cherryx@gmail.com
Guest
Posts: n/a
 
      06-11-2008

Use the setUI() method
and an extended BasicComboBoxUI class.

also import javax.swing.plaf.basic.*;

regards

Chinthaka Weerasinghe - Your friendly java tutor
(E-Mail Removed)



package pmg;

import java.awt.*;

import javax.swing.*;
import javax.swing.event.*;

import java.awt.event.*;
import java.util.ArrayList;

import javax.swing.plaf.basic.*;

public class ChoiceGui extends JComboBox {
private ArrayList<String> myList = new ArrayList<String>();
private boolean mySettingItem = false;
private boolean myRespondingToChangedQuery = false;

public static void main(String[] args) {

ArrayList<String> choices = new ArrayList<String>();
choices.addAll(java.util.Arrays.asList("Hello;Hell o, World!;A very
looooooooooooooong String;Very Slow;Bad dog, bad!".split(";")));

JFrame frame = new JFrame("Choice");
frame.getContentPane().add(new ChoiceGui(choices));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.pack();
frame.setVisible(true);
}

public ChoiceGui(ArrayList<String> choices) {

myList = choices;

setEditor(new Editor());
setRenderer(new Renderer3());
setBackground(Color.getHSBColor(1f/6, .2f, 1f));

this.setPreferredSize(new Dimension(150, 22));

for (int s = 0; s < myList.size(); s++)
this.addItem(myList.get(s));

this.setSelectedIndex(-1);

this.setEditable(true); // This is when the first setItem() is
called!
setUI(new myComboUI());
}

public class myComboUI extends BasicComboBoxUI{
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



public class Editor implements ComboBoxEditor {
private JTextField myTextField = new JTextField();

public void addActionListener(ActionListener l)
{ myTextField.addActionListener(l); }
public void removeActionListener(ActionListener l) { }
public void setItem(Object item) {
if (myRespondingToChangedQuery)
return;

System.out.println("setItem called!!!");
mySettingItem = true;
if (item != null) {
myTextField.setText(item.toString());
myTextField.setCaretPosition(0);
}
else if (!myRespondingToChangedQuery) // The value of SuperChoice
changed
// for a different reason, so the
// query must be reset.
myTextField.setText("");

mySettingItem = false;
}
public void selectAll() { myTextField.selectAll(); }
public Object getItem() { return null; }
public Component getEditorComponent() { return myTextField; }

public Editor() {
myTextField.getDocument().addDocumentListener(new
DocumentListener() {
public void dodo() {

javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
myRespondingToChangedQuery = true;

String q =
((JTextField)getEditor().getEditorComponent()).get Text();
removeAllItems();
for (int i = 0; i < myList.size(); i++)
if (myList.get(i).toUpperCase().contains(q.toUpperCas e()))
addItem(myList.get(i));

setPopupVisible(true);

myRespondingToChangedQuery = false;

}
});
}

public void changedUpdate(DocumentEvent e) {
if (mySettingItem)
return;
dodo();
}
public void insertUpdate(DocumentEvent e) {
if (mySettingItem)
return;
dodo();
}
public void removeUpdate(DocumentEvent e) {
if (mySettingItem)
return;

dodo();
}
});

}
}
class Renderer3 implements ListCellRenderer {
JPanel panel = new JPanel();
JLabel label = new JLabel();
JLabel label1 = new JLabel();
JLabel label2 = new JLabel();
JLabel label3 = new JLabel();

public Renderer3() {
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));

panel.add(label1);
panel.add(label2);
panel.add(label3);

panel.setOpaque(true);
panel.setBackground(Color.RED);
Font font = label1.getFont();
label.setFont(new Font(font.getName(),
Font.PLAIN,font.getSize()-2));
label1.setFont(new Font(font.getName(),
Font.PLAIN,font.getSize()));
label2.setFont(new Font(font.getName(), Font.BOLD,font.getSize()));
label3.setFont(new Font(font.getName(),
Font.PLAIN,font.getSize()));
label.setOpaque(true);
panel.setBackground(Color.getHSBColor(1f/6, .2f, 1f));
}

public Component getListCellRendererComponent(JList list, Object
value, int index, boolean isSelected, boolean cellHasFocus) {
String str = (value!=null)?value.toString():"null";
String q =
((JTextField)getEditor().getEditorComponent()).get Text();
if (q.length() != 0 && str.toUpperCase().indexOf(q.toUpperCase()) !
= -1) {
int start = str.toUpperCase().indexOf(q.toUpperCase());
label1.setText(str.substring(0, start));
label2.setText(str.substring(start, start + q.length()));
label3.setText(str.substring(start + q.length()));
panel.setBackground(isSelected?
list.getSelectionBackground():list.getBackground() );
panel.setForeground(isSelected?
list.getSelectionForeground():list.getForeground() );

return panel;
}
else {
Font font = label.getFont();
label.setFont(new Font("Dialog", Font.PLAIN, 12));
label.setBackground(isSelected?
list.getSelectionBackground():list.getBackground() );
label.setForeground(isSelected?
list.getSelectionForeground():list.getForeground() );
label.setText(" " + str);
return label;
}
}
}
}

On Jun 10, 9:26 am, Aaron Fude <(E-Mail Removed)> wrote:
> Hi,
>
> I'm having all sorts of problems controlling dimensions in Swing with
> LayoutManagers. (Is there an article somewhere about this?)
>

 
Reply With Quote
 
Aaron Fude
Guest
Posts: n/a
 
      06-12-2008
Thanks!

But can you explain why it makes a difference?
Why would, in the original solution, the scroll bars not appear for
JPanels but appear for the labels?

Also, how do I control the numbers of rows that is shown in the popup?

Thanks!
 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      06-29-2008
On Jun 10, 2:26=A0pm, Aaron Fude <(E-Mail Removed)> wrote:
> Hi,
>
> I'm having all sorts of problems controlling dimensions in Swing with
> LayoutManagers. (Is there an article somewhere about this?)



--
Patty T.


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"This is Preservation Month.
I appreciate preservation.
It's what you do when you run for president.
You gotta preserve."

--- Adolph Bush,
Speaking during "Perseverance Month"
at Fairgrounds Elementary School in Nashua, N.H.
As quoted in the Los Angeles Times, Jan. 28, 2000

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
can't view chm files Error(nothing is displayed on the right pane after clicking the left pane) Patrick.O.Ige ASP .Net 0 08-31-2006 04:56 AM
Controlling JComboBox size in JTable cell Yulia Java 1 07-04-2005 10:29 AM
change the scroll speed of a scroll pane? Kevin Java 1 02-05-2005 09:42 AM
adding label to panel in scroll pane memememe Java 1 07-11-2003 02:05 PM



Advertisments