Win a copy of Secure Financial Transactions with Ansible, Terraform, and OpenSCAP this week in the Cloud/Virtualization forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Trouble accessing a class from another class

 
Ranch Hand
Posts: 63
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So obviously I'm a noob. I have an assignment due in 2 days and I can't figure everything out on my own. Hoping these forums will help a bit.
I am struggling with the entire assignment but I don't know if I am allowed to ask about several things in one topic, so I am just gonna focus on the hardest one for me at the moment.
I'm trying to have the MouseListener of my program as a separate class and I'm getting something wrong with the access because it's not recognising the name of the class when I type it in the MouseListener class (they are in the same package).

This is the original class (meant to create a simple frame that allows users to enter some numbers into a list and then display some selections and occurrences of numbers):


and this is the MouseListener:



Error I'm getting when trying to run it:
C:\...\MouseClickListener.java:18:33
java: cannot find symbol
 symbol:   variable proglist
 location: class MouseClickListener

If I comment out the MouseClickListener class... and all the new methods I added to basically make the program functional, then the frame shows fine when I run, but none of the buttons work. This is what it looks like:


I know there's a lot of mess in the code and lots of things aren't fully working yet. I would appreciate any feedback and help, but I'm primarily stomped on the access thing.


Again... I am embarassed to post the state of this code. I know the methods are probably all wrong but I can't even test them because the frame stops showing when I have them or the MouseClickListener not commented out. I can't figure out why. I was guessing it might be related to the access.
 
Master Rancher
Posts: 4256
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check your spelling.  When you can't see what the problem is, use a case-sensitive search to check on the other places a token is used.  That will show where there are differences that your eye might miss.
 
Suane Mane
Ranch Hand
Posts: 63
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I change it to progList it then gets recognised but the other variables don't get recognised.
 
Marshal
Posts: 72478
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, what is ProgList (I have answered that for myself)? Why does your listener class extend it, and have a reference to it as a field? I would regard those two features as mutually incompatible. I suspect you probably don't want either.
You have gone to lots of trouble to ensure values for min and max, but what happens if somebody enters min=6 and max=3?
 
Campbell Ritchie
Marshal
Posts: 72478
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wouldn't use a mouse listener in the first place. You want action listeners each associated with the buttons, so you want three versions of action listener.That shows the use of a λ expression, which is almost certainly the most concise way to create individual listeners.

Moving you to our GUIs forum.
 
Suane Mane
Ranch Hand
Posts: 63
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So you would add the action listener in the original class (ProgList)? I was trying to have it as a separate class for the purpose of not having all the code in one class - lecturer says it's bad form and will remove marks for it.
 
Saloon Keeper
Posts: 23440
159
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Swing is one of those funny cases where you'd define an anonymous inner class implementation of Listeners and override only the methods of interest. That usually ran  to 5 lines of code, give or take and wasn't worth breaking out into an independent class definition file. Plus, being anonymous you couldn't - external classes have to have names.

But since around Java 8, the language has supported Lambdas, which were intended to do the same thing in a more elegant way. I don't have any recent Swing code, but perhaps someone here could give an example?
 
Suane Mane
Ranch Hand
Posts: 63
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Also, what is ProgList (I have answered that for myself)? Why does your listener class extend it, and have a reference to it as a field? I would regard those two features as mutually incompatible. I suspect you probably don't want either.
You have gone to lots of trouble to ensure values for min and max, but what happens if somebody enters min=6 and max=3?



The answer to the first part - ProgList is the class that holds the frame and main code. The rest of your question - I don't know. My listener class extended it because in my research I came across some examples that did it and from the way it was explained (I might have gotten it wrong) it was necessary to reference another class. The reference to it I don't even remember when I added and how.
The values for min and max - thank you. I'll have to wreck my brain to find a way to make sure that doesn't happen either!
 
Suane Mane
Ranch Hand
Posts: 63
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:Swing is one of those funny cases where you'd define an anonymous inner class implementation of Listeners and override only the methods of interest. That usually ran  to 5 lines of code, give or take and wasn't worth breaking out into an independent class definition file. Plus, being anonymous you couldn't - external classes have to have names.

But since around Java 8, the language has supported Lambdas, which were intended to do the same thing in a more elegant way. I don't have any recent Swing code, but perhaps someone here could give an example?


I didn't understand a word you just said
 
