SWT/Jface cell text not getting saved in Model - java

I asked a question related to the issue I was having before, but a new one arose. Originally, I wanted the capability of highlighting a row in my table with a single click, and editing a cell with a double click. greg-449 gave me the solution to the problem (getting rid of the FocusCellManager and simply using EditingSupport along with the other classes I had, and setting the editorActivationStrategy to have the MOUSE_DOUBLE_CLICK_SELECTION bit. However, now when I type text into a cell, the text is not saved into my model. On the other hand, when I use the FocusCellManager, the text is indeed saved.
Now, I have to use CellEditors, as there is already a very long list of CellEditors set up for the specific columns. I am not sure if it is possible to use EditingSupport along with CellEditors, or if it necessary at all. I am attaching some snippets.
Relevant Section of DatumTableViewer.java:
FocusCellOwnerDrawHighlighter drawHighlighter = new FocusCellOwnerDrawHighlighter(this); //removed this to fix the highlighting issue
final TableViewerFocusCellManager mgr = new TableViewerFocusCellManager(this, drawHighlighter); //removed this to fix the highlighting issue.
final ColumnViewerEditorActivationStrategy editorActivationSupport = getEditorActivationStrategy();
int tableKeyboardTraversalFeature = getKeyboardTraversalFeature();
TableViewerEditor.create(this, mgr, editorActivationSupport, tableKeyboardActivationFeature);
DatumColumnEmnum[] tableColumnsAsArray = getTableColumnsAsArray();
createTable(this.getTable(), tableColumnsAsArray);
setColumnProperties(properties.getPresentedColumnNames); //properties is passed into constructor
setCellEditors(tableColumnsAsArray); //I think this doesn't work properly without FocusCellManager
DatumCellModifier modifier = new DatumCellModifier(this, properties, myExpressionDataProvider); // myExpressionDataProvider is passed into constructor)
setCellModifier(modifier);
setContentProvider(contentProvider);
setInput(_myDatumList); // myDatumList set before this snippet
}
setCellEditors:
private void setCellEditors(DatumColumnEnum[] columns)
{
cellEditor[] editors = new CellEditor[columns.length];
for(int i = 0; i < columns.length; i++){
DatumColumnEnum columnID = columns[i];
switch(columnID) // cases have been trimmed to shorten this snippet
{
case DATUM_ID_COLUMN:
case DATUM_NUMBER_COLUMN:
case DATUM_NAME_COLUMN:
editors[i] = new TextCellEditor(this.getTable());
((Text) editors[i].getControl()).setTextLimit(60);
break;
}
}
this.setCellEditors(editors);
}
I suspect these snippets contain the problem, but if not, then please let me know and I'll add the other functions as well.

Related

adding a field name as a method parameter

I have a Swing form where I want to detect edited field data so I can update the data via a web service. There doesn't seem to be a simple way to do this as there are a plethora of medium to complex code examples for this, most of which are over my head! However, I think I found a simple solution that will fit my needs and I wanted to run it by the group for input / suggestions. FYI: it may not matter but know that I am using NetBeans so things like listeners are auto-coded by the app.
Step 1: When I load the form, I am saving all the data to a Class array so I know the starting point of the each field. Here is the code for that load:
public void coSetSearchDetail(String coDetail){
String[] text;
System.out.println("SingleCO Result: "+ coDetail);
text = coDetail.split("\\|");
txtName_CoDetail.setText(text[1]);
txtAddr_CoDetail.setText(text[2]);
txtAddr2_CoDetail.setText(text[3]);
txtCity_CoDetail.setText(text[4]);
txtState_CoDetail.setText(text[5]);
txtZip_CoDetail.setText(text[6]);
stringarrayCoDetails[0] = text[0];
stringarrayCoDetails[1] = text[1];
stringarrayCoDetails[2] = text[2];
stringarrayCoDetails[3] = text[3];
stringarrayCoDetails[4] = text[4];
stringarrayCoDetails[5] = text[5];
stringarrayCoDetails[6] = text[6];
java.awt.CardLayout card = (java.awt.CardLayout)pnlMain.getLayout();
card.show(pnlMain, "pnlCoDetail");
}
Step 2: I created a Lost Focus event listener for one field and am testing the current value of the field against the array:
private void txtName_CoDetailFocusLost(java.awt.event.FocusEvent evt) {
if (!(txtName_CoDetail.getText().equals(stringarrayCoDetails[1]))){
createEditBorder();
}
}
private void createEditBorder (){
Border border = BorderFactory.createLineBorder(Color.RED, 2);
txtName_CoDetail.setBorder(border);
}
Besides the general question of "is this an OK approach?", I would like to be able to pass the field name to the createEditBorder method so the listener for each data field can call it and I have one method for "edited text" formatting.

