Short:
Is possible to set an unknown value on a JSlider having no knob?
Long:
I'm working on a project that has a desktop client developed in Java Swing where the user is required to measure some parameters using a slider. That's a requirement, it has to be a slider.
Until the user interacts with the slider the value has to be unknown, and that means showing no knob. That's a requirement too.
When using a JSlider the knob shows always and is no way to set any value out of its bounds or set it to null as it uses the primitive type int and not the object Integer.
Is there a way to set it a null fail-value or at least some value that shows no knob? Is there some extension that would allow to do so?
Make your own implementation of BasicSliderUI and override paintThumb(Graphics g) to do what you require.
Also take a look here: How to hide the knob of jSlider?
With the great help of Harry Joy about hiding the knob, finally I've been able to solve the problem.
If you check Harry's answer you can read about overriding the method BasicSliderUI.paintThumb(Graphics) for hiding the knob. It works fine on most L&F's not based on Synth (and that means Nimbus), where the approach would be different, but doable via customizations on the L&F.
Installing it is a bit tricky: having a PropertyChangeListener on the UIManager that checks any change on the L&F and installs a proper UI delegate on the component does the magic (in this solution I'm just showing the one based in BasicSliderUI the others are copy-pastes). Also I've tweaked it a bit to make it set the value to the position where is first clicked.
For the delegate to know if it must paint the knob or not I decided having a client property on the JSlider called "ready", which is to be a boolean. This way, the delegate can be installed on any JSlider, not only the one extended.
Now, how to manage unknown values? Adding another new property "selectedValue", this one is bound to both "value" and "ready" and of Integer type. "ready" is false or true depending on if it's null or not, and viceversa, and both "value" and "selectedValue" hold the same value (one being int and the other Integer) unless not ready, which would set the "selectedValue" to null. It might sound complicated, but it is not.
Also there's a little tweak for clearing the value with [Esc] and propagating properties in the component implemented.
Here's the code:
BasicSliderUIExt
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import javax.swing.JSlider;
import javax.swing.SwingConstants;
import javax.swing.plaf.basic.BasicSliderUI;
public class BasicSliderUIExt extends BasicSliderUI {
public BasicSliderUIExt(JSlider slider) {
super(slider);
}
#Override
public void paintThumb(Graphics g) {
if (isReady(super.slider)) {
super.paintThumb(g);
}
}
#Override
protected TrackListener createTrackListener(final JSlider slider) {
return new TrackListener() {
#Override
public void mousePressed(MouseEvent event) {
if (isReady(slider)) {
super.mousePressed(event);
} else {
JSlider slider = (JSlider) event.getSource();
switch (slider.getOrientation()) {
case SwingConstants.VERTICAL:
slider.setValue(valueForYPosition(event.getY()));
break;
case SwingConstants.HORIZONTAL:
slider.setValue(valueForXPosition(event.getX()));
break;
}
super.mousePressed(event);
super.mouseDragged(event);
}
}
#Override
public boolean shouldScroll(int direction) {
if (isReady(slider)) {
return super.shouldScroll(direction);
}
return false;
}};
}
public static boolean isReady(JSlider slider) {
Object ready = slider.getClientProperty(JSliderExt.READY_PROPERTY);
return (ready == null) || (!(ready instanceof Boolean)) || (((Boolean) ready).booleanValue());
}
}
JSliderExt
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.lang.reflect.Constructor;
import javax.swing.BoundedRangeModel;
import javax.swing.JSlider;
import javax.swing.LookAndFeel;
import javax.swing.UIManager;
import javax.swing.plaf.SliderUI;
public class JSliderExt extends JSlider {
private static final long serialVersionUID = 1L;
public static final String EXTENT_PROPERTY = "extent";
public static final String MAXIMUM_PROPERTY = "maximum";
public static final String MINIMUM_PROPERTY = "minimum";
public static final String READY_PROPERTY = "ready";
public static final String SELECTED_VALUE_PROPERTY = "selectedValue";
public static final String VALUE_PROPERTY = "value";
public static final boolean READY_DEFAULT_VALUE = false;
protected SliderUI uix = new BasicSliderUIExt(this);
public JSliderExt(BoundedRangeModel model, boolean ready) {
super(model);
init(ready);
}
public JSliderExt(BoundedRangeModel model) {
super(model);
init(READY_DEFAULT_VALUE);
}
public JSliderExt(int orientation, int minimmum, int maximum, int value, boolean ready) {
super(orientation, minimmum, maximum, value);
init(ready);
}
public JSliderExt(int orientation, int minimmum, int maximum, int value) {
super(orientation, minimmum, maximum, value);
init(READY_DEFAULT_VALUE);
}
public JSliderExt(int minimmum, int maximum, int value, boolean ready) {
super(minimmum, maximum, value);
init(ready);
}
public JSliderExt(int minimmum, int maximum, int value) {
super(minimmum, maximum, value);
init(READY_DEFAULT_VALUE);
}
public JSliderExt(int minimmum, int maximum, boolean ready) {
super(minimmum, maximum);
init(ready);
}
public JSliderExt(int minimmum, int maximum) {
super(minimmum, maximum);
init(READY_DEFAULT_VALUE);
}
public JSliderExt(int orientation, boolean ready) {
super(orientation);
init(ready);
}
public JSliderExt(int orientation) {
super(orientation);
init(READY_DEFAULT_VALUE);
}
public JSliderExt(boolean ready) {
super();
init(ready);
}
public JSliderExt() {
super();
init(READY_DEFAULT_VALUE);
}
private void init(boolean ready) {
UIManager.addPropertyChangeListener(new PropertyChangeListener() { // Changes the UI delegate in L&F change
#Override
public void propertyChange(PropertyChangeEvent event) {
if ("lookAndFeel".equals(event.getPropertyName())) {
Object newValue = event.getNewValue();
if ((newValue != null) && (newValue instanceof LookAndFeel)) {
LookAndFeel lookAndFeel = (LookAndFeel) newValue;
try {
if (lookAndFeel instanceof MetalLookAndFeel) {
JSliderExt.this.uix = new MetalSliderUIExt();
} else if (lookAndFeel instanceof com.sun.java.swing.plaf.motif.MotifLookAndFeel) {
JSliderExt.this.uix = new MotifSliderUIExt(JSliderExt.this);
} else if (lookAndFeel instanceof com.sun.java.swing.plaf.windows.WindowsLookAndFeel) {
JSliderExt.this.uix = new WindowsSliderUIExt(JSliderExt.this);
} else {
throw new NullPointerException("Default Look & Feel not matched");
}
} catch (Exception e) {
try {
Package sliderPackage = JSliderExt.this.getClass().getPackage();
String lookAndFeelName = lookAndFeel.getName();
if (lookAndFeelName.equals("CDE/Motif")) {
lookAndFeelName = "Motif";
} else if (lookAndFeelName.equals("Windows Classic")) {
lookAndFeelName = "Windows";
} else if (lookAndFeelName.startsWith("JGoodies")) {
lookAndFeelName = "Basic";
}
Class<?> sliderUiType = Class.forName(sliderPackage.getName() + "." + lookAndFeelName
+ "SliderUIExt");
Constructor<?> constructor1 = null;
try {
constructor1 = sliderUiType.getConstructor(JSlider.class);
} catch (Exception e3) { // Nothing to do here
}
Constructor<?> constructor0 = null;
try {
constructor0 = sliderUiType.getConstructor();
} catch (Exception e3) { // Nothing to do here
}
Object sliderUi = null;
if (constructor1 != null) {
sliderUi = constructor1.newInstance(JSliderExt.this);
} else if (constructor0 != null) {
sliderUi = constructor0.newInstance();
}
if ((sliderUi != null) && (sliderUi instanceof SliderUI)) {
JSliderExt.this.setUI((SliderUI) sliderUi);
}
} catch (Exception e2) {
JSliderExt.this.uix = new BasicSliderUIExt(JSliderExt.this);
}
}
} else {
JSliderExt.this.uix = new BasicSliderUIExt(JSliderExt.this);
}
updateUI();
}
}});
addPropertyChangeListener(new PropertyChangeListener() {
#Override
public void propertyChange(PropertyChangeEvent event) {
String propertyName = event.getPropertyName();
if (READY_PROPERTY.equals(propertyName)) {
Object newValue = event.getNewValue();
if ((newValue == null) || (!(newValue instanceof Boolean)) || (((Boolean) newValue).booleanValue())) {
setSelectedValue(Integer.valueOf(getValue()));
} else {
setSelectedValue(null);
}
} else if (SELECTED_VALUE_PROPERTY.equals(propertyName)) {
Object newValue = event.getNewValue();
System.out.println(newValue);
if ((newValue != null) && (newValue instanceof Integer)) {
int value = getValue();
int newSelectedValue = ((Integer) newValue).intValue();
if (value != newSelectedValue) {
setValue(newSelectedValue);
}
setReady(true);
} else {
setReady(false);
}
repaint();
} else if (VALUE_PROPERTY.equals(propertyName)) {
setReady(true);
Object newValue = event.getNewValue();
if ((newValue != null) && (newValue instanceof Integer)) {
setSelectedValue((Integer) newValue);
}
}
}});
addKeyListener(new KeyAdapter() { // Enables escape key for clearing value
#Override
public void keyPressed(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.VK_ESCAPE) {
JSliderExt.this.setReady(false);
}
}});
setReady(ready);
}
#Override
public void setValue(int value) {
int oldValue = getValue();
super.setValue(value);
firePropertyChange(VALUE_PROPERTY, Integer.valueOf(oldValue), Integer.valueOf(value));
}
#Override
public void setExtent(int extent) {
int oldExtent = getExtent();
super.setExtent(extent);
firePropertyChange(EXTENT_PROPERTY, Integer.valueOf(oldExtent), Integer.valueOf(extent));
}
#Override
public void setMinimum(int minimum) {
int oldMinimum = getMinimum();
super.setMinimum(minimum);
firePropertyChange(MINIMUM_PROPERTY, Integer.valueOf(oldMinimum), Integer.valueOf(minimum));
}
#Override
public void setMaximum(int maximum) {
int oldMaximum = getMaximum();
super.setMaximum(maximum);
firePropertyChange(MAXIMUM_PROPERTY, Integer.valueOf(oldMaximum), Integer.valueOf(maximum));
}
public Integer getSelectedValue() {
return (Integer) getClientProperty(SELECTED_VALUE_PROPERTY);
}
public void setSelectedValue(Integer selectedValue) {
putClientProperty(SELECTED_VALUE_PROPERTY, selectedValue);
}
public boolean isReady() {
Object ready = getClientProperty(READY_PROPERTY);
return ((ready != null) && (ready instanceof Boolean)) ? ((Boolean) ready).booleanValue()
: READY_DEFAULT_VALUE;
}
public void setReady(boolean waiting) {
putClientProperty(READY_PROPERTY, Boolean.valueOf(waiting));
}
#Override
public void updateUI() {
setUI(this.uix);
updateLabelUIs();
}
}
Please note that using this code, might require some changes on selecting the delegates depending on your application, since this is intended for a Windows system. As said, Synth/Nimbus has to be worked in a different manner, but also any custom L&F or for OSX, needs the proper delegate to be extended and added on the listener.
Related
I have create my own SpreadsheetCellEditor to show a ColorPicker, but I don't want to return back the ColorPicker.getValue().toString, I want to return a label with
Background color of selected value. I have searched for setContentDisplay(ContentDisplay.GRAPHIC_ONLY) but it seems not exist on SpreadsheetCell.
So how I can achieve this?
Here is my implementation so far,
public class comboboxCellEditor extends SpreadsheetCellEditor {
private final ColorPicker colorPicker = new ColorPicker();
private EventHandler<KeyEvent> eh;
private ChangeListener<Color> cl;
private boolean ending = false;
public comboboxCellEditor(SpreadsheetView view) {
super(view);
}
#Override
public void startEdit(Object value) {
if (value instanceof Color) {
this.colorPicker.setValue((Color) value);
}
attachEnterEscapeEventHandler();
this.colorPicker.show();
this.colorPicker.requestFocus();
}
#Override
public Control getEditor() {
return colorPicker;
}
public String getControlValue() {
return this.colorPicker.getValue().toString();
}
#Override
public void end() {
if (this.colorPicker.isShowing()) {
this.colorPicker.hide();
}
this.colorPicker.removeEventFilter(KeyEvent.KEY_PRESSED, this.eh);
this.colorPicker.valueProperty().removeListener(this.cl);
}
private void attachEnterEscapeEventHandler() {
this.eh =
new EventHandler<KeyEvent>() {
public void handle(KeyEvent t) {
if (t.getCode() == KeyCode.ENTER) {
comboboxCellEditor.this.ending = true;
comboboxCellEditor.this.endEdit(true);
comboboxCellEditor.this.ending = false;
} else if (t.getCode() == KeyCode.ESCAPE) {
comboboxCellEditor.this.endEdit(false);
}
}
};
this.colorPicker.addEventFilter(KeyEvent.KEY_PRESSED, this.eh);
this.cl = new ChangeListener<Color>() {
public void changed(ObservableValue<? extends Color> observable, Color oldValue, Color newValue) {
if (!comboboxCellEditor.this.ending)
comboboxCellEditor.this.endEdit(true);
}
};
this.colorPicker.valueProperty().addListener(this.cl);
}
}
public class SpreadSheetComboboxCellType extends SpreadsheetCellType<Color> {
#Override
public SpreadsheetCellEditor createEditor(SpreadsheetView spreadsheetView) {
return new comboboxCellEditor(spreadsheetView);
}
#Override
public String toString(Color color) {
return color.toString();
}
#Override
public boolean match(Object o) {
return true;
}
#Override
public Color convertValue(Object o) {
if (o instanceof Color)
return (Color) o;
else {
return Color.valueOf((String) o);
}
}
public SpreadsheetCell createCell(int row, int column, int rowSpan, int columnSpan, Color value) {
SpreadsheetCellBase cell = new SpreadsheetCellBase(row, column, rowSpan, columnSpan, this);
cell.setItem(value);
Label label = new Label();
label.setGraphic(null);
label.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
cell.setGraphic(label);
return cell;
}
}
The SpreadsheetCell has an updateText method each time you modify the item. Therefore it's meant to display text first.
You have several options.
The easiest one is to add a listener to the cell and modify your graphic at that moment :
The text shown in the cell will be the result of the conversion of your SpreadsheetCellType. Since you don't want text, return nothing in your SpreadSheetComboboxCellType :
#Override
public String toString(Color color) {
return "";
}
Then simply add a Listener on your SpreadsheetCell and modify the graphic :
cell.itemProperty().addListener(new ChangeListener<Object>() {
#Override
public void changed(ObservableValue<? extends Object> ov, Object t, Object newValue) {
if (newValue != null && newValue instanceof Color ) {
//Do something
cell.setGraphic(My_Graphic_Object);
} else {
cell.setGraphic(null);
}
}
});
This should do the trick. Now you can implement your own SpreadsheetCell in order to have a total control on the display. It will be more efficient. Basically copy all the code for SpreadsheetCellBase (here : https://bitbucket.org/controlsfx/controlsfx/src/755c59605e623476ff0a6c860e2c218488776aec/controlsfx/src/main/java/org/controlsfx/control/spreadsheet/SpreadsheetCellBase.java?at=default&fileviewer=file-view-default )
And simply modify the updateText method :
private void updateText() {
if(getItem() == null){
text.setValue(""); //$NON-NLS-1$
}else if (!("").equals(getFormat())) { //$NON-NLS-1$
text.setValue(type.toString(getItem(), getFormat()));
} else {
text.setValue(type.toString(getItem()));
}
}
Instead of modifying the text, you would be modifying the graphic with the new color.
I tried to implement my own JSpinner model to accept an enumeration (including I18N), so I did like that:
searchSpinner.setModel(new AbstractSpinnerModel() {
int index = 0;
int minIndex = 0;
int maxIndex = MY_ENUM.values().length - 1;
Object selected = MY_ENUM.values()[index];
#Override
public Object getValue() {
return selected;
}
#Override
public void setValue(Object value) {
selected = value;
fireStateChanged();
}
#Override
public Object getNextValue() {
if (index < maxIndex) {
index++;
}
fireStateChanged();
return MY_ENUM.values()[index];
}
#Override
public Object getPreviousValue() {
if (index > minIndex) {
index--;
}
fireStateChanged();
return MY_ENUM.values()[index];
}
#Override
public void addChangeListener(ChangeListener l) {
}
#Override
public void removeChangeListener(ChangeListener l) {
}
});
The problem is that did not work, and even the spinner list looks like disabled. What am I doing wrong?
UPDATE: Based on first answer
You should extend from AbstractSpinnerModel (note to folks new to his question -- note that his original question had the class implementing the SpinnerModel interface. He later changed his code to reflect my recommendation) and be sure to call the fireStateChanged() method when appropriately. Also you've not taken into account edge cases and beyond edge cases.
e.g.,
import javax.swing.*;
import javax.swing.JSpinner.DefaultEditor;
public class MySpinnerPanel extends JPanel {
public static void main(String[] args) {
JSpinner spinner = new JSpinner(new MyEnumSpinnerModel());
JSpinner.DefaultEditor editor = (DefaultEditor) spinner.getEditor();
editor.getTextField().setColumns(5);
JPanel panel = new JPanel();
panel.add(spinner);
JOptionPane.showMessageDialog(null, panel);
}
}
enum MyEnum {
FE, FI, FO, FUM, FOO, FUBAR, SPAM
}
class MyEnumSpinnerModel extends AbstractSpinnerModel {
private int index = 0;
#Override
public Object getValue() {
return MyEnum.values()[index];
}
#Override
public void setValue(Object value) {
if (value instanceof MyEnum) {
index = ((MyEnum) value).ordinal();
fireStateChanged();
} else {
String text = value.toString() + " is not a valid enum item";
throw new IllegalArgumentException(text);
}
}
#Override
public Object getNextValue() {
if (index >= MyEnum.values().length - 1) {
return null;
} else {
return MyEnum.values()[index + 1];
}
}
#Override
public Object getPreviousValue() {
if (index <= 0) {
return null;
} else {
return MyEnum.values()[index - 1 ];
}
}
}
Edit
Note that the model itself should not require a listener to notify the view (as per the other answer to this question) as that's what the AbstractSpinnerModel does internally. It's fireStateChange() method is what the model itself should call to trigger this notification, same as most all other similar model structures in Swing such as any TableModel object that you create that derives from the AbstractTableModel. For details, please see the source code for the SpinnerListModel. Your code should emulate this class.
You should use ChangeListener to notify the view of changes in the model.
spinner = new JSpinner(new SpinnerModel() {
private ChangeListener l;
#Override
public void setValue(Object value) {
...
if(l != null) {
l.stateChanged(new ChangeEvent(this));
}
}
...
#Override
public void addChangeListener(ChangeListener l) {
this.l = l;
}
#Override
public void removeChangeListener(ChangeListener l) {
if(this.l == l) {
this.l = null;
}
}
});
Edit: You can use List to register many listeners.
At beginning I will say that I don't have in mind auto-complete combobox, but rather having a "setFilter(Set)" method in my combobox, so it displays what is in the set.
I was unable to achieve that effect, trying different approaches and I think it's the view responsibility to filter what it displays, so I should not extend ComboBoxModel.
This is what I have so far (main includes the case which doesn't work):
import java.awt.*;
import java.util.Set;
import javax.swing.*;
public class FilteredComboBox extends JComboBox {
private ComboBoxModel entireModel;
private final DefaultComboBoxModel filteredModel = new DefaultComboBoxModel();
private Set objectsToShow;
public FilteredComboBox(ComboBoxModel model) {
super(model);
this.entireModel = model;
}
public void setFilter(Set objectsToShow) {
if (objectsToShow != null) {
this.objectsToShow = objectsToShow;
filterModel();
} else {
removeFilter();
}
}
public void removeFilter() {
objectsToShow = null;
filteredModel.removeAllElements();
super.setModel(entireModel);
}
private void filterModel() {
filteredModel.removeAllElements();
for (int i = 0; i < entireModel.getSize(); ++i) {
Object element = entireModel.getElementAt(i);
addToFilteredModelIfShouldBeDisplayed(element);
}
super.setModel(filteredModel);
}
private void addToFilteredModelIfShouldBeDisplayed(Object element) {
if (objectsToShow.contains(element)) {
filteredModel.addElement(element);
}
}
#Override
public void setModel(ComboBoxModel model) {
entireModel = model;
super.setModel(entireModel);
if (objectsToShow != null) {
filterModel();
}
}
public static void main(String[] args) {
JFrame f = new JFrame();
f.setLayout(new BoxLayout(f.getContentPane(), BoxLayout.X_AXIS));
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DefaultComboBoxModel model = new DefaultComboBoxModel();
FilteredComboBox cb = new FilteredComboBox(model);
cb.setPrototypeDisplayValue("XXXXXXXXXXXX");
f.add(cb);
f.pack();
Set objectsToShow = new HashSet();
objectsToShow.add("1");
objectsToShow.add("3");
objectsToShow.add("4");
cb.setFilter(objectsToShow); // if you set that filter after addElements it will work
model.addElement("1");
model.addElement("2");
model.addElement("3");
model.addElement("4");
model.addElement("5");
f.setVisible(true);
}
}
"I think it's the view responsibility to filter what it displays" - I'd argue that, the view displays what it's told, the model drives what it can show, but that's me...
This is an idea I wrote way back in Java 1.3 (with generic updates) which basically wraps a proxy ComboBoxModel around another ComboBoxModel. The proxy (or FilterableComboBoxModel) then makes decisions about which elements from the original model match a filter and updates it's indices.
Basically, all it does is generates an index map between itself and the original model, so it's not copying anything or generating new references to the original data.
The filtering is controlled via a "filterable" interface which simply passes the element to be checked and expects a boolean result in response. This makes the API highly flexible as filtering can be done any way you want without the need to change the FilterableComboBoxModel in any way. It also means you can change the filter been used by simply applying a new one...
If, like I usually do, you want to pass some value to the filter, you will need to inform the model that the filter has changed, via the updateFilter method...yeah, I know, a ChangeListener would probably be a better idea, but I was trying to keep it simple ;)
For flexibility (and to maintain the current inheritance model), the core API is based on a ListModel, which means, you can also use the same concept with a JList
FilterableListModel
import java.util.ArrayList;
import java.util.List;
import javax.swing.AbstractListModel;
import javax.swing.ListModel;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;
public class FilterableListModel<E> extends AbstractListModel<E> implements ListDataListener {
private ListModel<E> peer;
private List<Integer> lstFilteredIndicies;
private IFilterable filter;
public FilterableListModel() {
lstFilteredIndicies = new ArrayList<Integer>(25);
}
public FilterableListModel(ListModel<E> model) {
this();
setModel(model);
}
public FilterableListModel(ListModel<E> model, IFilterable filter) {
this();
setModel(model);
setFilter(filter);
}
public void setModel(ListModel<E> parent) {
if (peer == null || !peer.equals(parent)) {
if (peer != null) {
fireIntervalRemoved(this, 0, peer.getSize() - 1);
peer.removeListDataListener(this);
}
peer = parent;
lstFilteredIndicies.clear();
if (peer != null) {
peer.addListDataListener(this);
}
filterModel(true);
}
}
public ListModel<E> getModel() {
return peer;
}
#Override
public int getSize() {
IFilterable filter = getFilter();
return filter == null ? getModel() == null ? 0 : getModel().getSize() : lstFilteredIndicies.size();
}
#Override
public E getElementAt(int index) {
IFilterable filter = getFilter();
ListModel<E> model = getModel();
E value = null;
if (filter == null) {
if (model != null) {
value = model.getElementAt(index);
}
} else {
int filterIndex = lstFilteredIndicies.get(index);
value = model.getElementAt(filterIndex);
}
return value;
}
public int indexOf(Object value) {
int index = -1;
for (int loop = 0; loop < getSize(); loop++) {
Object at = getElementAt(loop);
if (at == value) {
index = loop;
break;
}
}
return index;
}
#Override
public void intervalAdded(ListDataEvent e) {
IFilterable filter = getFilter();
ListModel model = getModel();
if (model != null) {
if (filter != null) {
int startIndex = Math.min(e.getIndex0(), e.getIndex1());
int endIndex = Math.max(e.getIndex0(), e.getIndex1());
for (int index = startIndex; index <= endIndex; index++) {
Object value = model.getElementAt(index);
if (filter.include(value)) {
lstFilteredIndicies.add(index);
int modelIndex = lstFilteredIndicies.indexOf(index);
fireIntervalAdded(this, modelIndex, modelIndex);
}
}
} else {
fireIntervalAdded(this, e.getIndex0(), e.getIndex1());
}
}
}
#Override
public void intervalRemoved(ListDataEvent e) {
IFilterable filter = getFilter();
ListModel model = getModel();
if (model != null) {
if (filter != null) {
int oldRange = lstFilteredIndicies.size();
filterModel(false);
fireIntervalRemoved(this, 0, oldRange);
if (lstFilteredIndicies.size() > 0) {
fireIntervalAdded(this, 0, lstFilteredIndicies.size());
}
} else {
fireIntervalRemoved(this, e.getIndex0(), e.getIndex1());
}
}
}
#Override
public void contentsChanged(ListDataEvent e) {
filterModel(true);
}
public void setFilter(IFilterable<E> value) {
if (filter == null || !filter.equals(value)) {
filter = value;
if (getModel() != null) {
if (getModel().getSize() > 0) {
fireIntervalRemoved(this, 0, getModel().getSize() - 1);
}
}
filterModel(true);
}
}
public IFilterable<E> getFilter() {
return filter;
}
protected void filterModel(boolean fireEvent) {
if (getSize() > 0 && fireEvent) {
fireIntervalRemoved(this, 0, getSize() - 1);
}
lstFilteredIndicies.clear();
IFilterable<E> filter = getFilter();
ListModel<E> model = getModel();
if (filter != null && model != null) {
for (int index = 0; index < model.getSize(); index++) {
E value = model.getElementAt(index);
if (filter.include(value)) {
lstFilteredIndicies.add(index);
if (fireEvent) {
fireIntervalAdded(this, getSize() - 1, getSize() - 1);
}
}
}
}
}
public void updateFilter() {
IFilterable filter = getFilter();
ListModel model = getModel();
if (filter != null && model != null) {
for (int index = 0; index < model.getSize(); index++) {
Object value = model.getElementAt(index);
if (filter.include(value)) {
if (!lstFilteredIndicies.contains(index)) {
lstFilteredIndicies.add(index);
fireIntervalAdded(this, getSize() - 1, getSize() - 1);
}
} else if (lstFilteredIndicies.contains(index)) {
int oldIndex = lstFilteredIndicies.indexOf(index);
lstFilteredIndicies.remove(oldIndex);
fireIntervalRemoved(this, oldIndex, oldIndex);
}
}
}
}
}
Filterable
public interface IFilterable<O> {
public boolean include(O value);
}
FilterableComboBoxModel
import javax.swing.ComboBoxModel;
public class FilterableComboBoxModel<E> extends FilterableListModel<E> implements ComboBoxModel<E> {
private FilterableComboBoxModel(ComboBoxModel<E> model) {
super(model);
}
public ComboBoxModel<E> getComboBoxModel() {
return (ComboBoxModel) getModel();
}
#Override
public void setSelectedItem(Object anItem) {
getComboBoxModel().setSelectedItem(anItem);
}
#Override
public Object getSelectedItem() {
return getComboBoxModel().getSelectedItem();
}
}
It should be noted that it might actually be possible to use a RowFilter instead, but I've never really had the time to look at it (since I already had a working API)
Below is my code that displays images in a JList. I want to edit the description by each of the images shown in the JList. I don't know how to do it & need help. Thanks...
import java.util.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.Serializable;
public class DesignPicture2 {
private static String imageName;
static ArrayList<String> imgName = new ArrayList<String>();
public static void main(String[] args) throws Exception {
DesignPicture2 mm = new DesignPicture2();
mm.getImageName("C:\\Images 2 display");
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
JFrame frame = new JFrame("Image panel");
frame.setSize(800, 500);
//frame.setLocationByPlatform(true);
frame.setLocation(600, 300);
JList imageList = createImageList();
frame.getContentPane().add(new JScrollPane(imageList));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
private static JList createImageList() {
JList imageList = new JList(createModel("C:\\Images 2 display"));
imageList.setCellRenderer(new ImageCellRenderer());
imageList.setLayoutOrientation(JList.HORIZONTAL_WRAP);
imageList.setVisibleRowCount(0);
imageList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
imageList.setFixedCellWidth(240);
imageList.setFixedCellHeight(120);
// imageList.setDragEnabled(false);
//imageList.setDropMode(DropMode.INSERT);
imageList.setTransferHandler(new ImageTransferHandler(imageList));
return imageList;
}
private static DefaultListModel createModel(String path) {
File folder = new File(path);
File[] listOfFiles = folder.listFiles();
DefaultListModel model = new DefaultListModel();
int count = 0;
for (int i = 0; i < listOfFiles.length - 1; i++) {
System.out.println("check path: " + listOfFiles[i]);
imageName = imgName.get(i).toString();
String name = listOfFiles[i].toString();
//load only JPEGS
if (name.endsWith("jpg")) {
try {
ImageIcon ii = new ImageIcon(ImageIO.read(listOfFiles[i]));
model.add(count, ii);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return model;
}
static class ImageTransferHandler extends TransferHandler {
private static final DataFlavor DATA_FLAVOUR = new DataFlavor(ColorIcon.class, "Images");
private final JList previewList;
private boolean inDrag;
ImageTransferHandler(JList previewList) {
this.previewList = previewList;
}
public int getSourceActions(JComponent c) {
return TransferHandler.MOVE;
}
protected Transferable createTransferable(JComponent c) {
inDrag = true;
return new Transferable() {
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[]{DATA_FLAVOUR};
}
public boolean isDataFlavorSupported(DataFlavor flavor) {
return flavor.equals(DATA_FLAVOUR);
}
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
return previewList.getSelectedValue();
}
};
}
public boolean canImport(TransferSupport support) {
if (!inDrag || !support.isDataFlavorSupported(DATA_FLAVOUR)) {
return false;
}
JList.DropLocation dl = (JList.DropLocation) support.getDropLocation();
if (dl.getIndex() == -1) {
return false;
} else {
return true;
}
}
public boolean importData(TransferSupport support) {
if (!canImport(support)) {
return false;
}
Transferable transferable = support.getTransferable();
try {
Object draggedImage = transferable.getTransferData(DATA_FLAVOUR);
JList.DropLocation dl = (JList.DropLocation) support.getDropLocation();
DefaultListModel model = (DefaultListModel) previewList.getModel();
int dropIndex = dl.getIndex();
if (model.indexOf(draggedImage) < dropIndex) {
dropIndex--;
}
model.removeElement(draggedImage);
model.add(dropIndex, draggedImage);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
protected void exportDone(JComponent source, Transferable data, int action) {
super.exportDone(source, data, action);
inDrag = false;
}
}
static class ImageCellRenderer extends JPanel implements ListCellRenderer {
int count = 0;
DefaultListCellRenderer defaultListCellRenderer = new DefaultListCellRenderer();
JLabel imageLabel = new JLabel();
JLabel descriptionLabel = new JLabel();
ImageCellRenderer() {
setLayout(new BorderLayout());
Border emptyBorder = BorderFactory.createEmptyBorder(5, 5, 5, 5);
imageLabel.setBorder(emptyBorder);
descriptionLabel.setBorder(emptyBorder);
add(imageLabel, BorderLayout.AFTER_LINE_ENDS);
add(descriptionLabel, BorderLayout.SOUTH);
// imageLabel.setText(imgName.get(0).toString());
}
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
defaultListCellRenderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
setBorder(defaultListCellRenderer.getBorder());
setBackground(defaultListCellRenderer.getBackground());
imageLabel.setIcon((Icon) value);
if (count > imgName.size() - 1) {
count = 0;
} else {
descriptionLabel.setText(imgName.get(count).toString());
}
return this;
}
}
public void getImageName(String path) {
int c = 0;
final File dir = new File(path);
// array of supported extensions (use a List if you prefer)
final String[] EXTENSIONS = new String[]{
"jpg", "gif", "png", "bmp" // and other formats you need
// filter to identify images based on their extensions
};
final FilenameFilter IMAGE_FILTER = new FilenameFilter() {
#Override
public boolean accept(final File dir, final String name) {
for (final String ext : EXTENSIONS) {
if (name.endsWith("." + ext)) {
return (true);
}
}
return (false);
}
};
if (dir.isDirectory()) { // make sure it's a directory
for (final File f : dir.listFiles(IMAGE_FILTER)) {
BufferedImage img = null;
c++;
try {
img = ImageIO.read(f);
// you probably want something more involved here
// to display in your UI
System.out.println("image: " + f.getName());
imgName.add(f.getName().toString());
} catch (final IOException e) {
// handle errors here
System.out.println("Error!");
}
}
System.out.println("C: " + c);
} else {
System.out.println("Invalid Directory!");
}
}
static class ColorIcon implements Icon, Serializable {
private Color color;
ColorIcon(Color color) {
this.color = color;
}
public void paintIcon(Component c, Graphics g, int x, int y) {
g.setColor(color);
g.fillRect(x, y, getIconWidth(), getIconHeight());
}
public int getIconWidth() {
return 200;
}
public int getIconHeight() {
return 100;
}
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) {
return false;
}
return color.equals(((ColorIcon) o).color);
}
}
}
When I run the above code, it show the image in proper way, but the description of each image is fixed and I don't know how to change it. Hope anyone can help me.
I agree with trashgod (+1 to his suggestion), a JTable will be a simpler solution, here's why...
JList doesn't support editability, so you'd need to create it...
So, first, we'd need some kind of ListModel that provided some additional functionality, in particular, the ability to set the value at a particular index...
import javax.swing.ListModel;
public interface MutableListModel<E> extends ListModel<E> {
public void setElementAt(E value, int index);
public boolean isCellEditable(int index);
}
Next, we'd need some kind editor, in this case, following standard Swing API conventions, this asks for some kind of base interface
import java.awt.Component;
import javax.swing.CellEditor;
import javax.swing.JList;
public interface ListCellEditor<E> extends CellEditor {
public Component getListCellEditorComponent(
JList<E> list,
E value,
boolean isSelected,
int index);
public void applyEditorValue(E value);
}
Now, we need to create ourselves a custom JList capable of actually performing all the required functionality of editing a cell value...
Things like...
Recognising a "start editing" event
Determine if the cell can be edited
Managing the editing process, preparing and showing the editor, knowing when the editor has stopped or canceled and clean up appropriately...
Handling selection changes while editing is in progress
Handling component focus change (which I've not done cause that's an awesome amount of fun in itself...)
For example...
import java.awt.Component;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JList;
import javax.swing.KeyStroke;
import javax.swing.ListModel;
import javax.swing.event.CellEditorListener;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class EditableList<E> extends JList<E> {
private ListCellEditor<E> editor;
private int editingCell = -1;
private Component editorComponent;
private CellEditorHandler handler;
public EditableList(MutableListModel<E> model) {
this();
setModel(model);
}
public EditableList() {
InputMap im = getInputMap(WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_F2, 0), "editorCell");
ActionMap am = getActionMap();
am.put("editorCell", new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("Edit baby");
int cell = getSelectedIndex();
editCellAt(cell);
}
});
addListSelectionListener(new ListSelectionListener() {
#Override
public void valueChanged(ListSelectionEvent e) {
if (isEditing()) {
if (!stopCellEditing()) {
cancelCellEditing();
}
}
}
});
}
public boolean isEditing() {
return editorComponent != null;
}
public void cancelCellEditing() {
getEditor().cancelCellEditing();
}
public boolean stopCellEditing() {
return getEditor().stopCellEditing();
}
public CellEditorHandler getCellEditorHandler() {
if (handler == null) {
handler = new CellEditorHandler();
}
return handler;
}
public void setEditor(ListCellEditor<E> value) {
if (value != editor) {
ListCellEditor old = editor;
editor = value;
firePropertyChange("editor", old, editor);
}
}
public ListCellEditor<E> getEditor() {
return editor;
}
public boolean isCellEditable(int cell) {
boolean isEditable = false;
ListModel model = getModel();
if (model instanceof MutableListModel) {
MutableListModel mcm = (MutableListModel) model;
isEditable = mcm.isCellEditable(cell);
}
return isEditable;
}
protected void editCellAt(int index) {
if (isCellEditable(index)) {
ListCellEditor<E> editor = getEditor();
if (editor != null) {
Rectangle cellBounds = getCellBounds(index, index);
E value = getModel().getElementAt(index);
boolean selected = isSelectedIndex(index);
editingCell = index;
editor.addCellEditorListener(getCellEditorHandler());
editorComponent = editor.getListCellEditorComponent(this, value, selected, index);
editorComponent.setBounds(cellBounds);
ensureIndexIsVisible(index);
add(editorComponent);
revalidate();
}
}
}
public int getEditingCell() {
return editingCell;
}
protected void editingHasStopped(ListCellEditor editor) {
editingCell = -1;
if (editorComponent != null) {
remove(editorComponent);
}
if (editor != null) {
editor.removeCellEditorListener(getCellEditorHandler());
}
}
public class CellEditorHandler implements CellEditorListener {
#Override
public void editingStopped(ChangeEvent e) {
E value = getModel().getElementAt(getEditingCell());
getEditor().applyEditorValue(value);
((MutableListModel) getModel()).setElementAt(value, getEditingCell());
editingHasStopped((ListCellEditor)e.getSource());
}
#Override
public void editingCanceled(ChangeEvent e) {
editingHasStopped((ListCellEditor)e.getSource());
}
}
}
Now, having done all that, you will need change the way you've structured your program, instead of using a List and ListModel to manage the descriptions and images separately, you should probably merge the concept into a single, manageable object, for example...
public class ImagePreview {
private String name;
private ImageIcon image;
public ImagePreview(String name, ImageIcon image) {
this.name = name;
this.image = image;
}
public String getDescription() {
return name;
}
public ImageIcon getImage() {
return image;
}
protected void setDescription(String description) {
this.name = description;
}
}
Even if you choose to use a JTable instead, you'll find this easier to manage...
Now we need some way to render and edit these values, to this end, I choose to start with a base component which could display the image and text...
public class ImagePreviewPane extends JPanel {
private JLabel imageLabel = new JLabel();
private JLabel descriptionLabel = new JLabel();
public ImagePreviewPane() {
setLayout(new BorderLayout());
Border emptyBorder = BorderFactory.createEmptyBorder(5, 5, 5, 5);
imageLabel.setBorder(emptyBorder);
descriptionLabel.setBorder(emptyBorder);
add(imageLabel, BorderLayout.CENTER);
add(descriptionLabel, BorderLayout.SOUTH);
}
protected JLabel getDescriptionLabel() {
return descriptionLabel;
}
protected JLabel getImageLabel() {
return imageLabel;
}
public void setImage(ImageIcon icon) {
imageLabel.setIcon(icon);
}
public void setDescription(String text) {
descriptionLabel.setText(text);
}
}
Create an extended version that could handle editing...
public static class ImagePreviewEditorPane extends ImagePreviewPane {
private JTextField editor;
public ImagePreviewEditorPane() {
super();
editor = new JTextField();
remove(getDescriptionLabel());
add(editor, BorderLayout.SOUTH);
}
#Override
public void setDescription(String text) {
editor.setText(text);
}
public String getDescription() {
return editor.getText();
}
public void setImagePreview(ImagePreview preview) {
setImage(preview.getImage());
setDescription(preview.getDescription());
}
#Override
public void addNotify() {
super.addNotify();
editor.requestFocusInWindow();
}
}
This is done to try and make it easier to modify the components later...
Next, a ListCellRenderer
public class ImageCellRenderer extends ImagePreviewPane implements ListCellRenderer {
public ImageCellRenderer() {
}
#Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
Color bg = null;
Color fg = null;
JList.DropLocation dropLocation = list.getDropLocation();
if (dropLocation != null
&& !dropLocation.isInsert()
&& dropLocation.getIndex() == index) {
bg = DefaultLookup.getColor(this, getUI(), "List.dropCellBackground");
fg = DefaultLookup.getColor(this, getUI(), "List.dropCellForeground");
isSelected = true;
}
if (isSelected) {
setBackground(bg == null ? list.getSelectionBackground() : bg);
setForeground(fg == null ? list.getSelectionForeground() : fg);
} else {
setBackground(list.getBackground());
setForeground(list.getForeground());
}
if (value instanceof ImagePreview) {
ImagePreview ip = (ImagePreview) value;
setImage(ip.getImage());
setDescription(ip.getDescription());
} else {
setImage(null);
setDescription("??");
}
setEnabled(list.isEnabled());
setFont(list.getFont());
return this;
}
}
And editor...
public class ImagePreviewListCellEditor extends AbstactListCellEditor<ImagePreview> {
private ImagePreviewEditorPane previewPane;
public ImagePreviewListCellEditor() {
previewPane = new ImagePreviewEditorPane();
InputMap im = previewPane.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "accept");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "cancel");
ActionMap am = previewPane.getActionMap();
am.put("accept", new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
stopCellEditing();
}
});
am.put("cancel", new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
cancelCellEditing();
}
});
}
public void applyEditorValue(ImagePreview preview) {
preview.setDescription(previewPane.getDescription());
}
#Override
public Component getListCellEditorComponent(JList<ImagePreview> list, ImagePreview value, boolean isSelected, int index) {
Color bg = null;
Color fg = null;
JList.DropLocation dropLocation = list.getDropLocation();
if (dropLocation != null
&& !dropLocation.isInsert()
&& dropLocation.getIndex() == index) {
bg = DefaultLookup.getColor(previewPane, previewPane.getUI(), "List.dropCellBackground");
fg = DefaultLookup.getColor(previewPane, previewPane.getUI(), "List.dropCellForeground");
isSelected = true;
}
if (isSelected) {
previewPane.setBackground(bg == null ? list.getSelectionBackground() : bg);
previewPane.setForeground(fg == null ? list.getSelectionForeground() : fg);
} else {
previewPane.setBackground(list.getBackground());
previewPane.setForeground(list.getForeground());
}
if (value instanceof ImagePreview) {
ImagePreview preview = (ImagePreview)value;
previewPane.setImagePreview(preview);
} else {
previewPane.setImagePreview(null);
}
return previewPane;
}
}
And finally, putting it altogether...
public class DesignPicture2 {
public static void main(String[] args) throws Exception {
DesignPicture2 mm = new DesignPicture2();
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
JFrame frame = new JFrame("Image panel");
frame.setSize(800, 500);
frame.setLocation(600, 300);
JList imageList = createImageList();
frame.getContentPane().add(new JScrollPane(imageList));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
private static JList createImageList() {
EditableList<ImagePreview> imageList = new EditableList(createModel("..."));
imageList.setEditor(new ImagePreviewListCellEditor());
imageList.setCellRenderer(new ImageCellRenderer());
imageList.setLayoutOrientation(JList.HORIZONTAL_WRAP);
imageList.setVisibleRowCount(0);
imageList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
imageList.setFixedCellWidth(240);
imageList.setFixedCellHeight(120);
return imageList;
}
private static MutableListModel<ImagePreview> createModel(String path) {
File folder = new File(path);
File[] listOfFiles = folder.listFiles();
DefaultMutableListModel<ImagePreview> model = new DefaultMutableListModel<>();
int count = 0;
for (int i = 0; i < listOfFiles.length - 1; i++) {
System.out.println("check path: " + listOfFiles[i]);
String name = listOfFiles[i].toString();
if (name.endsWith("jpg")) {
try {
ImageIcon ii = new ImageIcon(ImageIO.read(listOfFiles[i]));
model.addElement(new ImagePreview(name, ii));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return model;
}
}
JList does not support the notion of a cell editor. Instead, use a two-column JTable. Some important points:
Your model's isCellEditable() implementation should return true for the description column in order to make it editable.
Your model's implementation of getColumnClass() can let the default renderer display your image by returning ImageIcon or Icon .
I am struggling a long time now with the following problem:
I do not understand why the method setStatus () does not work. The method setButton () check whether the individual elements in a field "status" to "true" if all this have to activate a button. Unfortunately staus field values do not change.
I'd like to add, that textBox.setTitle() and textBox.addStyleName work well.
Here is my code:
package com.mg;
import java.util.ArrayList;
import com.comarch.spr.client.UserServlet;
import com.comarch.spr.client.UserServletAsync;
import com.gargoylesoftware.htmlunit.javascript.host.Window;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.BlurHandler;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.TextBox;
public class Validator {
private ArrayList<ValidationElement> listOfElements = new ArrayList<Validator.ValidationElement>();
private Button button;
public int i = 0;
public Validator(ArrayList<ValidationElement> listOfElements, Button button) {
this.listOfElements = listOfElements;
this.button = button;
}
public void createElement0(TextBox textbox, String regex, int type, UserServletAsync userServlet ) {
ValidationElement element = new ValidationElement(textbox, regex, type, userServlet);
listOfElements.add(element);
}
public void createElement1(TextBox textbox, String regex, int type ) {
ValidationElement element = new ValidationElement(textbox, regex, type);
listOfElements.add(element);
}
public void addElement(ValidationElement element) {
listOfElements.add(element);
}
public void setButton() {
int sizeOfList = listOfElements.size();
int check = 0;
for(ValidationElement element : listOfElements) {
element.validate();
if (element.status == true)
check = check + 1;
}
if (check != 0)
button.setEnabled(true);
else
button.setEnabled(false);
}
//========================== VALIDATION ELEMENT CLASS===========================
public class ValidationElement {
public TextBox textBox = new TextBox();
public boolean status;
public String regex;
public int type; // 0 - validateFromDatabase() , || 1 - checkRegEx()
public UserServletAsync userServlet;
public ValidationElement(TextBox textbox, String regex, int type ) {
this.textBox = textbox;
this.regex = regex;
this.type = type;
}
public ValidationElement(TextBox textbox, String regex, int type, UserServletAsync userServlet) {
this.textBox = textbox;
this.regex = regex;
this.type = type;
this.userServlet = userServlet;
}
public boolean isStatus() {
return status;
}
public void setStatus(boolean status) {
this.status = status;
}
//========================== VALIDATION FUNCTIONS =========================================
public void validate() {
if (type == 0) {
validateFromDatabase();
}
else if(type == 1)
checkRegEx();
}
public void validateFromDatabase() {
textBox.addBlurHandler(new BlurHandler() {
#Override
public void onBlur(BlurEvent event) {
final String text = textBox.getText();
userServlet.validateUser(textBox.getText(), new AsyncCallback<Boolean>() {
public void onFailure(Throwable caught) {
}
public void onSuccess(Boolean result) {
boolean resultOfCallback = result;
if(resultOfCallback){
textBox.addStyleName("invalid");
textBox.setTitle("error");
}
else if (text.matches(regex) && !resultOfCallback) {
textBox.setStyleName("gwt-TextBox");
setStatus(true);
}
else if (text.matches(regex) && resultOfCallback) {
}
else {
textBox.addStyleName("invalid");
textBox.setTitle("error");
}
}
});
}
});
}
public void checkRegEx () {
textBox.addKeyPressHandler(new KeyPressHandler() {
#Override
public void onKeyPress(KeyPressEvent event) {
final String text = textBox.getText();
if(!text.matches(regex))
{
setStatus(false);
}
else {
setStatus(true);
}
isValid();
}
});
textBox.addBlurHandler(new BlurHandler() {
public void onBlur(BlurEvent event) {
final String text = textBox.getText();
if(!text.matches(regex))
{
textBox.addStyleName("invalid");
textBox.setTitle("error");
setStatus(false);
}
else
{
setStatus(true);
textBox.setStyleName("gwt-TextBox");
textBox.setTitle("error");
}
}
});
}
public void isValid() {
if(this.status = true)
i=i+1;
}
}
}
public void isValid() {
if(this.status = true)
i=i+1;
}
Shouldn't this be
public void isValid() {
if(this.status == true)
i=i+1;
}
The first method sets the status to true, then always increments i.
Or you can go one better:
public void isValid() {
if(this.status) {
i++;
}
}
Also if you're using Eclipse, you can set the compiler options to highlight accidental boolean assignments in if statements as compilation errors. It's a very useful option.
In class Validator, there is:
public int i = 0;
In class ValidationElement, which is inside Validator, in for example checkRegEx() method, I put:
i++;
public void checkRegEx () {
textBox.addKeyPressHandler(new KeyPressHandler() {
#Override
public void onKeyPress(KeyPressEvent event) {
final String text = textBox.getText();
if(!text.matches(regex))
{
setStatus(false);
}
else {
setStatus(true);
}
isValid();
}
});
textBox.addBlurHandler(new BlurHandler() {
public void onBlur(BlurEvent event) {
final String text = textBox.getText();
if(!text.matches(regex))
{
textBox.addStyleName("invalid");
textBox.setTitle("error");
setStatus(false);
}
else
{
textBox.setStyleName("gwt-TextBox");
textBox.setTitle("ok");
i++;
}
}
});
}
When I want to see if everything is okay. It turns out that the value of "i" in setButton() method in class Validator is still zero, while the other lines of code in this block work well:
textBox.setStyleName("gwt-TextBox");
textBox.setTitle("ok");
Most likely it's just too early to check status at the next line after validate() is invoked.
As far as I can tell status changes only after BlurEvent happens. TextBox updates you mention work fine because these seem to be triggered at the right moment.
public void setButton() {
int sizeOfList = listOfElements.size();
int check = 0;
button.setEnabled(false); // reset button
for(ValidationElement element : listOfElements) {
element.validate();
// drop attempt to check status here
}
// drop attempt to enable button here
}
//...
{
button.setEnabled(true); // was: setStatus(true);
textBox.setStyleName("gwt-TextBox");
textBox.setTitle("error");
}
Also I would prefer to define ValidationElement
static:
public static class ValidationElement
// have quite a painful experience chasing subtle bugs
// related to named non-static inner classes