Campbell Ritchie
Marshal
Posts: 72478
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What have you been taught about creating listeners? You haven't missed the lecture where they were described, I hope. You add a listener to a component. You will find more information in the Java™ Tutorials, but I am by no means convinced they show you the best way to write listeners throughout. You can also find out about anonymous classes and λs in a different Java™ Tutorials section. I have no qualms about that section.
 
Suane Mane
Ranch Hand
Posts: 63
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any idea why the frame isn't showing at all while the methods code is there? When I run the program nothing pops up but it's showing as running.
 
Saloon Keeper
Posts: 4358
163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Suane,

that is because you initialize some fields as follows:

So, your program IS running, it is just waiting for some input!

But, you are right when stating your code is a little messy  
 
Saloon Keeper
Posts: 8020
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's 4 ways to write the same listeners. Obviously the 4th way using lambdas is the most elegant.







 
Suane Mane
Ranch Hand
Posts: 63
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:hi Suane,

that is because you initialize some fields as follows:

So, your program IS running, it is just waiting for some input!

But, you are right when stating your code is a little messy  



Where am I supposed to place the user input then so that it doesn't initialize until needed?
 
Piet Souris
Saloon Keeper
Posts: 4358
163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

a way is to put this part in main(), and give these variables as arguments to your constructor. What I would certainly do is to put some text before usng scanner,nextInt, for instance:

Then the user sees that he/she is supposed to input something.
 
Suane Mane
Ranch Hand
Posts: 63
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:
a way is to put this part in main(), and give these variables as arguments to your constructor. What I would certainly do is to put some text before usng scanner,nextInt, for instance:

Then the user sees that he/she is supposed to input something.



I looked at this link for help with constructors and based on that and your advice I changed code to:


and the main:


But the variables are not recognised in the methods anymore after this change. What am I doing wrong?
 
Suane Mane
Ranch Hand
Posts: 63
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:






Thank you very much! I simplified my code to


Too bad I can't test it until I fix the window showing up problem.
 
Piet Souris
Saloon Keeper
Posts: 4358
163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The way you do it now makes the three variables local to the constructor. If you need these fields for other methods as well, you should make them instance fields. Like:
 
Suane Mane
Ranch Hand
Posts: 63
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you! Now however it only pops up the window after I've entered 3 numbers in the console for some reason.
 
Piet Souris
Saloon Keeper
Posts: 4358
163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I know the cause of that, but to be sure: can you show us your current code?
 
Suane Mane
Ranch Hand
Posts: 63
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the updated full code (fixed the methods a bit as well):
 
Suane Mane
Ranch Hand
Posts: 63
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I figured it out. I swapped the line

to be at the top of main() so that the window shows up first before any request for numbers.
I've further fixed and tested the methods. Add and search work but the range is not working as intended.


Output:
Number added
[2]
Number added
[2, 4]
Number added
[2, 4, 5]
Number added
[2, 4, 5, 2]
Number added
[2, 4, 5, 2, 8]
Number added
[2, 4, 5, 2, 8, 4]


Output:
There is/are 2 occurrence(s) of the number 2
There is/are 1 occurrence(s) of the number 5



Output:
Here is the list of number between 2 and 5:
[2, 4, 5, 2, 8, 4]
[2, 4, 5, 2, 8, 4]
[2, 4, 5, 2, 8, 4]
[2, 4, 5, 2, 8, 4]
[2, 4, 5, 2, 8, 4]
 
Piet Souris
Saloon Keeper
Posts: 4358
163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But why do you need the three variables  (number, max and min)? The variable 'number' should be read from the first textfield, and the min and max from the second and third textfields.

You can read a textfield by issuing the method 'textfield.getTex()t', that will be a String that you must translate to an int. So, if button1 is clicked, read the value from textfield 1, convert it to an int and add it to the list. If the field is empty, then it is up to you what to do: add a 0, or show  an error in some way. The same holds for the min and max. If the button 'display range' is clicked, you should check both textfields for sensible content. Et cetera.

That makes your action listeners a tad more complex, though. For instance:
 
Piet Souris
Saloon Keeper
Posts: 4358
163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is obvious that our replies crossed each other!

Looks good what you have. But as you see, you do not need the initial three numbers.
 
Suane Mane
Ranch Hand
Posts: 63
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I realised that. Took me so long. I don't really know what I was thinking when I decided to use the 3 variables. It just slipped my mind that I had to read the values from the textfields. So I guess I need none of the variables or arguments now.
 
