• Post Reply Bookmark Topic Watch Topic
  • New Topic

Stepped JComboBox  RSS feed

 
Philip Pross
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Java brother & sisters,
I'm try to understand and learn swing 1.3. I'm quite interesting in this "Stepped ComboBox" , but I having problems implementing this with a frame that are ready exist. I know I must set a new PreferredSize for the JComboBox, I've done this, but with this I'm getting errors :
C:\jdk1.4\bin\SteppedComboBoxExample.java:28: Type expected.
combo.setPreferredSize(new Dimension(50, d.height));. I think its cause of the
"new Dimension"
Here are the examples I'm working with.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class SteppedComboBoxExample extends JFrame
{
public static void main (String args[])
{
SteppedComboBoxExample f = new SteppedComboBoxExample();
}
SteppedComboBoxExample()
{
createGUI();
setSize (300, 100);
setVisible(true);//show();
}
public void createGUI()
{
String[] str = {
"A",
"abcdefghijklmnopqrstuvwxyz",
"ABCDEFGHIJKLMNOPQRSTUVWXYZAJFITMRJKFCIE943958687979694839394858",
"0123456789"};
SteppedComboBox combo = new SteppedComboBox(str);
Dimension d = combo.getPreferredSize();
combo.setPreferredSize(new Dimension(50, d.height));
combo.setPopupWidth(d.width);
getContentPane().setLayout(new FlowLayout());
combo.setBounds(250,10,50,29);
getContentPane().add(combo);
}
}

///********************************///

import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.plaf.metal.*;
import javax.swing.plaf.basic.*;
class SteppedComboBoxUI extends MetalComboBoxUI
{
protected ComboPopup createPopup()
{
BasicComboPopup popup = new BasicComboPopup( comboBox )
{
public void show()
{
Dimension popupSize = ((SteppedComboBox)comboBox).getPopupSize();
popupSize.setSize( popupSize.width,
getPopupHeightForRowCount( comboBox.getMaximumRowCount() ) );

Rectangle popupBounds = computePopupBounds( 0,
comboBox.getBounds().height, popupSize.width, popupSize.height);

scroller.setMaximumSize( popupBounds.getSize() );
scroller.setPreferredSize( popupBounds.getSize() );
scroller.setMinimumSize( popupBounds.getSize() );
list.invalidate();
int selectedIndex = comboBox.getSelectedIndex();

if ( selectedIndex == -1 )
{
list.clearSelection();
}
else
{
list.setSelectedIndex( selectedIndex );
}

list.ensureIndexIsVisible( list.getSelectedIndex() );
setLightWeightPopupEnabled( comboBox.isLightWeightPopupEnabled() );
show( comboBox, popupBounds.x, popupBounds.y );
}
};
popup.getAccessibleContext().setAccessibleParent(comboBox);

return popup;
}
}


public class SteppedComboBox extends JComboBox
{
protected int popupWidth;

public SteppedComboBox(ComboBoxModel aModel)
{
super(aModel);
setUI(new SteppedComboBoxUI());
popupWidth = 0;
}
public SteppedComboBox(final Object[] items)
{
super(items);
setUI(new SteppedComboBoxUI());
popupWidth = 0;
}

public SteppedComboBox(Vector items)
{
super(items);
setUI(new SteppedComboBoxUI());
popupWidth = 0;
}


public void setPopupWidth(int width)
{
popupWidth = width;
}

public Dimension getPopupSize()
{
Dimension size = getSize();
if (popupWidth < 1) popupWidth = size.width;
return new Dimension(popupWidth, size.height);
}
}


My question is there a way to insert my String "str" with a ActionListener , maybe with a button ???
 
Philip Pross
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
heres what I trying to do ... basically reset the combobox if I press on a button

bt.addActionListener( new ActionListener()
{
public void actionPerformed (ActionEvent e)
{
String[] str1 = {
"A",
"c",
"wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww",
"zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"};
combo.removeAllItems();

combo = new SteppedComboBox(str1);
Dimension d = combo.getPreferredSize();
combo.setPreferredSize(new Dimension(50, d.height));
combo.setPopupWidth(d.width);
}
});
Help needed please
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!