I'm trying to write a GUI with several JTextFields. Two of the fields are for float values. I already have the fields ignoring any non-numerical inputs via a formatter, but I also want the text in the text field to update to a float value if an integer is inputted (i.e. "5" changes to "5.00"). I tried to update the text field using a setText() command from within an event, but the displayed value is not changing. I want the text to change once focus is lost on the text field.
The code is very long, so I will include the relevant snippet.
heightField.addFocusListener(new FocusAdapter() {
#Override
public void focusLost(FocusEvent arg0) {
heightDone = !heightField.getText().trim().equalsIgnoreCase("");
//This is the problem code//
if(UF.isInt(heightField.getText().trim()))
heightField.setText(heightField.getText().trim().concat(".00"));
System.out.println(heightField.getText());
heightFormat = UF.isFloat(heightField.getText().trim());
isDone();
}
});
maybe you need to refresh the frame after the change on the textField :
frame.repaint();
Related
I have implemented Custom Header Component. Upon click of header text, I am calling this.params.setSort(order, event.shiftKey) to perform sorting. The sorting works.
But I have onSortChanged event and want to get the sortIndex of that column.
But event.column.getSortIndex() returns old sortIndex instead of new value.
onSortChanged(event?) {
`this.sortIndex = event.column.getSortIndex();` // RETURNS OLD VALUE
}
Use getColumnState()
let sortModel = this.props.columnApi.getColumnState().filter((col) => col.sort);
sortModel[0].sortIndex; // sortIndex
I have several fields in a screen, that are partially dependent each on other by validating rules.
If user changes one field then I can affect another fields using setValue(). But I am fighting with the problem, valueChange event is fired from setValue() just as from user activity.
My example: I have four fields "activity_status", "schedule_date", "start_date", "end_date". By editing any one field I want to affect another three fields (changing status, setting or shifting dates). How to avoid recursive calling valueChange method?
I can imagine a variable justProcessedField that can working as a lock, but has anybody a better hands-on solution?
Usually we do set a flag when we trigger the first valueChangeEvent() and then ignore all others, until the first trigger is finished processing.
The pseudocode looks like this:
private boolean _ignoreTriggers= false; // Set ignore triggers when we do manual setValue stuff
field1.addListener(new ValueChangeListener() {
#Override
public void valueChange(ValueChangeEvent event) {
if (!_ignoreTriggers) {
_ignoreTriggers= true;
// Do the processing and setValues(...) in the other fields
_ignoreTriggers= false;
}
}
}
);
With different booleans you can also make groups of fields "sensisble/insensible" to changes in other fields.
To avoid the ValueChangeEvents you can create custom fields, which are extentions of the fields you want to modify. This custom fields should have a public method which calls the setInternalValue method.
Example for Checkbox-Field:
public class CheckBoxSilent extends CheckBox {
/**
* Set the new value without calling a {#link ValueChangeListener}
*
* #param newValue the new value to be set.
*/
public void setValueSecretly(boolean newValue) {
setInternalValue(newValue);
markAsDirty();
}
}
I use a CellTable with EditTextCell
When the EditTextCell fire the FieldUpdater, I want to do a validation and set the EditTextCell to the old value if validation fail. But I cant find how to update the CellTable or the specified row.
Here a piece of code:
titleColumn.setFieldUpdater(new FieldUpdater<QuestionDto, String>() {
#Override
public void update(int index, QuestionDto object, String value) {
if (!isValid(value))
// Here I need to set the EditTextCell to the value in my object
else
// It's valid I do the work
}
});
I was looking for something like : ((EditTextCell)titleColumn.getCell(index)).setValue(object.getTitle());
The other solution is to reset all the CellTable like that:
table.setRowData(dataProvider.getList());
But it's don't work too.
I'm not very knowledgeable of EditTextCell but for other widgets I would catch the ChangeEvent (is it possible to catch it the cell you're using ?) then call event.stopPropagation() if I don't want the user action to have any effect.
I'm currently trying to make a TextBox for my GUI with XNA, and I was wondering how could I find tagged text in a string.
For instanceI have this kind of text:
Hey there, I was <r>going to</r> the <b>Mall</b> today!
So the <r> tag would represent red text and the <b> tag would represent blue text.
And I want to know exactly where the red text starts and where the blue text starts so I could render them separately.
Do you have any suggestion what to do about it, and what to use for doing that?
Thanks in advance.
I would suggest doing this with two methods
First, have a method that can take your string and return a collection of string color pairs:
struct StringColorPair {
public string myText; // the text
public Color myColor; // the color of this text
public int myOffset; // characters before this part of the string
// (for positioning in the Draw)
}
public List<StringColorPair> ParseColoredText(string text) {
var list = new List<StringColorPair>();
// Use a regex or other string parsing method to pull out the
// text chunks and their colors and then for each set of those do:
list.Add(
new StringColorPair {
myText = yourParsedSubText,
myColor = yourParsedColor,
myOffset = yourParsedOffset }
);
return list;
}
Then you would need a draw method like this:
public void Draw(List<StringColorPair> pairs) {
foreach(var pair in pairs) {
// Draw the relevant string and color at its needed offset
}
}
Well you could just parse the line and when you reach a set a color property of your text so that it will now render blue but it will have to be a separate render call or else the whole string will turn blue. So if you make a new string when you come upon a tag then set the color property then render that string then that should work.
The functionality I am trying to use is:
- Create a ObjectDataSource for selection and updating controls on a web page (User Control).
- Use the DataObjectTypeName to have an object created that would send the data to an UpdateMethod.
- Before the values are populated in the DataObjectTypeName’s object, I would like to pre-populate the object so the unused items in the class are not defaulted to zeros and empty strings without me knowing whether the zero or default string was set by the user or by the application.
I cannot find a way to pre-populate the values (this was an issue back in 2006 with framework 2.0). One might ask “Why would anyone need to pre-populate the object?”. The simple answer is: I want to be able to randomly place controls on different User Controls and not have to be concerned with which UpdateMethod needs to handle which fields of an object.
For Example, let’s say I have a class (that reflects a SQL Table) that includes the fields: FirstName, LastName, Address, City, State, Zip. I may want to give the user the option to change the FirstName and LastName and not even see the Address, City, State, Zip (or vice-versa). I do not want to create two UpdateMethods where one handled FirstName and LastName and the other method handles the other fields. I am working with a Class of some 40+ columns from multiple tables and I may want some fields on one screen and not another and decide later to change those fields from one screen to another (which breaks my UpdateMethods without me knowing).
I hope I explained my issue well enough.
Thanks
This is hardly a solution to the problem, but it's my best stab at it.
I have a GridView with its DataSourceID set to an ObjectDataSource.
Whenever a row is updated, I want the property values in the object to be selectively updated - that is - only updated if they appear as columns in the GridView.
I've created the following extension:
public static class GridViewExtensions
{
public static void EnableLimitUpdateToGridViewColumns(this GridView gridView)
{
_gridView = gridView;
if (_gridView.DataSourceObject != null)
{
((ObjectDataSource)_gridView.DataSourceObject)
.Updating += new ObjectDataSourceMethodEventHandler(objectDataSource_Updating);
}
}
private static GridView _gridView;
private static void objectDataSource_Updating(object sender, ObjectDataSourceMethodEventArgs e)
{
var newObject = ((object)e.InputParameters[0]);
var oldObjects = ((ObjectDataSource)_gridView.DataSourceObject).Select().Cast<object>();
Type type = oldObjects.First().GetType();
object oldObject = null;
foreach (var obj in oldObjects)
{
if (type.GetProperty(_gridView.DataKeyNames.First()).GetValue(obj, null).ToString() ==
type.GetProperty(_gridView.DataKeyNames.First()).GetValue(newObject, null).ToString())
{
oldObject = obj;
break;
}
}
if (oldObject == null) return;
var dynamicColumns = _gridView.Columns.OfType<DynamicField>();
foreach (var property in type.GetProperties())
{
if (dynamicColumns.Where(c => c.DataField == property.Name).Count() == 0)
{
property.SetValue(newObject, property.GetValue(oldObject, null), null);
}
}
}
}
And in the Page_Init event of my page, I apply it to the GridView, like so:
protected void Page_Init()
{
GridView1.EnableLimitUpdateToGridViewColumns();
}
This is working well for me at the moment.
You could probably apply similar logic to other controls, e.g. ListView or DetailsView.
I'm currently scratching my head to think of a way this can be done in a rendering-agnostic manner - i.e. without having to know about the rendering control being used.
I hope this ends up as a normal feature of the GridView or ObjectDataSource control rather than having to hack it.