If i want to add an ArrayList to a JList, it is not shown in the list, but the array contains the items. How could i fix this?
public void updateLeftList(){
// Enter the search text
interFace.Search.setText(this.search);
// Get the left list
JList leftList = interFace.LeftList;
leftList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
// Define the arraylists
allPdf = new ArrayList();
curPdf = new ArrayList();
addPdf = new ArrayList();
// Get Files from manuals folder
File files = new File(configFile.getProperty("dir") + "/" + this.taal);
File[] listFiles = files.listFiles();
for(int i = 0; i < listFiles.length; i++){
if(listFiles[i].getName().endsWith(".pdf") && listFiles[i].isFile()){
allPdf.add(listFiles[i].getName().toString());
}
}
leftList.setListData(allPdf.toArray());
}
use ListModel to add the data:
1. create new instance of DefaultListModel
2. add your file names to the list model
3. set it as your JList list model
for(Object item : arrayList.toArray()){
((DefaultListModel) list1.getModel()).addElement(item);
}
Related
I am new to test automation and i am currently working on a personal project
I have this method in which it finds all the links for a section on the page, clicks on each link, irritates through each tab and then gets the title of each page
However, i want a way to verify the titles of these links against a list of expected titles
What would be the best approach to modify this in order to do this? would it be better to store in an array and then assert/verify each title separately?
I have tried a few ways to assert by changes return type to String and also to List but with no luck
public void linksAreWorkingDashBoardLeftPanal() throws Exception {
List<WebElement> li_All = links_myAccountNav.findElements(By.tagName("a"));
for(int i = 0; i < li_All.size(); i++){
String clickOnLinkTab = Keys.chord(Keys.CONTROL, Keys.ENTER);
links_myAccountNav.findElements(By.tagName("a")).get(i).sendKeys(clickOnLinkTab);
Thread.sleep(5000);
}
//Opens all the tabs
Set<String> getTitleinWindow = driver.getWindowHandles();
Iterator<String> it = getTitleinWindow.iterator();
while(it.hasNext())
{
driver.switchTo().window(it.next());
System.out.println(driver.getTitle());
}
Modify the while loop as follows;
List<String> expectedTitleList = new ArrayList<>();
// Get the expected titles in a list.
// You have to initiate and add expected titles to this list, before running the while loop.
List<String> actualTitleList = new ArrayList<>();
while(it.hasNext())
{
driver.switchTo().window(it.next());
actualTitleList.add(driver.getTitle());
}
// Create copies of each list;
Set<String> expectedSet = new HashSet<>(expectedTitleList);
Set<String> actualSet = new HashSet<>(actualTitleList);
Set<String> expectedOnlySet = new HashSet<>(expectedSet);
Set<String> actualOnlySet = new HashSet<>(actualSet);
expectedOnlySet.removeAll(actualSet);
actualOnlySet.removeAll(expectedSet);
// Check if expectedOnlySet and actualOnlySet are empty
// If both of them are empty then all titles are received as expected.
boolean isEmpty = (expectedOnlySet.size() == 0 && actualOnlySet.size() == 0);
if (expectedOnlySet.size() > 0 || actualOnlySet.size() > 0) {
// If expectedOnlySet contain any title; it means those titles are not opened in the browser.
// If actualOnlySet contain any title; it means those titles are not expected titles.
StringBuilder message = new StringBuilder();
for (String x: expectedOnlySet) {
message.append("Did not receive: ");
message.append(x).append(", ");
}
for (String y: actualOnlySet) {
message.append("Not expected but received: ");
message.append(y).append(", ");
}
Assert.assertTrue(false, message,toString());
}else {
Assert.assertTrue(true);
}
Here you can use removeAll() method in Collections. Check the documentation
We have to check both scenarios;
Whether all expected titles are received
All received titles are expected
Thanks! I took what i needed from your code and it worked perfectly.
this work and i added my own assertion
List<WebElement> li_All = links_myAccountNav.findElements(By.tagName("a"));
for(int i = 0; i < li_All.size(); i++){
String clickOnLinkTab = Keys.chord(Keys.CONTROL, Keys.ENTER);
links_myAccountNav.findElements(By.tagName("a")).get(i).sendKeys(clickOnLinkTab);
}//Opens all the tabs
Set<String> getTitleinWindow = driver.getWindowHandles();
Iterator<String> it = getTitleinWindow.iterator();
// Get the expected titles in a list.
// You have to initiate and add expected titles to this list, before running the while loop.
List<String> expectedTitleList = new ArrayList<>();
expectedTitleList.add("Page 1 title");
expectedTitleList.add("Page 2 title");
expectedTitleList.add("Page 3 title");
expectedTitleList.add("Page 4 title");
expectedTitleList.add("Page 5 title");
expectedTitleList.add("Page 6 title");
expectedTitleList.add("Page 7 title");
List<String> actualTitleList = new ArrayList<>();
while(it.hasNext())
{
driver.switchTo().window(it.next());
actualTitleList.add(driver.getTitle());
}
//System.out.println(expectedTitleList);
// System.out.println(actualTitleList);
if(actualTitleList.equals(expectedTitleList)).....
I want to copy specific data from one list to another. It shows
ERROR:
java.lang.NullPointerException
at controllers.WebForms.getMenu(WebForms.java:134)
at Routes$$anonfun$routes$1$$anonfun$applyOrElse$2$$anonfun$apply$2.apply(routes_routing.scala:222)
List<menu> allMenus = menu.find.all();
List<menu> selectedMenu=null;
System.out.println("selected menu:"+selectedMenu);//prints: null
int count = 0;
for (models.menu m : allMenus) {
System.out.println("m:"+m);
if (m.r_id == r_id) {
count=1;
System.out.println("r_id:"+m.r_id+" "+m.item);
//prints: r_id:1 Noodles
selectedMenu.add(m);
//throws NULLPointer Exception
System.out.println("selected menu inside loop:"+selectedMenu);
}
}
List<menu> selectedMenu = new ArrayList<>();. You have to initialise the list before you perform operations on it.
List selectedMenu = new ArrayList<>();
I'm trying to implement a palette. I try to set a default selected list but it's empty.
myLists:
// here I get a Set of the categorys which are already in that group
Set<Category> selectedCategorysSet = new HashSet<Category>();
selectedCategorysSet = group.getCategorys();
// here I get all categorys exists
List<Category> listCategory = new ArrayList<Category>();
listCategory = catDao.getAll(Category.class);
List<Category> selectedCats = new ArrayList<Category>();
List<Category> tmpList = new ArrayList<Category>();
// the palette doesnt accept an Set so I added the set to a List
selectedCats.addAll(selectedCategorysSet);
// here I delete every Category from the whole List which is already selected (stored in a temporary list)
for(Category catList:listCategory){
for(Category cat:selectedCategorysSet){
if(cat.getCategoryId() == catList.getCategoryId()){
tmpList.add(catList);
}
}
}
listCategory.removeAll(tmpList);
/*
two multiple select boxes which switches items between each other
*/
IChoiceRenderer<Category> renderer = new ChoiceRenderer<Category>("title","categoryId");
final Palette<Category> palette = new Palette<Category>("palette",
new ListModel<Category>(selectedCats),
new CollectionModel<Category>(listCategory),
renderer, 10, false);
I already debugged that code, it works but my selected values are empty.
Here is a picture of my debugged variables:
but the selected field is still empty!
what am I doing wrong?
You should not delete every Category from the whole List which is already selected.
Palette component must store whole list of values in it's choicesModel which is listCategory in your code.
So, just remove following code from your implementation:
for(Category catList:listCategory){
for(Category cat:selectedCategorysSet){
if(cat.getCategoryId() == catList.getCategoryId()){
tmpList.add(catList);
}
}
}
listCategory.removeAll(tmpList);
Search by id method:
public class ClientsDetailsList {
public ArrayList <ClientDetails> aListOfClientDetails;
public ClientDetails getClientDetails(String givenID) {
boolean found = false;
int index = 0;
for(;index<aListOfClientDetails.size();index++){
if((aListOfClientDetails.get(index).ClientID.equals(givenID)))
found = true;
}
if(found)
return aListOfClientDetails.get(index);
else return null;
}
}
aListOfClientDetails List format It reads a file and creates a list of ClientDetails.
[IC-x00042W Ms LQ Bethea 205, Willis Road Bolton BO5 1DQ 2000000007 2000100037 2006200319,
IC-x00033D Mr R Bowie 119, Thatcher Way Glasgow GL9 5SX 2006000016 2003100008 2005300001,
IC-x00013A MS GRV Blackwell 209, Drunk Road Hawick HK8 1MY 2006000009 2004100014 2003200304,
IC-x00018O Ms NAP Wallis 244, Grubb Lane Durham DU4 4ZX 2000000006 2003100012 2006200305]
One line is an object of the list. With the method above I try to return an object of the list found by ID (e.g. first token IC-x00042W). However when I run this in my main method it returns Only the first object.(IC-x00042W/Ms/LQ/Bethea/205, Willis Road/Bolton/BO5.....)
If I search by id of another object it returns null.
Source of ClientDetailsList:
The txt file has the following data:
IC-x00042W/Ms/LQ/Bethea/205, Willis Road/Bolton/BO5 1DQ/2000000007/2000100037/2006200319#
IC-x00033D/Mr/R/Bowie/119, Thatcher Way/Glasgow/GL9 5SX/2006000016/2003100008/2005300001#
IC-x00013A/MS/GRV/Blackwell/209, Drunk Road/Hawick/HK8 1MY/2006000009/2004100014/2003200304#
IC-x00018O/Ms/NAP/Wallis/244, Grubb Lane/Durham/DU4 4ZX/2000000006/2003100012/2006200305#
IC-x00037N/Miss/DOD/Burke/272, Ambrose Lane/Cambridge/CB2 2XD/2005000003/2001100020#
IC-x00039A/Dr/X/Salter/285, Bannister Road/Sea Palling/SP2 6GW/2000000002/2005100029/2005200306#
IC-x00011I/MR/R/Reece/104, Bannister Lane/Cromer/CR0 6LD/2005000012/2003100001/2001200300#
IC-x00025V/Mr/P/Abbott/163, Drunk Lane/Hunstanton/HU1 1UR/2003000029/2004100017#
IC-x00008L/Dr/P/Runyon/150, Tick Tock Way/Swindon/SW8 4OJ/2004000005/2006100005/2001200316#
IC-x00028F/MR/X/Watt/267, Malton Road/Cambridge/CB4 1PQ/2004100016/2004200312#
IC-x00031X/Mr/S/Lorenz/276, Tick Tock Way/London/LN9 7ID/2005000023/2005100007#
IC-x00020C/Mr/LNV/Mcmillan/44, Drunk Street/London/LN6 1RG/2001000019#
IC-x00015H/Mr/TQZ/Dubose/201, Drunk Road/London/LN4 5RA/2003000026/2006100028/2000200307#
//Creates ClientsDetailsList from source file
public static ClientsDetailsList readFile(File inputFile) throws IOException{
ArrayList <String> clientData = new ArrayList<String>();
ArrayList <ClientDetails> cdList = new ArrayList<>();
ArrayList <PolicyList> arrayofPolsLists = new ArrayList<>();
//Lists of ClientDetails fields
ArrayList <Name> clientName = new ArrayList<>();
ArrayList <String> clientID = new ArrayList<String>();
ArrayList <Address> clientAddress = new ArrayList<>();
// Lists of Name class fields
ArrayList <String> clientTitle = new ArrayList<String>();
ArrayList <String> clientInitials = new ArrayList<String>();
ArrayList <String> clientSurname = new ArrayList<String>();
//Lists of Address class fields
ArrayList <String> clientStreet = new ArrayList<String>();
ArrayList <String> clientCity = new ArrayList<String>();
ArrayList <String> clientPostCode = new ArrayList<String>();
ArrayList <ArrayList <Policy>> list = new ArrayList<ArrayList<Policy>>();
Scanner fileScan = new Scanner(inputFile);
fileScan.useDelimiter("#");
int i =0;
//Reading the file
while(fileScan.hasNext()){
clientData.add(fileScan.next());
Scanner cdScan = new Scanner(clientData.get(i));
cdScan.useDelimiter("/");
ArrayList <String> tokens = new ArrayList<String>();
ArrayList <Policy> clientPolicyNo = new ArrayList<>();
while(cdScan.hasNext()){
tokens.add(cdScan.next());
}
clientID.add(tokens.get(0));
clientTitle.add(tokens.get(1));
clientInitials.add(tokens.get(2));
clientSurname.add(tokens.get(3));
clientStreet.add(tokens.get(4));
clientCity.add(tokens.get(5));
clientPostCode.add(tokens.get(6));
boolean whileController = true;
while(whileController){
clientPolicyNo.add(new Policy(tokens.get(7)));
switch(tokens.size()){
case 9 : clientPolicyNo.add(new Policy(tokens.get(8)));
break;
case 10: clientPolicyNo.add(new Policy(tokens.get(8)));
clientPolicyNo.add(new Policy(tokens.get(9)));
break;
case 11: clientPolicyNo.add(new Policy(tokens.get(8)));
clientPolicyNo.add(new Policy(tokens.get(9)));
clientPolicyNo.add(new Policy(tokens.get(10)));
break;
}
whileController=false;
}
list.add(clientPolicyNo);
i++;
}
//Adding policy lists
for(int j =0; j<clientID.size();j++){
arrayofPolsLists.add(new PolicyList());
arrayofPolsLists.get(j).aListOfPolicies=list.get(j);
}
//Creating Name objects
for(int j =0;j<clientID.size();j++){
clientName.add(new Name());
clientName.get(j).Title = clientTitle.get(j);
clientName.get(j).Initials = clientInitials.get(j);
clientName.get(j).Surname = clientSurname.get(j);
}
//Creating Address objects
for(int j =0;j<clientID.size();j++){
clientAddress.add(new Address());
clientAddress.get(j).street = clientStreet.get(j);
clientAddress.get(j).city = clientCity.get(j);
clientAddress.get(j).postcode = clientPostCode.get(j);
}
//Creating ClientDetails
for(int j =0;j<clientID.size();j++){
cdList.add(new ClientDetails());
cdList.get(j).ClientID = clientID.get(j);
cdList.get(j).fullName = clientName.get(j);
cdList.get(j).fullAddress = clientAddress.get(j);
cdList.get(j).clientsPolicies = arrayofPolsLists.get(j);
}
//Creating a ClientDetailsList object
ClientsDetailsList ClientDetList = new ClientsDetailsList();
ClientDetList.aListOfClientDetails = cdList;
return ClientDetList;
}
ClientDetails class has 4 fields:
public String ClientID;
public Name fullName;
public Address fullAddress;
public PolicyList clientsPolicies;
Main method
File clientsFile = new File("ClientDetailsInput");
InputData e = new InputData();
ClientsDetailsList testList = new ClientsDetailsList();
testList = e.readFile(clientsFile);
System.out.println(testList.getClientDetails("IC-x00013A"));
Put a break after found = true;, for example...
public ClientDetails getClientDetails(String givenID) {
boolean found = false;
int index = 0;
for(;index<aListOfClientDetails.size();index++){
if((aListOfClientDetails.get(index).ClientID.equals(givenID))) {
found = true;
break;
}
}
if(found)
return aListOfClientDetails.get(index);
else return null;
}
Or you could simplify it further, by doing away with the need for the index at all, for example
public ClientDetails getClientDetails(String givenID) {
boolean found = false;
int index = 0;
ClientDetails details = null;
for (ClientDetails check : aListOfClientDetails) {
if(check.ClientID.equals(givenID)) {
details = check;
break;
}
}
return details;
}
Updated
After actually been able to read the data, I added
String check = aListOfClientDetails.get(index).ClientID;
System.out.println(givenID + " = " + check);
if ((check.equals(givenID))) {
to the search list and it printed...
IC-x00013A = IC-x00042W
IC-x00013A =
IC-x00033D
IC-x00013A =
IC-x00013A
IC-x00013A =
IC-x00018O
IC-x00013A =
IC-x00037N
IC-x00013A =
IC-x00039A
IC-x00013A =
IC-x00011I
IC-x00013A =
IC-x00025V
IC-x00013A =
IC-x00008L
IC-x00013A =
IC-x00028F
IC-x00013A =
IC-x00031X
IC-x00013A =
IC-x00020C
IC-x00013A =
IC-x00015H
...which freaked me out, until I realised that the ID's were prefixed with a new line character...!
So what I did was add trim to each result from the tokens in the read method...
clientID.add(tokens.get(0).trim());
(I did it for each line, just haven't shown), which then lead to
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 13, Size: 13
at java.util.ArrayList.rangeCheck(ArrayList.java:638)
at java.util.ArrayList.get(ArrayList.java:414)
at testsearch.TestSearch$ClientsDetailsList.getClientDetails(TestSearch.java:47)
at testsearch.TestSearch.main(TestSearch.java:23)
Which is what I expected should happen in your search method...
If we have a closer look at the search loop...
for(;index<aListOfClientDetails.size();index++){
if((aListOfClientDetails.get(index).ClientID.equals(givenID)))
found = true;
}
You should be able to see, that regardless of the state of found, the index will ALWAYS be equal to aListOfClientDetails.size() at the end of the loop, as there is no other exit condition to the loop that takes found into consideration...
Which takes me back to my original suggestions...
Never discount the power of a simple System.out.println statement to check your sanity and a good debugger...
Debugging would have helped i guess.
At the end you are returning
return aListOfClientDetails.get(i);
But you should get index "index" instead of "i" that was never initiated as far as i see.
So replace that with
return aListOfClientDetails.get(index);
And of course dont forget to leave the loop when you find something so index stays the right index.
Or just return the object right after you found it instead of setting found to true.
And last but not least: I dont know ClienID so I cant tell from here but if it does not implement the equals function the you actually will not get it work the way you want. So check what it does and perhaps override it.
Setting the Scanner's delimiter to '#' or '/' causes the line separators \n (whatever it is on your system) to remain in the data. So, some next() method call will eventually produce a String value with a leading \n, which is bound to happen for all IDs from the second one up, if the line structure is aligned with the '#' signs.
The code you have for parsing is extremely complex. I'd advocate to read lines (\n-delimited) and handle one line at a time, using
String[] tokens = line.split( "/" );
and assigning the strings to the destination fields of one object. Avoid the many lists - this only confuses matters.
I cannot retrieve the selection in the JComboBox that was selected by the user. the J ComboBox inclued a list of car registration numbers, which then the user has to select one and it will be added to the booking ArrayList. Unfortunately it is not working properly. and the booking is not being saved because of this. Please Help Me! Maybe I need to change the get Method for the combo box.
ArrayList BookingList = CarRentalSystem.BookingList;
ArrayList CarList = CarRentalSystem.CarList;
UsingFiles BookingFile = CarRentalSystem.BookingFile;
String [] regNums;
public NewBooking() {
regNums = new String[CarList.size()+1];
for (int i = 0; i< CarList.size();i++){
regNums[i] = ""+((Car)CarList.get(i)).getCarNum();
}
initComponents();
....
Booking book = new Booking();
String regNum = cmbCar.getActionCommand();
book.getRegNum();
Not easy to understand what u want can you add more code?
try this to convert your list into a String Array that u pass in as a argument to your combobox.
public String[] regNums() {
String tArray[] = null;
for (int i=0; i<carList.size(); i++){
//Convert into a String[] and put the arrayList values in it.
tArray = carList.toArray(new String[i]);
}
}