• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Issues with JTextFields and JButtons

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am working on the final part of my Inventory Program.

I am working on the addButton and searchButton pieces.

For the addButton, I have it working with a JOptionPane asking for additional items. However, the assignment calls for them to be added through the JTextField instead. Also, the button needs to change the text to Click to Add and then be able to be clicked to Add the item. I can get the text to change on the button, but unable to get the button to add the item to the array. I have a an addItem() method and an addItem2() method written up in InventoryLauncher, where the addItem2() is the one where I was trying to use the JTextField to enter the text.

For the searchButton, I have it working to find the item in the array, but I am unable to get the button to work to search for an item. It is only working when I hit the enter key.

Can someone point me in the right direction for using the button a second time to add and to search?

Thanks!


Here is the code

Inventory.java


Books.java
 
Amie Mac
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does my question make sense?
 
Amie Mac
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am surprised I haven't seen a reply on my post. I am wondering if there is something wrong with my original post?
 
Marshal
Posts: 28425
102
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, yes, there is something wrong with your question. Sorry, but it's far too long to be read and understood due to the large volume of code which is not relevant to the problem. I looked at it twice and gave up on the idea of trying to answer because of that.

So have a look at the SSCCE (Short, Self Contained, Correct (Compilable), Example) page which describes how you can produce a much better code example for your question.
 
Amie Mac
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:Well, yes, there is something wrong with your question. Sorry, but it's far too long to be read and understood due to the large volume of code which is not relevant to the problem. I looked at it twice and gave up on the idea of trying to answer because of that.

So have a look at the SSCCE (Short, Self Contained, Correct (Compilable), Example) page which describes how you can produce a much better code example for your question.



It would be nice to know if I would need to modify my question to get some help instead of just not answering at all.
 
Amie Mac
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I apologize for the short answer... I've been frustrated with this program all week.

I have put a couple of methods in my InventoryLauncher file to show that I've tried to code something that works according to the requirements. I felt that I explained that in the OP.
 
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Amie

On the add button click you need to open a new window probably a JFrame (not JOptionPane) that will have all the input fields with a save button.
 
Marshal
Posts: 80874
506
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Disagree: don't have more that one frame. You can add buttons to a dialogue window.
 
Swastik Dey
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any issues with showing more than one frame? It would be nice if you could please clarify it.
 
Campbell Ritchie
Marshal
Posts: 80874
506
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is not what frames are intended for. It is what dialogue windows are intended for.
 
Amie Mac
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My assignment is already handed in and I ended up using JOptionPanes. But for my own learning I would like to know how to use the JTextFields for the input and modifications for the top row of JButtons that I have in the program (Add, Modify, Delete)

For example, the requirements for the Add button and method are:
1. When clicking on the Add button, the button text should change to Click to Add! so the user should click it to add the item to an array
2. When the Add button is clicked it would make the appropriate JTextFields editable so that text can be entered (Item Name, Item Number, Author, Qty in Stock, and Price)
Note: I am able to make the button text change with a setText on the button, and I am able to get the fields to become editable, but I am not able to take what is typed in the fields and enter them into an Array with the Enter key or with the "Go" button.
 
Campbell Ritchie
Marshal
Posts: 80874
506
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Amie Mac wrote: . . .
1. When clicking on the Add button, the button text should change to Click to Add! so the user should click it to add the item to an array

Why an array?
You realise the change in button text may cause the whole display to change strangely

2. When the Add button is clicked it would make the appropriate JTextFields editable . . .

You can create listeners which call certain methodsBut if you are setting them editable when you push the button, how is the user supposed to enter anything beforehand? And how are you setting them uneditable?

Go through the JTextComponent class and its subclasses. You will probably find you can add action listeners to them, and they can be activated by pushing the enter key while the component has focus.
For things more complicated you can consider key bindings and document listeners. Also find somebody who knows more about them than I do!

Note I am not creating a Listener there but using a λ. It only works in Java8.
 
Amie Mac
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Amie Mac wrote: . . .
1. When clicking on the Add button, the button text should change to Click to Add! so the user should click it to add the item to an array

Why an array?
You realise the change in button text may cause the whole display to change strangely

2. When the Add button is clicked it would make the appropriate JTextFields editable . . .

You can create listeners which call certain methodsBut if you are setting them editable when you push the button, how is the user supposed to enter anything beforehand? And how are you setting them uneditable?

Go through the JTextComponent class and its subclasses. You will probably find you can add action listeners to them, and they can be activated by pushing the enter key while the component has focus.
For things more complicated you can consider key bindings and document listeners. Also find somebody who knows more about them than I do!

Note I am not creating a Listener there but using a λ. It only works in Java8.



That's why I am so confused. I asked my instructor for help... he stated that each time the button is clicked, the code has to handle the event of hitting the button with the actionListener.
These are the examples he sent to me for the Add button:

Instructors Note: I assume that you can display the data. So when you click on Add, use the actionlistener to perfrom the task of adding. Here is a sample code:


Instructors Note: Each time when you have a button, you need to handle the event of hitting the button with the actionlisterner. So in the actionlistener when the source of the button is add, then you call the add function.


I was able to get the Search method to partially work through the JTextField, but just by pressing Enter and not by clicking on the button a second time, but not the Add method...
Taking an online class through UoP for Java Programming was not the greatest way to learn Java Programming.>
 
Campbell Ritchie
Marshal
Posts: 80874
506
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
DVDs? That sounds totally different from text components. Does that mean that the details of the DVD, e.g. title, actors, year, are in the different text fields?

As I said earlier, why an array? A List<DVD> would have been far better; you can simply call its add method.If you have to use an array, use the arraycopy or copyOf methods rather than a loop. The version of copyOf(...) in that link allows you to create an array 1 larger; then you can assign the last element to the new DVD.
I also do not like the second code snippet. It suggests you are using addActionListener(this); which I do not like at all. I think you should have an action listener object for each different action. There are alternatives; you can use an Action instead (like action listener but better) or if you use Java8 you can replace the objects with λs.

Yes, whenever you push a button, its action listener has to handle the action. Only Actions and λs might be better.
 
Amie Mac
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, the details of the DVDs are in the different text fields.
DVDs is what the instructor used in his inventory program, whereas I used books.

We used an array because that was the progression of the assignments. Since it's a beginning Java course. I also think the List would have been better, but didn't take the time to change the code to it from the Array. I am sure there are way better ways (as you stated) to do this entire program. I may look into re-doing it with the better code that you mentioned.

By the way, what is λs? I don't recognize that symbol.
 
Amie Mac
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just googled it... found out it's Lamda
 
Campbell Ritchie
Marshal
Posts: 80874
506
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Amie Mac wrote:. . .
By the way, what is λs? . . .

It's all Greek to me

Did you look for Church's λ calculus? Or this section in the Java™ Tutorials?
 
Amie Mac
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found the Lamda section in the Java tutorials
https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html
reply
    Bookmark Topic Watch Topic
  • New Topic