Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help with a project  RSS feed

 
stelios papamichael
Ranch Hand
Posts: 93
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey there! I recently got assigned a small project by a logistic office and i am having trouble figuring out some things.The app has to be able to add and remove clients from the list(i've figured that piece of code out) and also, be able to list the clients out based on their name or otherwise based on a number.
I am thinking of adding a JButton or 2 that when pressed each one lists the clients in alphabetical order or in numerical order(a good addition would be if i could type in the name to find the client).Also it must save those clients whenever a change is made.I haven't figured out those 2 problems yet since i am a begginer i'd say in Java.I will post my code below and also talk about one more issue i am facing.



If you run my project and add like 2 clients and delete the one with an index of 0, the 2nd one with an index of 1 will still have an index of one instead of changing to 0.I did count-- inside the remove button but that did not do the trick.I also tried some other ways but that didn't do it either.Any suggestions for solving out this issues would be great.(I searched on google for the saving part and found a site but i am not sure if it is correct). Thanks in advance!
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the 2nd one with an index of 1 will still have an index of one

What do you do to see that? 
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a suggestion: how about adding a ListDataListener to the model?  Then you don't have to ask for a counter (index) when removing an element.  It could also change the way you do other things in the app.
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of

You should just use this in order to have the instructions show while the user inputs the information:

And get rid of the unused static variables name, subname, and afm.

 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To speed up testing, hardcode the responses for all the dialogs.  Something like this:
 
Christopher Adams
Greenhorn
Posts: 17
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I'm not too familiar with DefaultListModel()
but to have the model update the displayed info wouldn't you have to do something like this,
after you decrement the counter:
 
Junilu Lacar
Sheriff
Posts: 11152
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you really need to use Swing for this? Honestly, from what you've shared so far, it looks to me that you're already in over your head in terms of managing the complexity of the code. Swing just makes the situation with complexity worse.
 
stelios papamichael
Ranch Hand
Posts: 93
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
the 2nd one with an index of 1 will still have an index of one

What do you do to see that? 


added 3 clients, each one is assigned an index 1st one 0, 2nd one 1 and 3rd one 2. After deleting the 1st one with an index of 0,the previously 2nd one with an index of 1 still has an index of 1 instead of 0 as it should since the first one got removed!
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After deleting the 1st one with an index of 0,the previously 2nd one with an index of 1 still has an index of 1 instead of 0 as it should since the first one got removed!

Are you talking about the contents of the String that is built on line 46 that has the value of current in it?  That String will not be changed when items are removed from the list or when the value of current is changed.
 
Junilu Lacar
Sheriff
Posts: 11152
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
stelios papamichael wrote:added 3 clients, each one is assigned an index 1st one 0, 2nd one 1 and 3rd one 2. After deleting the 1st one with an index of 0,the previously 2nd one with an index of 1 still has an index of 1 instead of 0 as it should since the first one got removed!

What are you basing this assertion on?  Are you perhaps printing out the element and seeing something like "2nd Client ... | Position: (1)"?
 
stelios papamichael
Ranch Hand
Posts: 93
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:I have a suggestion: how about adding a ListDataListener to the model?  Then you don't have to ask for a counter (index) when removing an element.  It could also change the way you do other things in the app.


I searched and found this website about LISTDATALISTENERS and i tried to make it work but i get some erros about the Log. {code} property and the (newLine) one and i cannot seem to make it work.Do you have any experience with with ListDataListeners?
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  i cannot seem to make it work

Please explain what you want it to do.   
And post the code you are having problems with.
 
stelios papamichael
Ranch Hand
Posts: 93
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:
stelios papamichael wrote:added 3 clients, each one is assigned an index 1st one 0, 2nd one 1 and 3rd one 2. After deleting the 1st one with an index of 0,the previously 2nd one with an index of 1 still has an index of 1 instead of 0 as it should since the first one got removed!

What are you basing this assertion on?  Are you perhaps printing out the element and seeing something like "2nd Client ... | Position: (1)"?


Yup! I print out the name, last name, afm number and position of the client! If we take into account the previous case i mentioned above, and i choose to remove the client in index 1(which in this case is the current client's position) after the 2nd one and the 3rd one are left, it throws an error.
 
stelios papamichael
Ranch Hand
Posts: 93
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
  i cannot seem to make it work

Please explain what you want it to do.   
And post the code you are having problems with.


I declared model = new new DefaultListModel(); , implemented LISTDATALISTENER,
wrote :
model.addListDataListener(new ListModel());

then imported everything that was needed and tried to run this method :

public void contentsChanged(ListDataEvent e) {
        log.append("contentsChanged: " + e.getIndex0() +
                   ", " + e.getIndex1() + newline);
    }

but it gave me an error at the log. part and at the ...+ newline part asking me to create a log class etc.
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I print out the name, last name, afm number and position 

The position must be determined when the element is retrieved from the model for showing in the list's window.  One way would be to override the DefaultListModel's method that returns the String for the element and have it append the position to the end of the String.
 
stelios papamichael
Ranch Hand
Posts: 93
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
I print out the name, last name, afm number and position 

The position must be determined when the element is retrieved from the model for showing in the list's window.


Doesn't that happen when we add an element to the list? it shows on the list automatically!
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to do some research and reading about what the DefaultListModel does and how the JList class uses it.
There are some details in the API doc for those two classes:  http://docs.oracle.com/javase/8/docs/api/index.html
and the tutorial: http://docs.oracle.com/javase/tutorial/uiswing/components/list.html
 
stelios papamichael
Ranch Hand
Posts: 93
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:You need to do some research and reading about what the DefaultListModel does and how the JList class uses it.
There are some details in the API doc for those two classes:  http://docs.oracle.com/javase/8/docs/api/index.html
and the tutorial: http://docs.oracle.com/javase/tutorial/uiswing/components/list.html


I read through the DefaultListModel and through the How to use lists links that you gave me and they did in fact teach me a few things and saw some cool properties i could use.However, i don't see how i can fix my first problem using any of the list's properties.Firstly like i said, i wanna just change the index of it's element when something is removed or added and display it to the list like position.I tried multiple things to be honest but ended up not solving it.I will definitely keep on searching but if you got any hints for m, feel free to share them !
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think how the JList class gets the elements from the ListModel.  The ListModel's API doc says:

public interface ListModel<E>
This interface defines the methods components like JList use to get the value of each cell in a list

If you override the ListModel's method that is defined in the DefaultListModel  class that returns the "value of each cell", you could append the position value to the end of the String.
In your method, call the super's method and append the String you want to see to the value that is returned.
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, here's my impression of what you could do: don't use a DefaultListModel.  It seems that a DLM will reorder the elements in its underlying Vector (like an Array).  You will never know what the new indices are.

What I think you need is a way to get which element is selected and I don't think a DLM will do that for you.  Look at the web page Norm Radder gave you.  I think a plain JList will do.  When the delete button is pressed, check if there is a selected element and warn the user if not.  If there is a selected element, get its index and delete it.
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the DLM will work, but requiring the user to enter an index number is wrong.  As Knute suggests, a better interface would be for the user to select the item to be deleted and then press the delete button.
I wouldn't enable the Delete button until an item has been selected.
 
stelios papamichael
Ranch Hand
Posts: 93
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:Think how the JList class gets the elements from the ListModel.  The ListModel's API doc says:

public interface ListModel<E>
This interface defines the methods components like JList use to get the value of each cell in a list

If you override the ListModel's method that is defined in the DefaultListModel  class that returns the "value of each cell", you could append the position value to the end of the String.
In your method, call the super's method and append the String you want to see to the value that is returned.


Sorry if i'm getting on your nerves :P but i am a begginer and i am having trouble understanding the overriding part.From what i can tell, the DefaultListModel comes with a ListModel interface which does what you stated above to get the value of each item on each cell in a JList for example.The thing is,what does override do and how can i access that method.Should i make a reference or a variable and then access it's properties?

PS. Really really sorry if my questions are dumb(and sorry for my bad English but i am not a native speaker).
 
stelios papamichael
Ranch Hand
Posts: 93
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Well, here's my impression of what you could do: don't use a DefaultListModel.  It seems that a DLM will reorder the elements in its underlying Vector (like an Array).  You will never know what the new indices are.

What I think you need is a way to get which element is selected and I don't think a DLM will do that for you.  Look at the web page Norm Radder gave you.  I think a plain JList will do.  When the delete button is pressed, check if there is a selected element and warn the user if not.  If there is a selected element, get its index and delete it.


I was thinking about it yesterday night when i read those 2 docs but i firstly wanna see if i can do it using a DLM otherwise i may do that!
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what does override do
That's an important part of OOP: allowing a class that extends another class to redefine what a method in the super class does.  You need to understand that. 
Besides explicitly extending a class, you can create an anonymous class by following the call to the class's constructor with {}s that wrap the methods you are overriding:

See the tutorial: http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
PS. Really really sorry if my questions are dumb(and sorry for my bad English but i am not a native speaker)

There's NoNeedToSaySorry (that's a link).  I believe you started this thread in Beginning Java and that's a place where you can ask "dumb" questions.  Everybody asks "dumb" questions in their beginning stages.  Even outside of Beginning Java, I think people know that we're all here to learn.
 
stelios papamichael
Ranch Hand
Posts: 93
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
what does override do
That's an important part of OOP: allowing a class that extends another class to redefine what a method in the super class does.  You need to understand that. 
Besides explicitly extending a class, you can create an anonymous class by following the call to the class's constructor with {}s that wrap the methods you are overriding:

See the tutorial: http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html


Awesome that topic seemed to be quite an eye opener for me as far as overriding goes! So, to make sure i understand correctly : the superClass here that we want to access is DefaultListModel right?And we can access it's methods and override them by creating an anonymous class.?Like this? :

 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
creating an anonymous class.?Like this? :

What happened when you compiled and executed that code?

Also while testing new code it is useful to print out the values of the variables used in the code:
1) to see if the code is being executed
2) to see the values the program gets
 