Suane Mane
Ranch Hand
Posts: 63
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Trying to tackle some error handling if a user enters a double instead of int. I used a try/catch but it still stopped the program when the exception occurred. Am I placing it in the wrong place?

 
Marshal
Posts: 26493
81
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

Suane Mane wrote:Trying to tackle some error handling if a user enters a double instead of int. I used a try/catch but it still stopped the program when the exception occurred. Am I placing it in the wrong place?



Well, yes. The code which might throw the exception should be inside the try-block.

However your exception-handling code which would be executed isn't especially helpful. It writes to the console, which is typically not visible when you're running a Swing program. And then it creates a JLabel and does nothing with it, so it wouldn't be visible either. Since your homework is due soon I would advise displaying a JOptionPane with the error message. (It would be better to just not allow the user to key incorrect data into the text fields, but that's a more advanced topic which you don't have time for now.)
 
Suane Mane
Ranch Hand
Posts: 63
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm only testing it so far on the console because I will move on to displaying the messages on the frame later. I wanted to make sure all the methods work as intended before adding them to labels or looking into how to best add them to the panels.
Also I thought the "else..." part of the code covered the part that would have to be in the try block because that covers all the numbers that can be entered from 0 to 9, which means it can hold 2.5, 4.6, 7.3 etc.
 
Carey Brown
Saloon Keeper
Posts: 8020
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

A couple of general comments. You need more descriptive variable names, like "addButton" instead of "button1". And "addText" rather than "text1". Your variable "Top"  should begin with a lower case letter (Java convention) but I would suggest "topPanel". These things aren't critical in a small program like yours but do become critical as programs increase in size. This is defensive programming because, most likely it will be you maintaining the code  down the road.
 
Paul Clapham
Marshal
Posts: 26493
81
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

Suane Mane wrote:Also I thought the "else..." part of the code covered the part that would have to be in the try block because that covers all the numbers that can be entered from 0 to 9, which means it can hold 2.5, 4.6, 7.3 etc.



No. A try-block is specifically for catching exceptions. It's the Integer.parseInt() method that will throw an exception, so it's the code that needs to be in the try-block. The rest of the code checking for < 0 and > 9 doesn't need to be in the try-block and the code might be clearer if it wasn't.
 
Suane Mane
Ranch Hand
Posts: 63
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Suane Mane wrote:Also I thought the "else..." part of the code covered the part that would have to be in the try block because that covers all the numbers that can be entered from 0 to 9, which means it can hold 2.5, 4.6, 7.3 etc.



No. A try-block is specifically for catching exceptions. It's the Integer.parseInt() method that will throw an exception, so it's the code that needs to be in the try-block. The rest of the code checking for < 0 and > 9 doesn't need to be in the try-block and the code might be clearer if it wasn't.



If I surround only that part with the try block then the rest of the method becomes unusable:
 
Piet Souris
Saloon Keeper
Posts: 4358
163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With

you make 'value' a local variable in the try-block, and is therefore out of scope in the rest of the code. A simple remedy is:
int value = 0;
try { value = ....};
and value stays in scope.

The last line (JLabel rules = ...) does not work, unless you add that label to the panel and issue a revalidate. That will probably be too complicaed at this stage, so can you add a long JLabel to your panel in the constructor, and use that label to display these messages?
 
Master Rancher
Posts: 3831
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, if a NumberFormatException is thrown, that happens before the parsed value is assigned to the variable valueEntered.  So, what's the point of printing value in the catch block?  That variable can't possibly tell you anything useful - if you're in catch block, the number was not parsed.  If you use Piet's code, you can print the old value of valueEntered, which is 0 in Piet's code.  (Well, he called it "value" there...)  But again, how does that help you?  Just don't bother trying to print valueEntered in this case; it can't help you.  And then, you don't have to change its scope just to print it.

Now, that advice seems contrary to what I would normally want in a catch block, which is to give an error message that contains any useful information you can add.  But there is a variable whose value you can print, that will actually help here.  Namely, the value of the thing that was supposed to be a number, but wasn't: text1.getText().

So:

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you extend Proglist then it's the same class so for purpose it's "this' at most difficult it would need disambiguation by using  "super" no requirement for a variable declaration.
 
Suane Mane
Ranch Hand
Posts: 63
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all. I did manage to figure most of it out in time for submission!
 
I am displeased. You are no longer allowed to read this tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic