I have 10 JSliders, 10 JLabels and 10 JToggleButtons. When the user drag a slider, i need to update its corresponding label and move to the same value all the sliders with the corresponding button toggled. Now, my problem is not how to do this, but instead how to have this all in the same event.
我有10个JSliders,10个JLabel和10个JToggleButtons。当用户拖动滑块时,我需要更新其相应的标签并移动到相同的值,所有滑块都会切换相应的按钮。现在,我的问题不是如何做到这一点,而是如何在同一事件中拥有这一切。
In my good ol' VB days ( ;) ) i'd just have to assing the sliders with the same name, and so the buttons and the labels, and i'd have automagically arrays of sliders, labels and buttons accessible by the index, all firing the same event.
在我很好的'VB天(;)中,我只需要使用相同的名称来分配滑块,所以按钮和标签,以及我可以通过索引访问滑块,标签和按钮的自动数组,所有射击同一事件。
In my current C++Builder days, i have to set the Tag property (a generic int property) for each of them, select all the sliders and declare the same event for them all, then get back the tag from the sender object and use it as an index. I'll then declare an array of Labels and Buttons pointers, so i can access the control by the index.
在我当前的C ++ Builder中,我必须为每个人设置Tag属性(一个通用的int属性),选择所有的滑块并为它们声明相同的事件,然后从发送者对象中取回标签,用它作为索引。然后我将声明一个标签和按钮指针数组,所以我可以通过索引访问控件。
In Java, how can i set the same event for all my sliders in the IDE (i use NetBeans)? And how can i access the labels and the togglebuttons as an array?
在Java中,我如何为IDE中的所有滑块设置相同的事件(我使用NetBeans)?我如何作为一个数组访问标签和togglebuttons?
I've seens some example but they all are building controls at runtime, i want to design my form this way, instead.
我已经看到了一些例子,但他们都在运行时构建控件,我想以这种方式设计我的表单。
Thanks
2 个解决方案
#1
1
OK, so really fast, here is roughly what I had in mind (can be very much improved, used some shortcuts to do it faster):
好的,非常快,这里大致是我的想法(可以大大改进,使用一些快捷方式来更快地完成):
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.AbstractButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class TestMVC {
public static class ModelObject {
private PropertyChangeSupport pcs;
public ModelObject() {
pcs = new PropertyChangeSupport(this);
}
public PropertyChangeSupport getPropertyChangeSupport() {
return pcs;
}
}
public static class Model extends ModelObject {
private List<Item> items;
public Model() {
items = new ArrayList<TestMVC.Item>();
}
public List<Item> getItems() {
return items;
}
public void addToItems(Item item) {
item.setModel(this);
items.add(item);
getPropertyChangeSupport().firePropertyChange("item", null, item);
}
public void removeFromItems(Item item) {
items.add(item);
item.setModel(null);
getPropertyChangeSupport().firePropertyChange("item", item, null);
}
}
public static class Item extends ModelObject {
public static final String VALUE = "value";
public static final String LABEL = "label";
public static final String ENABLED = "enabled";
private Model model;
private boolean enabled;
private String label;
private int value;
public Item() {
}
public Item(boolean enabled, String label, int value) {
super();
this.enabled = enabled;
this.label = label;
this.value = value;
}
public Model getModel() {
return model;
}
public void setModel(Model model) {
this.model = model;
}
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
boolean old = this.enabled;
this.enabled = enabled;
getPropertyChangeSupport().firePropertyChange(ENABLED, old, enabled);
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
String old = this.label;
this.label = label;
getPropertyChangeSupport().firePropertyChange(LABEL, old, enabled);
}
public int getValue() {
return value;
}
public void setValue(int value) {
_internalSetValue(value);
if (this.isEnabled()) {
for (Item item : model.getItems()) {
if (item != this && item.isEnabled()) {
item._internalSetValue(value);
}
}
}
}
private void _internalSetValue(int value) {
int old = this.value;
this.value = value;
getPropertyChangeSupport().firePropertyChange(VALUE, old, value);
}
}
public static class ItemView implements PropertyChangeListener {
private Item item;
private JLabel label;
private JSlider slider;
private AbstractButton button;
public ItemView(Item item) {
super();
this.label = new JLabel(item.getLabel());
this.slider = new JSlider(0, 100);
this.slider.setValue(item.getValue());
this.slider.setEnabled(item.isEnabled());
this.button = new JCheckBox((String) null, item.isEnabled());
this.item = item;
item.getPropertyChangeSupport().addPropertyChangeListener(this);
}
public JLabel getLabel() {
return label;
}
public JSlider getSlider() {
return slider;
}
public AbstractButton getButton() {
return button;
}
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals(Item.LABEL)) {
label.setText(item.getLabel());
} else if (evt.getPropertyName().equals(Item.ENABLED)) {
button.setSelected(item.isEnabled());
slider.setEnabled(item.isEnabled());
} else if (evt.getPropertyName().equals(Item.VALUE)) {
slider.setValue(item.getValue());
}
}
}
private Model model;
public TestMVC(Model model) {
super();
this.model = model;
}
protected void initUI() {
JFrame frame = new JFrame(TestMVC.class.getSimpleName());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc1 = new GridBagConstraints();
GridBagConstraints gbc3 = new GridBagConstraints();
gbc3.weightx = 1.0;
gbc3.fill = GridBagConstraints.HORIZONTAL;
gbc3.gridwidth = GridBagConstraints.REMAINDER;
for (final Item item : model.getItems()) {
final ItemView view = new ItemView(item);
view.getButton().addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
item.setEnabled(view.getButton().isSelected());
}
});
view.getSlider().addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
item.setValue(view.getSlider().getValue());
}
});
panel.add(view.getButton(), gbc1);
panel.add(view.getLabel(), gbc1);
panel.add(view.getSlider(), gbc3);
}
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
private static Model loadModel() {
Random random = new Random();
Model model = new Model();
for (int i = 0; i < 10; i++) {
Item item = new Item(random.nextBoolean(), "Some label " + random.nextLong(), random.nextInt(100));
model.addToItems(item);
}
return model;
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Model model = loadModel();
new TestMVC(model).initUI();
}
});
}
}
#2
1
Add a ChangeListener
to each of your sliders. The listener should have a collection of all the other sliders it needs to update.
为每个滑块添加ChangeListener。监听器应该包含需要更新的所有其他滑块的集合。
Something along these lines;
沿着这些方向的东西;
public void stateChanged(ChangeEvent e) {
JSlider source = (JSlider)e.getSource();
int val = (int)source.getValue();
for (JSlider slider : otherSliders) {
slider.setValue(val);
}
}
#1
1
OK, so really fast, here is roughly what I had in mind (can be very much improved, used some shortcuts to do it faster):
好的,非常快,这里大致是我的想法(可以大大改进,使用一些快捷方式来更快地完成):
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.AbstractButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class TestMVC {
public static class ModelObject {
private PropertyChangeSupport pcs;
public ModelObject() {
pcs = new PropertyChangeSupport(this);
}
public PropertyChangeSupport getPropertyChangeSupport() {
return pcs;
}
}
public static class Model extends ModelObject {
private List<Item> items;
public Model() {
items = new ArrayList<TestMVC.Item>();
}
public List<Item> getItems() {
return items;
}
public void addToItems(Item item) {
item.setModel(this);
items.add(item);
getPropertyChangeSupport().firePropertyChange("item", null, item);
}
public void removeFromItems(Item item) {
items.add(item);
item.setModel(null);
getPropertyChangeSupport().firePropertyChange("item", item, null);
}
}
public static class Item extends ModelObject {
public static final String VALUE = "value";
public static final String LABEL = "label";
public static final String ENABLED = "enabled";
private Model model;
private boolean enabled;
private String label;
private int value;
public Item() {
}
public Item(boolean enabled, String label, int value) {
super();
this.enabled = enabled;
this.label = label;
this.value = value;
}
public Model getModel() {
return model;
}
public void setModel(Model model) {
this.model = model;
}
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
boolean old = this.enabled;
this.enabled = enabled;
getPropertyChangeSupport().firePropertyChange(ENABLED, old, enabled);
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
String old = this.label;
this.label = label;
getPropertyChangeSupport().firePropertyChange(LABEL, old, enabled);
}
public int getValue() {
return value;
}
public void setValue(int value) {
_internalSetValue(value);
if (this.isEnabled()) {
for (Item item : model.getItems()) {
if (item != this && item.isEnabled()) {
item._internalSetValue(value);
}
}
}
}
private void _internalSetValue(int value) {
int old = this.value;
this.value = value;
getPropertyChangeSupport().firePropertyChange(VALUE, old, value);
}
}
public static class ItemView implements PropertyChangeListener {
private Item item;
private JLabel label;
private JSlider slider;
private AbstractButton button;
public ItemView(Item item) {
super();
this.label = new JLabel(item.getLabel());
this.slider = new JSlider(0, 100);
this.slider.setValue(item.getValue());
this.slider.setEnabled(item.isEnabled());
this.button = new JCheckBox((String) null, item.isEnabled());
this.item = item;
item.getPropertyChangeSupport().addPropertyChangeListener(this);
}
public JLabel getLabel() {
return label;
}
public JSlider getSlider() {
return slider;
}
public AbstractButton getButton() {
return button;
}
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals(Item.LABEL)) {
label.setText(item.getLabel());
} else if (evt.getPropertyName().equals(Item.ENABLED)) {
button.setSelected(item.isEnabled());
slider.setEnabled(item.isEnabled());
} else if (evt.getPropertyName().equals(Item.VALUE)) {
slider.setValue(item.getValue());
}
}
}
private Model model;
public TestMVC(Model model) {
super();
this.model = model;
}
protected void initUI() {
JFrame frame = new JFrame(TestMVC.class.getSimpleName());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc1 = new GridBagConstraints();
GridBagConstraints gbc3 = new GridBagConstraints();
gbc3.weightx = 1.0;
gbc3.fill = GridBagConstraints.HORIZONTAL;
gbc3.gridwidth = GridBagConstraints.REMAINDER;
for (final Item item : model.getItems()) {
final ItemView view = new ItemView(item);
view.getButton().addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
item.setEnabled(view.getButton().isSelected());
}
});
view.getSlider().addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
item.setValue(view.getSlider().getValue());
}
});
panel.add(view.getButton(), gbc1);
panel.add(view.getLabel(), gbc1);
panel.add(view.getSlider(), gbc3);
}
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
private static Model loadModel() {
Random random = new Random();
Model model = new Model();
for (int i = 0; i < 10; i++) {
Item item = new Item(random.nextBoolean(), "Some label " + random.nextLong(), random.nextInt(100));
model.addToItems(item);
}
return model;
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Model model = loadModel();
new TestMVC(model).initUI();
}
});
}
}
#2
1
Add a ChangeListener
to each of your sliders. The listener should have a collection of all the other sliders it needs to update.
为每个滑块添加ChangeListener。监听器应该包含需要更新的所有其他滑块的集合。
Something along these lines;
沿着这些方向的东西;
public void stateChanged(ChangeEvent e) {
JSlider source = (JSlider)e.getSource();
int val = (int)source.getValue();
for (JSlider slider : otherSliders) {
slider.setValue(val);
}
}