stelios papamichael
Ranch Hand
Posts: 93
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
creating an anonymous class.?Like this? :

What happened when you compiled and executed that code?

Also while testing new code it is useful to print out the values of the variables used in the code:
1) to see if the code is being executed
2) to see the values the program gets




This is the code i wrote to test it out and thankfully i get no errors.So to see if it worked i tried using System.out.println(fromSuper); but that did not work because it is a statement i guess.But how can i find if it works?I accessed the capacity method of the DefaultListModel and added 5 to it to make sure it worked but i cannot see the result.I placed the system.... statement inside the ListModel constructor, outside of it, inside the anonymous class and inside the main method but i always get errors like that statement must be removed or inside the main method, create local variable fromSuper,which seems fair ;p ! I also did this :

System.out.println(model.capacity()); but it always prints out 10 no matter if i add 5 or not !Am i doing something wrong here?(+ let me know if this is a valid anonymous class decleration format)
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That method does not print anything. If you want anything printed, add a print call to that method. If you print model.capacity(), you do not appear to be doing anything with that method. Try printing dlm.capacity().

And yes, that is a correct implementation of an anonymous class.
 
stelios papamichael
Ranch Hand
Posts: 93
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:That method does not print anything. If you want anything printed, add a print call to that method. If you print model.capacity(), you do not appear to be doing anything with that method. Try printing dlm.capacity().

