For a Java Schoolproject I would like to have a table from witch you can select a Item that then shows up on a new window. In that window you can change things like ComboBoxes and others. My only problem is, that I dont know how to select the Item of the ComboBox I need. All the ComboBoxItems are Objects and I dont know how to handle this.
My ComboBoxItem looks like this:
Apprentice [person=Person, DB ID: 9, Kappa Kappa, Kappastrasse 21,
CityID: 4521, kappa.kappa#kappa.ch, idpersonen=9,
vertragsstart=2020-01-02, ausbildungsct=2, id=6]
Now, my Question is, how do I select the ComboBoxitem where the id=6, all the things I found needed the whole Object to select a special Item. How would you guys go at this problem?
Good luck and thanks for the Help.
Bono
All I had to do was a really simple while with a for and a if.
int trys = 0;
while (0 == apprenticeComboBoxZeugnis.getItemCount() && trys < 10000) {
System.out.println(apprenticeComboBoxZeugnis.getItemCount());
for (int i = 0; i < apprenticeComboBoxZeugnis.getItemCount(); i++) {
apprenticeComboBoxZeugnis.setSelectedIndex(i - 1);
int spacko = getApprenticeCombo();
if (spacko == lehrlingsid) {
TableFilterListenerZeugnis tableFilterListenerZeugnis = new TableFilterListenerZeugnis(
this);
tableFilterListenerZeugnis.updateNoten();
break;
}
}
trys++;
}
This first trys until the number of Objects is not = 0 and after that looks at every object, cuts out the id with getApprenticeCombo() and compares it with my id I allready have. If they match it breaks out and is done with it.
Related
my Main contains a conveyor, which transports a carton (my agent) from A to B. I added a radio button with which the user can choose the size of the box ("small", "medium", "big" & "very big"). I now want every carton to save the value of the radio button. I tried a lot of different stuff like linking it to a parameter, but I didn't manage to figure it out.
Picture of my model:
the code I used for the radio button is:
if( value == 0 )
radioValue = 1;
else if( value == 1 )
radioValue = 2;
else if( value == 2 )
radioValue = 3;
else if( value == 3 )
radioValue = 4;
radioValue being the parameter in the Main i linked my radio-button to.
How do I give this parameter to the agent and how do I read it out later?
Thanks in advance for helping!
in your Kist agent create a variable called size of type int.
in the source block, in the properties, on the "on at exit" action, under the action section of the properties write the following code:
agent.size=radioValue;
This is one of the most basic things to do in AnyLogic... so I suggest for you to go through a training before even starting to work on any model. This can be done by going through the tutorials, the anylogic in 3 days book or my course here: noorjax.teachable.com/p/anylogic
later you can use agent.size to access the size of your agent, in any of the blocks.
Without seeing more of the code, my general suggestion would be to consider looping through the agents (e.g., with a for loop) and having them all set a local variable cartSize equal to radioValue (I wouldn't use "size" because its also used for agentsets I believe). Just spitballing here, but something like this?
for (int x = 0; x < carton.size(); x++) {
carton.get(x).cartSize = radioValue;
}
I'm writing a selenium test script for my student management system. I have a situation where I need to enter values and click the same button 15 times. So, I used a for loops for the scenario.
Here is the screen I need to test.
So, I need to add two values to mark range text boxes and select grade from the drop down list and click add button. I need to do this scenario for 15 times.
Here are the values I need to enter
Here is the drop down list.
I tried following scenario for this.
for(int x=95; x<=11; x=x-6){
driver.findElement(By.xpath("//input[#type='number']")).sendKeys(""+x);
for(int y=100; y<=16; y=y-6){
driver.findElement(By.xpath("(//input[#type='number'])[2]")).sendKeys(""+y);
for(int z=1; z<=15; z++){
Select mark2 = new Select(driver.findElement(By.xpath("//select[#id='gradeSelector']")));
mark2.selectByValue(""+z);
driver.findElement(By.xpath("//input[#value='Add']")).click();
}
}
}
but, nothing happens.
Thanks in advance. :)
Try following:
int x=95, y=100;
for(int z=1; z <=15; z++){
driver.findElement(By.xpath("//input[#type='number']")).sendKeys(""+x);
driver.findElement(By.xpath("(//input[#type='number'])[2]")).sendKeys(""+y);
Select mark2 = new Select(driver.findElement(By.xpath("//select[#id='gradeSelector']")));
//mark2.selectByValue(""+z);
mark2.selectByIndex(z);
driver.findElement(By.xpath("//input[#value='Add']")).click();
x=x-6;
y=y-6;
}
It's just the for loop logic is incorrect - the x<=11 condition never evaluates to true, replace:
for(int x=95; x<=11; x=x-6) {
with:
for(int x=95; x>=11; x=x-6) {
It may seem a silly question but I've tried a variety of ways and have no idea how to solve. I am new to android programming and am completing a spinner with a list of objects. When you select the object in need spinner pick up the object ID and the number of animals of the same.
My last attempt was is, but I know he does not enter the IF because the equals can not compare the list with a string, but do not know how to make this comparison.
String posicaoSpinner = String.valueOf(sLote.getSelectedItemPosition());
int idLote =0;
int qtdAnimais;
for (int i = 0; i < loteList.size(); i++) {
for (Lote lotes : loteList) {
if (posicaoSpinner.equals(loteList.get(i))) {
idLote = Integer.valueOf(String.valueOf(lotes.get_id()));
qtdAnimais = lotes.getQtd_animais() - itemPovoamento.getAnimais();
lotes.setQtd_animais(qtdAnimais);
}
}
}
Any help is welcome. Thank you so much
If you need the value of the selected item you can simply use .getSelectedItem() of the spinner. So:
String posicaoSpinner = posicaoSpinner.getSelectedItem().toString();
should do the trick?
I'm developing a tool which is supposed to save the content from a JTable to a CSV file, I have this "add row" button to add a new row, but I need the last row to be filled on every cell and then be allowed to add a new row.
Here is the code I have, but this doesn't create the new row nor throw any errors on console.
private void btnAddRowActionPerformed(java.awt.event.ActionEvent evt) {
for(int i=0;i<=jTable1.getColumnCount();i++){
if(jTable1.isRowSelected(jTable1.getRowCount())){
do{
model.insertRow(jTable1.getRowCount(), new Object[]{});
} while(jTable1.getValueAt(jTable1.getRowCount(), i).equals(""));
}
}
}
Okay, so what you seem to be saying is, the user should not be allowed to add a new row until the last row is fully completed...
You existing loop doesn't make sense, basically, for each column, you are checking to see if the last row is selected, and inserting a new row for each column which is blank ("")...?
Remember, generally Java is zero indexed, this means, the last row is actually jTable1.getRowCount() - 1, so, it's unlikely that your if isRowSelected would be true, which is actually a good thing, cause otherwise you would have had a real mess...
Assuming I understand your question correctly (as it's a little vague), you could try something more like this...
boolean rowCompleted = true;
int lastRow = jTable1.getRowCount() - 1;
if (jTable1.isRowSelected(lastRow)) {
for (int col = 0; col < jTable1.getColumnCount(); col++) {
Object value = jTable.getValueAt(lastRow, col);
if (value == null || value.toString().trim().isEmpty()) {
rowCompleted = false;
break;
}
}
}
if (rowCompleted) {
// Insert new row...
} else {
// Show error message
}
Maybe use a TableModelListener.
Every time a cell is updated on the last row of the table you check to make sure all columns have data. If all columns have data you enable the "Add Row" button, otherwise you disenable the button.
I was checking this post and I used the code posted by MadProgrammer, but I made a few modifications and I got this working properly according to your need. If you want you can ask me for the project and I can happily provide it to you
private void btnAddRowActionPerformed(java.awt.event.ActionEvent evt) {
boolean rowCompleted;
int lastRow = jTable1.getRowCount()-1;
if(jTable1.isRowSelected(lastRow)){
for(int col=0;col<jTable1.getColumnCount();col++){
Object value = jTable1.getValueAt(lastRow, col);
if(value == null || value.toString().trim().isEmpty()){
rowCompleted=false;
}
else{
rowCompleted=true;
}
if(rowCompleted==true){
model.insertRow(jTable1.getRowCount(), new Object[]{});
}
else{
JOptionPane.showMessageDialog(null, "Something went worng. Try this:\n - Please select a row before adding new row.\n - Please verify there are no empty cells","Processing table's data",1);
}
break;
}
}
else{
JOptionPane.showMessageDialog(null, "Something went wrong. Verify this:\n - There is not any row selected.\n - You can only create new rows after last row","Processing table's data",1);
}
}
I hope this could help you, but first say thanks to MadProgrammer :D
I want selenium to press "TAB" for me and then write something in the focused field, now I know that I can use
sendKeys(Keys.TAB)
But as I understand it That require a locator behind it, I want to test the tab order of my page and to do so, I want to be able to focus on my first element only, then tab my way through the page like this:
--THE ELEMENTS IN THE TAB ORDER THEY ARE SUPOSED TO BE--
String[] elementArray = {"firstname","lastname", "phone", "email"};
for(int x = 0; x < 4; x = x+1)
{
WebElement theElement = driver.findElement(By.id(elementArray[x]));
if (x == 0) {driver.theElement.sendKeys(x)}
else{driver.(TheCurrentlyFocusedElement).sendKeys(x)}
String elementval = theElement.getAttribute("value");
assertEquals(x, elementval);
(TheCurrentlyFocusedElement).sendKeys(Keys.TAB);
}
So the question is, is there a method I can use that allows me to use the currently focused element as a locator? i.e.:
driver.focusedElement().sendKeys(Keys.TAB); ?
This is what you are looking for -
driver.switchTo().activeElement();
I have just had to do something similar but found that sending tab didn't actually change focus (it did when manual testing, just not via Selenium).
I wanted to prove the order of input fields on screen (positioning was more important then tabbing in this case), so had to do something like the following;
String[] elementArray = {"firstname","lastname", "phone", "email"};
List<WebElement> inputFields = driver.findElements(By.cssSelector("input"));
for(int x = 0; x < 4; x++)
{
assertEquals(elementArray[x], inputFields.get[x]);
}
I would probably create a list rather than an array, and compare those, but you should get the gist.
Yes, I know that this is quite a simplified approach and that this doesn't actually prove that fields are definitely in that order once rendered, but it gives you some security for regression.