Issue changing value in ArrayList from GUI

I have an ArrayList called conditionList which stores condition names.
Whenever one is added/edited or deleted, the Lists on my GUI update no problems.
You can see below that i use 2 models... a DefaultListModel called condListModel and a DefaultComboBoxModel called conditionModel.
The code i have below is for the method editCondition(), at this stage the text is already changed on the GUI and is being submitted here. On my GUI, after ive submitted the change, the ComboBox and JList change no problem so im sure that the model changes are correct.
HOWEVER MY PROBLEM IS: When i save the ArrayList conditionList through serialization, and then load it back up, the change is gone. SO I think there is problem in my code with changing the String Value in the ArrayList(named conditionList), can anyone have a look and see if u notice an issue
String conString = jListCondition.getSelectedValue().toString();
for(String c: conditionList)
{
if(conString.compareTo(c) == 0)
{
String temp = entConName.getText();
c = temp;
//edit the Condition jList model
int x = condListModel.indexOf(conString);
condListModel.setElementAt(temp, x);
jListCondition.setModel(condListModel);
//edit the Condition comboBox model
int i = conditionModel.getIndexOf(conString);
conditionModel.insertElementAt(temp, i);
conditionModel.removeElement(conString);
entCondition.setModel(conditionModel);
//reset buttons
editConConfirm.setEnabled(false);
editCon.setEnabled(false);
deleteCon.setEnabled(false);
entConName.setText("");
addCon.setEnabled(true);
}
}

Java Default Highlighter

Im using the DefaultHightlighter.DefaultHightlighterPainter to highlight text within a java text pane.
I want to remove all highlights (there could be more than one string highlighted) and want it to return the locations of the strings where the highlight has been removed, so obviously I cant use
pseudoCodeTextPane.getHighlighter().removeHighlight(highlight);
Can anyone help?
Thanks
How about something like
Highlighter.Highlight[] highlights = pseudoCodeTextPane.getHighlighter().getHighlights();
int[] startOffsets = new int[highlights.length];
int[] endOffsets = new int[highlights.length];
for (int i = 0; i < highlights.length; ++i) {
startOffsets[i] = highlights[i].getStartOffset();
endOffsets[i] = highlights[i].getEndOffset();
}
pseudoCodeTextPane.getHighlighter().removeAllHighlights();
// now do whatever processing you want to do with the highlight locations
If you remove all highlights (I suppose with removeAllHighlights) you can getHighlights before that and use the information you receive there.

Netbeans - Entering items in a jComboBox