And yes, that is a correct implementation of an anonymous class.


Alright,that definitely worked thanks(it was dumb mistake :P ).
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you see how to simplify the overridden method to just a return statement?
 
stelios papamichael
Ranch Hand
Posts: 93
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The thing is that right now i need to get the method that returns the index of an element in the defaultlistmodel from the JList and i am a bit lost,is there such a method?I used dlm.getIndex(counter); but it throws an error as i expected!I am still looking to find a way to do that with google ! Any advice?
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From what I can see, you need to put the DLM in a JList, then listen to the JList for selected items.
 
stelios papamichael
Ranch Hand
Posts: 93
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:From what I can see, you need to put the DLM in a JList, then listen to the JList for selected items.


After reading all the answers and my original thread, i realized that i can remove the selected item in the list(client in this case) and not use indexes and counters for that since it gets more complicated.Now that i got that out of the way, you mentioned adding a dlm in a JList.How does that happen, i researched a bit on google as always and came up with something that uses ActionListeners and ListSelectionListeners but the adding a dlm in a jlist got me confused.
 
Paul Clapham
Sheriff
Posts: 22503
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
stelios papamichael wrote:Now that i got that out of the way, you mentioned adding a dlm in a JList.How does that happen.


JList has a constructor which takes a ListModel, if I'm not mistaken. And I wouldn't be surprised to find it also has a setModel(ListModel) as well. Did somebody earlier in the thread already point you to the JList tutorial? If not, here it is: How to Use Lists.

Once you get used to using JList you'll probably find that you've been trying to implement many of its features yourself. Sounds like that's already happening. It's possible to learn about JList by working with somebody else's code but I've always found it more practical to start with code from the tutorial and mush it around until it's code which does what I want.
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like from this code that you are wrapping the DLM in a JList already.

So start by getting selected element from the list:
 
stelios papamichael
Ranch Hand
Posts: 93
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:It looks like from this code that you are wrapping the DLM in a JList already.

So start by getting selected element from the list:


I figured it out on my own before i read your answer but that's almost identical to what i did and now it works like a charm.Now my 2nd problem is saving the list and any change made to it when i the user presses the File -> Save file JMenu button.Like i mentioned at the beggining, i did find a website and as always i will try to solve it on my own but just in case any hints could help me !

[PS. Thanks a lot for helping me solve my first problem and for all the awesome resources you gave me]!
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now my 2nd problem is saving the list and any change made to it when i the user presses the File -> Save file JMenu button.

That depends a lot on how you are going to "save" the list.  Probably the two easiest are to save the contents of the List to a text file, or using an ObjectOutputStream (that's a link).
 
stelios papamichael
Ranch Hand
Posts: 93
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:
Now my 2nd problem is saving the list and any change made to it when i the user presses the File -> Save file JMenu button.

That depends a lot on how you are going to "save" the list.  Probably the two easiest are to save the contents of the List to a text file, or using an ObjectOutputStream (that's a link).


I came up to those 2 solutions too but i am not sure if they can deliver what i want.When i open my application from my desktop,i want it to open the list etc. but have everything i added there and save any new client that i have added or removed when i hit the save button.Will Saving the list's content in a .txt file do the job?
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will Saving the list's content in a .txt file do the job?

That will do half the job.  The other half will require the code to read the file and use its contents to populate the list.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!