JComboBox as CellRenderer does not set the correct value - swing

I'm using a JComboBox as CellRenderer in my JTable.
Everything works fine the JComboBox displays the correct item for the corresponding row.
The problem I am currently working on is that when I choose a new value in the JComboBox (for example row 9) the value is set correctly, but when I try to change the value in the next row, the JComboBox (for example in row 10) automatically sets the value of the row before.
I created a DropDownCellRenderer class which extends JComboBox and implements TableCellRenderer, I thought that is enough, but it seems that the DropDownCellRenderer-object is the same for every row.
table.getColumnModel().getColumn( 3 ).setCellRenderer( new DropDownCellRenderer() );
table.getColumnModel().getColumn( 3 ).setCellEditor( new DefaultCellEditor( new DropDownCellRenderer() ) );
How can I avoid that every row uses the same object?

Looked at your renderer's source code.
I don't think you have to look up the Product by name. The value passed to you is the Product, which is coming from your table model (if it is implemented correctly). Just set the value as selected item and it should work.
To make renderer behave correctly, change its foreground and background colors according to isSelected parameter. The code should look like:
if (isSelected) {
setForeground(table.getSelectionForeground());
super.setBackground(table.getSelectionBackground());
} else {
setForeground(table.getForeground());
setBackground(table.getBackground());
}
Make your initial array of values an argument of the constructor. This will transform your renderer into universal combobox renderer.

It sounds like you're saving and displaying values within the combo box itself, not from the model of the table. When you set a value and save a combobox value you need to update the model

Related

Select row checkbox on each item select in Primefaces datatable cellEditor

When I select an item in datatable with editMode="cell" (e.g. inputText , selectOneMenu) only that specific component is selected which is normal, but I want to select to whole row when I click on it, not just the component.
my problem is kind of UI related, there are specific areas on the row when you click on, the whole row gets selected, but when you click on the component in the row, only that component gets selected not the row and if you're collecting the object in backing bean, you don't have that row object.
You can use the clientId from AjaxBehaviorEvent to get the index of the data.
ClientId is look like this
form:feeTbl:0:j_idt49
0 is the index. To get the index, you need to split it.

AS3 Databinding to Specific property at an index in ArrayCollection

I have a situation where I want to use databinding from an ArrayCollection to populate text fields in a Flex view.
The ArrayCollection is populated from an SQL Result object. I store the ArrayColelction in my model class using getters and setters like this:
private var _monthlyData:ArrayCollection;
public function set monthlyData(value:ArrayCollection):void{
_monthlyData = value;
}
[Bindable]
public function get monthlyData():ArrayCollection{
return _monthlyData;
}
I use the monthlyData as a dataprovider for a list etc which works fine. I also need to use properties at certain indexs in this collection as text field strings.
When the text field text properties are set I don’t neccesarily have the monthlyData arrayCollection populated yet.
The text fields are set in another outside class with has a singleton reference to this model so I set the fields like so at the moment:
textField.text = _model.monthlyData.getItemAt(3).Month;
I want to setup binding to the array collection instead of just using this assignment method so that when that item in the array is refreshed or the entire arrayCollection is populated or updated , it will update the textField text.
I’m having trouble getting the binding to work.
I’m using bindageTools at the moment but have been also using the built in as3 BindingUtils to little effect.
I can do the following which sets the initial text property correctly, but it wont update when the ArrayCollection changes:
Bind.fromProperty(_model.monthlyData.getItemAt(3),"Month").toProperty(textField, "text");
So if someone could please point me in the right direction as to which way is best to get the binding going in pure AS3 no MXML, I’d really appreciate it.
Cheers
Marco
From the code you provide, I can see that monthlyData is bindable, which is fine. I'll assume that _model is bindable too.
However the getItemAt() method is not bindable (it will not dispatch propertychange events when items change positions in the collection), hence the text property of the text field will not be updated.
What you can do is something like this:
[Bindable]
public var selectedDate3:MyDate;
<s:TextInput id="myTextInput" text="{selectedDate3.month}" />
or the AS equivalent (why you want to make things hard on yourself is beyond me though)
BindingUtils.bindProperty(myTextInput, "text", selectedDate3, "month");
and then programmatically update selectedDate3:
_model.monthlyData.addEventListener(CollectionEvent.COLLECTION_CHANGE, updateSelected);
private function updateSelected(event:CollectionEvent):void {
selectedDate3 = _model.monthlyData.getItemAt(3);
}
Note that the month property of MyDate must also be bindable.
You mention that these fields are in a VGroup. I'm guessing you want to display a top 3 or something. This is still a list. It would be much easier and cleaner to do this with a List or DataGroup and simply filter the ArrayCollection to only display the first 3 items (or whatever rule for the items to be displayed), or configure the DataGroup to display only three items (it has no scrollbar anyway).

