This JComboBox does not resizes itself  
Author Message
kiwi





PostPosted: 2005-5-12 20:35:00 Top

java-programmer, This JComboBox does not resizes itself Hello.

By the program below, I wanted to change the message "a" on the
JComboBox
left side into "a-changed" by clicking the "change" button.
But all I saw was "..." because the JComboBox does not resizes itself.

How can I resize the JComboBox?

Thank you in advance.
----
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;


public class ChangeItem extends JFrame {
public static void main(String[] args) {
new ChangeItem();
}

private ChangeItem() {
setSize(300, 100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container cp = getContentPane();
cp.setLayout(new FlowLayout());
Combo combo = new Combo();
cp.add(combo);
cp.add(combo.new XButton());
setVisible(true);
}

private static class Combo extends JComboBox {
private Combo() {
addItem(new Item("a"));
}

private class XButton extends JButton {
private XButton() {
super("change");
}

protected void fireActionPerformed(ActionEvent e) {
Item i = (Item) getSelectedItem();
i.displayName = i.displayName == "a-changed" ? "a"
: "a-changed";
Combo.this.repaint();
}
}
}

private static class Item {
private String displayName;

private Item(String displayName) {
this.displayName = displayName;
}

public String toString() {
return displayName;
}
}
}

 
Arnaud Berger





PostPosted: 2005-5-12 21:27:00 Top

java-programmer >> This JComboBox does not resizes itself Hi,

I modified your code a little bit, by using a custom ComboBoxModel.

You may of course put your Item objects in the Vector instead of a String.

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;

public class ChangeItem extends JFrame {

public static void main(String[] args) {
new ChangeItem();
}

private ChangeItem() {
setSize(300, 100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container cp = getContentPane();
cp.setLayout(new FlowLayout());
Combo combo = new Combo();
cp.add(combo);
cp.add(combo.new XButton());
setVisible(true);
}

private class Combo extends JComboBox {

private Combo() {
Vector data=new Vector();
data.add("a");
setModel(new CustomComboBoxModel(data));
}

class XButton extends JButton {

private XButton() {
super("change");
addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
int selectedIndex=getSelectedIndex();

((CustomComboBoxModel)Combo.this.getModel()).changeRow(selectedIndex,"a-chan
ged");
Combo.this.setSelectedIndex(selectedIndex);
}});

}
}
}

/* not used in the example */
private static class Item {
private String displayName;
private Item(String displayName) {
this.displayName = displayName;
}

public String toString() {
return displayName;
}
}

private class CustomComboBoxModel extends DefaultComboBoxModel{

Vector data;

public CustomComboBoxModel(Vector _data){
super(_data);
data=_data;
}

public void changeRow(int selected, String newValue){
if(this.getElementAt(selected).equals("a")){
data.remove(selected);
data.add(selected,newValue);
fireIntervalRemoved(this,selected,selected);
fireIntervalAdded(this,selected,selected);
}

}
}
}

Regards,

Arnaud
<email***@***.com> a ecrit dans le message news:
email***@***.com...
> Hello.
>
> By the program below, I wanted to change the message "a" on the
> JComboBox
> left side into "a-changed" by clicking the "change" button.
> But all I saw was "..." because the JComboBox does not resizes itself.
>
> How can I resize the JComboBox?
>
> Thank you in advance.
> ----
> import java.awt.Container;
> import java.awt.FlowLayout;
> import java.awt.event.ActionEvent;
> import javax.swing.JButton;
> import javax.swing.JComboBox;
> import javax.swing.JFrame;
>
>
> public class ChangeItem extends JFrame {
> public static void main(String[] args) {
> new ChangeItem();
> }
>
> private ChangeItem() {
> setSize(300, 100);
> setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
> Container cp = getContentPane();
> cp.setLayout(new FlowLayout());
> Combo combo = new Combo();
> cp.add(combo);
> cp.add(combo.new XButton());
> setVisible(true);
> }
>
> private static class Combo extends JComboBox {
> private Combo() {
> addItem(new Item("a"));
> }
>
> private class XButton extends JButton {
> private XButton() {
> super("change");
> }
>
> protected void fireActionPerformed(ActionEvent e) {
> Item i = (Item) getSelectedItem();
> i.displayName = i.displayName == "a-changed" ? "a"
> : "a-changed";
> Combo.this.repaint();
> }
> }
> }
>
> private static class Item {
> private String displayName;
>
> private Item(String displayName) {
> this.displayName = displayName;
> }
>
> public String toString() {
> return displayName;
> }
> }
> }
>