I have generated a GUI from netbeans in which I have placed a combobox too.
By default, the items in combobox are item1, item2, item3, item4.
But I want my own items. Netbeans doesn't allow editing generated code so how can i edit the comnbobox according to me.
Note: I know one method by editing the "model" property of that jComboBox but I don't want to do it like that because I want various items (which are in an array) in that jComboBox so I want to pass that array in that jComboBox like as follows:
jComboBox2 = new javax.swing.JComboBox();
String [] date = new String[31];
for(int i = 0; i < 31; i++) {
date[i] = i + 1;
}
jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(date));
There are 2 approaches I am aware of:
Simple approach - After the call to initComponents() in the constructor add you code to build your model and call jComboBox2.setModel(myModel) to set it. So the constructor would look something like:
public SomeClass() {
initComponents();
String [] date = new String[31];
for(int i = 0; i < 31; i++) {
date[i] = i + 1;
}
jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(date));
}
Complex approach - add a readable property that holds the desired model. For example:
private ComboBoxModel getComboBoxModel()
{
String[] items = {"Item A", "Item B", "Item C"};
return new DefaultComboBoxModel(items);
}
Then, in the jComboBox2 property sheet, click the button to edit the model.
In the editor panel change the dropdown from Combo Box Model Editor to Value from existing component.
Select Property. Choose the comboBoxModel property. Click OK
I tried the second way once. Never really used it again. Too much work, no real much gain. Plus it displays an empty combo box in the designer which just makes layout harder.
I use the first approach, plus use NetBean's model editor to supply some representative values for the model. That gives me the sensible size behavior in the designer at the cost of one unnecessary line in initComments().
Using Netbeans NEON and other netbeans version
1. Go to the properties of the combobox
2. Then go to model
you can inject your code by using "custom code" feature in the GUI editor for the "model" of combobox
public NewJFrame() {
initComponents();
reformatComboBox();
}
private void reformatComboBox() {
JComboBoxName.removeAllItems();
JComboBoxName.addItem("item1");
JComboBoxName.addItem("item2");
}
Completing blurec answer (I cannot comment yet), in the GUI editor select the comboxbox, go properties, then model, then hit the three dots. Then select Custome Code and add your code, for instance:
new DefaultComboBoxModel<>(functionThatReturnsAnStringArray())
For the posterity:
Right click the ComboBox and select Customize Code. Here at the comboBox.setModel, in the left select custom property.
After new String, add your values in the following form:
Value 1: Integer.toString(myInt1)
Value 2: Integer.toString(myInt2)
If your variables are int of course. If not just put the String variable and you are done.
Hope it helps.

GWT - ListBox - pre-selecting an item

I got a doubt regarding pre-selecting(setSelectedIndex(index)) an item in a ListBox, Im using Spring + GWT.
I got a dialog that contains a panel, this panel has a FlexPanel, in which I've put a couple ListBox, this are filled up with data from my database.
But this Panel is for updates of an entity in my database, thus I wanted it to pre-select the current properties for this items, allowing the user to change at will.
I do the filling up in the update method of the widget.
I tried setting the selectedItem in the update method, but it gives me an null error.
I've searched a few places and it seems that the ListBox are only filled at the exact moment of the display. Thus pre-selecting would be impossible.
I thought about some event, that is fired when the page is displayed.
onLoad() doesnt work..
Anyone have something to help me out in here?
I really think you can set the selection before it's attached and displayed, but you have to have added the data before you can select an index. If this is a single select box you could write something like this:
void updateListContent(MyDataObject selected, List<MyDataObject> list){
for (MyDataObject anObject : list) {
theListBox.addItem(anObject.getTextToDisplay(), anObject.getKeyValueForList());
}
theListBox.setSelectedIndex(list.indexOf(selected));
}
If this is a multiple select box something like this may work:
void updateListContent(List<MyDataObject> allSelected, List<MyDataObject> list){
for (MyDataObject anObject : list) {
theMultipleListBox.addItem(anObject.getTextToDisplay(), anObject.getKeyValueForList());
}
for (MyDataObject selected : allSelected) {
theMultipleListBox.setItemSelected(list.indexOf(selected), true);
}
}
(Note I haven't actually compiled this, so there might be typos. And this assumes that the selected element(s) is really present in the list of possible values, so if you cant be sure of this you'll need to add some bounds checking.)
I've been happily setting both the values and the selection index prior to attachment so as far as I'm aware it should work. There's a bug however when setting the selected index to -1 on IE, see http://code.google.com/p/google-web-toolkit/issues/detail?id=2689.
private void setSelectedValue(ListBox lBox, String str) {
String text = str;
int indexToFind = -1;
for (int i = 0; i < lBox.getItemCount(); i++) {
if (lBox.getValue(i).equals(text)) {
indexToFind = i;
break;
}
}
lBox.setSelectedIndex(indexToFind);
}
Pre-selection should work also with setValue()-function. Thus, no complicated code is needed.

Categories

Resources