AS3/Air/Starling mobile app textinput class and how to get text value when in a class

I have a mobile app built in as3/air for mobile devices and I am wondering what the best approach to handle textinput fields are.
What I mean is I have multiple input fields on multiple screens. The inputs have a label (textfield), background color under the label (quad) and an input (textinput). Some are password, some are not. Some are different width/heights, multi-line/single line etc. All however use embedded fonts, have a name and ID assigned etc.
I have created a class file that extends Sprite and built all the components of the textinput (textfield, quad, textinput) so my other classes/screens can just create a new instance of this class passing custom values.
var textField:TextFieldClass = new TextFieldClass();
textField.DrawTextField(name, ID, width, height, isPassword, hasLabel, labelPosition etc);
The above works great. I can reuse the class to draw multiple textinput on screen with minimal code however I am having trouble getting the text value/ID/name when there are multiple instances.
I have tried adding each textField instance into an array and iterating through but that gets x number of the last textField instance e.g. if first instance is named txtEmail and the second is txtPassword, I get 2 txtPasswords.
I have also tried getChildByName and specifying the name of the textinput but when I use txtEmail I get a cannot access null value but txtPassword works.
Maybe I am going about this wrong so happy to use a better approach if one exists (which I am sure there is). Basically I would like to have a reusable textinput class that allows custom design (quads, fonts etc) without having to copy paste the entire textinput code for each new input field.
Thanks

separating data in an array for display in Flex

I'm new to flex/flash builder, i need to read in data from a text document, then slice it into pieces i set out in my custom class.
all of this so far worked
var theCustomer:Customer=new Customer(name,address,phoneNo,comment,custNo);
custArray.addItem(theCustomer);
So now what i want to do is display only the name from each entry of the array into a combobox - and then on close it will display all the details into a list box
If i just bind the custArray to the combobox it displays name:address:phoneNo:comment:custNo as i set it out, but like i said i want only the name so how do i separate the name from each Customer entry in the array ??
Any help you be awesome and thanks in advance !!!
If I'm understanding your question correctly, I think you want to set the labelField property on the combobox. This specifies the field in the source data objects to use in the label.
<s:ComboBox dataProvider="{custArray}" labelField="name"/>
The ComboBox has several ways to specify what it should use as the "label" for each item in the dataProvider:
By default, if the elements in the dataProvider has a property named label, and that property contains a String it will display that value.
ComboBox has a labelField property that you can use to tell it where to find the "label" for each item. In your case, you could set the labelField to "name"
ComboBox has a labelFunction property that allows you to use a function (that you write) to specify what text should be displayed for each item.
I suggest using the the labelField, as that seems the most straight forward in this case:
<s:ComboBox dataProvider="{custArray}" labelField="name" />

Can not clear the JList Java

I am using NETBEANS 7 . I have on JList in my project. Here is my code
private void dateChooserSelectedDatePropertyChange(java.beans.PropertyChangeEvent evt) {
DefaultListModel model = new DefaultListModel();
listSerialNo = new JList(model);
UpdatePurchase updatePurchase = new UpdatePurchase();
Vector<String> serialNo = new Vector<String>();
serialNo = updatePurchase.getSerialNo(date);
if(serialNo.size()>0){
listSerialNo.setListData(serialNo);
}
else{
model.clear();
this.resetFields();
}
}
If I write above code then list doesn't show any thing & its not clearing the JList. But If I remove
DefaultListModel model = new DefaultListModel();
listSerialNo = new JList(model);
then list populates with some data from vector serialNo.
My problem:
I want to clear the contents of list if date value changed & no records are present for selected date.
Also I want to clear the contents of list when I click "Reset" button on my form.
Basically I can not clear the contents of jList. Please guide me. Thanks.
Try this-
listSerialNo.setListData(new Vector());
This will re-set the list data with a empty vector object.
so all the list data will be removed.
You probably already have an JList component added which is visible. In your code you create a new one while overriding the field containing the old one. Therefore you lost the 'connection' to the old (and visible) one. By updating the new one nothing visible will change as you update the wrong one.
By removing those 2 lines, you are not creating a new JList anymore and are updating the correct one.
So why not just remove those 2 lines as that seems to work according to your post.
The method setListData actually replaces the data model of your JList. Thus you do not clear the current list model but the initially assigned (which is no longer under use). Therefore, do not use setListData but add the new elements directly to your original model.