I am consuming a web service which is returning me result of type "ArrayOfKeyValueOfintstring"
I am confused how to add this data to my combo box in java.
Here is my code
org.tempuri.ThirdPartyService service = new org.tempuri.ThirdPartyService();
org.tempuri.IThirdPartyService port = service.getBasicHttpBindingIThirdPartyService();
// TODO initialize WS operation arguments here
java.lang.String key = line.trim();
// TODO process result here
String>)port.getTests(key).getKeyValueOfintstring();
com.microsoft.schemas._2003._10.serialization.arrays.ArrayOfKeyValueOfintstring result = port.getVulnerabilities(key);
EDIT
for(int i=0;i<=result.getKeyValueOfintstring().size();i++)
{
result.getKeyValueOfintstring().get(i).getKey();
result.getKeyValueOfintstring().get(i).getValue();
JOptionPane.showMessageDialog(null, "key is"+result.getKeyValueOfintstring().get(i).getKey());
JOptionPane.showMessageDialog(null, "Value is"+result.getKeyValueOfintstring().get(i).getValue());
model.addElement(new Item(key, value));
}
I have tried to get the key pair in dialog box and i got it correctly. But now i am not getting how to add them to my ComboBox. I have created table "Vector model = new Vector();" and adding it to the combo box like this "cbTestName = new JComboBox(model);"
Is it the correct way or do i need to apply anything else to add the key value pair to my combo box.
If you'll go to declaration of
port.getTests(key).getKeyValueOfintstring(),
you'll probably find its implemented as
List<KeyValuePairOfintstring>
and KeyValuePairOfintstring is looks like
...
protected Integer key;
...
protected String value;
So one of the ways you can do - is run over port.getTests(key).getKeyValueOfintstring() in the loop, and build your map with your java business objects, you want to display in Combo Box.
You can override your object's toString method as a simplest way to control how will they look in the ComboBox.
Related
I'm noob in javafx and scene builder. I want to populate tableview by selecting one item from combobox. It is possible?
i try with String val = combobox.getValue() and i put the string in SQL query in preparedStatement for directly sort but app stops at the null string value and tableview is not updated.
Thank you guys!
It is possible that the String is being initialized with the ComboBox value even before the ComboBox gets an input. In that case, the ComboBox will return a null value.
You should add an onAction event for the ComboBox which will update the string.
You can use the following code segment to do that
comboBox.setOnAction((event) -> {
val = comboBox.getValue();
//Any other action you want to carry out when an item of the combo box is selected
});
Or if you are using an FXML file and want to add the onAction event in the controller, you can use this.
public void comboBoxEvent(ActionEvent event){
val = comboBox.getValue();
} // Use this code when working with FXML files
Both these examples assume that the String var was defined globally. Just to be on the safer side, when you are comparing var to another value or storing it somewhere else, you should put it under an if condition
if(var != null)
//Code segment here
I'm currently working on a CRUD application and I have defined a LOV like this:
My question is how can I get all these return values in for example a ValueChangeListener defined like this:
public void onValueChanged(ValueChangeEvent ev){
BindingContext bctx = BindingContext.getCurrent();
oracle.binding.BindingContainer bindings = bctx.getCurrentBindingsEntry();
DCIteratorBinding iterBind = (DCIteratorBinding)bindings.get("MpStavkeulazaView5Iterator");
System.out.println("Vrijednost je" + ev.getNewValue());
}
This code only gives me the value of the list attribute, but I want the other values too.
Any other info please tell me.
First of all - using backing bean's value change listener is not ideal for such use case:
Try instead the setter on your Row Impl for the same purpose.
Remember: if you can't test your use case from BC tester, your ADF design is flawed.
Second of all: your LOV can return multiple values:
http://adfbugs.blogspot.co.uk/2009/11/returning-multiple-values-from-lov-in.html
You can bind row attributes and then get values of this bindings or just get this attributes from iterator. If you going to handle it in valueChangeListener you'll have to process updates, before getting this values:
public void onValueChanged(ValueChangeEvent ev){
BindingContainer bindings = BindingContext.getCurrent().getCurrentBindingsEntry();
DCIteratorBinding iterBind = (DCIteratorBinding)bindings.get("MpStavkeulazaView5Iterator");
System.out.println("Vrijednost je" + ev.getNewValue());
ev.processUpdates(FacesContext.getCurrentInstance());
Row row = iterBind.getCurrentRow();
System.out.println("Proizvod: " + row.getAttribute("Proizvod"));
System.out.println("Jmjere: " + row.getAttribute("Jmjere"));
}
However its may be better to use Transient attribute in your ViewObject and do calculations there?
I have a JTable, and I want validate data in the first column. When the user type the entry in any cell in the first column and click in another cell (focus lost), I want show message that the entry is false, and focus again in the cell until the entry is valid.
First I thought that the cell is like the JTextFiled, so I have tried the method addFocusListener(...) but it doesn't work!
table.getValueAt(0, 0).addFocusListener(
new FocusListener() {
public void focusGained(FocusEvent e) {
}
public void focusLost(FocusEvent e) {
for (int n = 0; n <= table.getValueAt(0, 0).toString().length(); n++) {
if (Character.isDigit(table.getValueAt(0, 0).toString().charAt(n)) == false) {
JOptionPane.showMessageDialog(null,
"Error: code is a number !", "Error Message",
JOptionPane.ERROR_MESSAGE);
break;
} else
break;
}
}
}
}
});
creation of JTable:
String [][]data={ {"-","-","0","-"},
{"-","-","0","-"},
{"-","-","0","-"},
{"-","-","0","-"},
{"-","-","0","-"},
{"-","-","0","-"},
{"-","-","0","-"},
{"-","-","0","-"} };
String[] header = {"Code Projet", "Description", "Duree", "Taches anterieurs"};
table = new JTable(data, header);
The TableCellEditor that is added to a JTable will do any validation on the values that are entered into a JTable.
From the Java Table Tutorial:
The automatic checking of user-entered strings occurs when the default
editor attempts to create a new instance of the class associated with
the cell's column. The default editor creates this instance using a
constructor that takes a String as an argument. For example, in a
column whose cells have type Integer, when the user types in "123" the
default editor creates the corresponding Integer using code equivalent
to new Integer("123"). If the constructor throws an exception, the
cell's outline turns red and refuses to let focus move out of the
cell. If you implement a class used as a column data type, you can use
the default editor if your class supplies a constructor that takes a
single argument of type String.
So, if you want to just make sure the user enters in an Integer (which is what it looks like from your code), you can set the column type to a type of Integer .
table.setDefaultEditor(Integer.class,
new IntegerEditor(0, 100));
Now the DefaultCellEditor will do the checking for you, and throw an exception if the type is not correct. You will just have to make sure you catch the exception and deal with it properly.
I think tutorial I linked above on creating tables would be very helpful for you, especially the part I linked to which talks about validating user text.
I have a HashSet that I created and this is what it contains. It will contain more later on, this is pasted from standard out when I did a toString on it. Just to show the contents.
foo.toString(): Abstractfoo [id=2, serial=1d21d, value=1.25, date=2012-09-02 12:00:00.0]
INFO [STDOUT] price.toString(): Abstractfoo [id=1, serial=1d24d, value=1.30, date=2012-09-19 12:00:00.0]
I have a List that I also have and I need to compare the two. One of the elements in List is:
Bar.toString(): Bar [id=1d21d, name=Dell, description=Laptop, ownerId=null]
Here is what I am trying to do...
Bar contains all of the elements I want foo to have. There will only be one unique serial. I would like my program to see if an element in the list that is in HashSet contains the id for bar. So serial == id.
Here is what I've been trying to do
Removed code and added clearer code below
I've verified the data is getting entered into the HashSet and List correctly by viewing it through the debugger.
foo is being pulled from a database through hibernate, and bar is coming from a different source. If there is an element in bar I need to add it to a list and I'm passing it back to my UI where I'll enter some additional data and then commit it to the database.
Let me know if this makes sense and if I can provide anymore information.
Thanks
EDIT: Here is the class
#RequestMapping(value = "/system", method = RequestMethod.GET)
public #ResponseBody
List<AbstractSystem> SystemList() {
// Retrieve system list from database
HashSet<AbstractSystem> systemData = new HashSet<AbstractSystem>(
systemService.getSystemData());
// Retrieve system info from cloud API
List<SystemName> systemName= null;
try {
systemName = cloudClass.getImages();
} catch (Exception e) {
LOG.warn("Unable to get status", e);
}
// Tried this but, iter2 only has two items and iter has many more.
// In production it will be the other way around, but I need to not
// Have to worry about that
Iterator<SystemName> iter = systemName.iterator();
Iterator<AbstractSystem> iter2 = systemData .iterator();
while(iter.hasNext()){
Image temp = iter.next();
while(iter2.hasNext()){
AbstractPricing temp2 = iter2.next();
System.out.println("temp2.getSerial(): " + temp2.getSerial());
System.out.println("temp.getId(): " + temp.getId());
if(temp2.getSerial().equals(temp.getId())){
System.out.println("This will be slow...");
}
}
}
return systemData;
}
If N is the number of items in systemName and M is the number of items in systemData, then you've effectively built an O(N*M) method.
If you instead represent your systemData as a HashMap of AbstractSystem by AbstractSystem.getSerial() values, then you just loop through the systemName collection and lookup by systemName.getId(). This becomes more like O(N+M).
(You might want to avoid variables like iter, iter2, temp2, etc., since those make the code harder to read.)
EDIT - here's what I mean:
// Retrieve system list from database
HashMap<Integer, AbstractSystem> systemDataMap = new HashMap<AbstractSystem>(
systemService.getSystemDataMap());
// Retrieve system info from cloud API
List<SystemName> systemNames = cloudClass.getImages();
for (SystemName systemName : systemNames) {
if (systemDataMap.containsKey(systemName.getId()) {
System.out.println("This will be slow...");
}
}
I used Integer because I can't tell from your code what the type of AbstractSystem.getSerial() or SystemName.getId() are. This assumes that you store the system data as a Map elsewhere. If not, you could construct the map yourself here.
I am trying to search for UserName and return values onto jComboBox, here is the code
public void actionPerformed(java.awt.event.ActionEvent e) {
sr = new Search(((String) jComboBoxReceiver.getSelectedItem()));
usrList = sr.searchUser();
String[] userList = new String[usrList.size()] ;
for(int i=0;i<usrList.size();i++){
userList[i]= usrList.get(i).getUserName();
}
model = new DefaultComboBoxModel(userList);
jComboBoxReceiver.setModel(model);
}
after you click to somewhere else or click enter,it will conduct the search, however, it will go search for the first item again, which is very confusing... then i tried using key Pressed
if(e.getKeyCode()==13){
sr = new Search(((String) jComboBoxReceiver.getSelectedItem()));
usrList = sr.searchUser();
String[] userList = new String[usrList.size()] ;
for(int i=0;i<usrList.size();i++){
userList[i]= usrList.get(i).getUserName();
}
model = new DefaultComboBoxModel(userList);
jComboBoxReceiver.setModel(model);
}
And this one does not react at all.
You need to set the listener(s) on the Editor not the ComboBox itself. See the answer here:
Detecting when user presses enter in Java
Wow, you're rebuilding a ComboBoxModel each time ? Isn't it a little expensive ? You know there is a MutableComboBoxModel, also implemented by DefaultComboBoxModel that would allow you to add/remove elements from you combobox without rebuilding its model each time ?
Concerning your question, I don't understand the statement
However, if i do that, it does perform correctly, however, it will go search for the first item again
Do you mean your JComboBox starts to blink with content being modified each time ?
if so, maybe is it because your ActionListener is linked to JComboBox, which content changes continuously.
Anyway, i suggest you add some logs, like
sr = new Search(((String) jComboBoxReceiver.getSelectedItem()));
DefaultComboBoxModel model = (DefaultComboBoxModel) jComboBoxReceiver.getModel();
model.remvoeAllElements();
usrList = sr.searchUser();
String[] userList = new String[usrList.size()] ;
for(int i=0;i<usrList.size();i++){
String username = usrList.get(i).getUserName();
System.out.println(username); // feel free to instead use one loger
model.addElement(username);
}
Besides, i would tend to suggest you an other approach, in which combo box model don't contain simple Strings, but rather User objects, with a ListCellRenderer displaying only the user name.
IMO, what will really be confusing for your users is to have the content and selection of a combo box changed as soon as they select one of its options.
Anyway, if you really want to do that, then you should remove the action listener (or deactivate it) before changing its content, and re-add it (or reactivate it) after :
public void actionPerformed(java.awt.event.ActionEvent e) {
sr = new Search(((String) jComboBoxReceiver.getSelectedItem()));
usrList = sr.searchUser();
String[] userList = new String[usrList.size()] ;
for(int i=0;i<usrList.size();i++){
userList[i]= usrList.get(i).getUserName();
}
model = new DefaultComboBoxModel(userList);
jComboBoxReceiver.removeActionListener(this);
jComboBoxReceiver.setModel(model);
jComboBoxReceiver.addActionListener(this);
}