• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Assigning Dropdown Menu Items to Classes  RSS feed

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello! I'm new here and doing my best to learn Java. Below is a project that I've been working on for some time as I learn.

Today I am learning to tackle GUI. I have played with IntelliJ, and I have come up with a design that I like. But I am not certain how to have that design preview. Every time I attempt to, it causes an error with "Class not found: javax.swing.JPanel". That's not my primary concern right now, but it would be nice to be able to use my design once I understand how this GUI functions.

My main concern, in a nutshell, is I am attempting to construct a dropdown menu with a list of items. Each individual item will have its own public class program. The program is about 100 lines long, and it must be custom tailored to each item on the dropdown list. So, I need the dropdown menu to select a specific class. I would then like to have a run button execute the program. I would also like to have a console pop up or something, since the program does take some time to run, and the console will show exactly what it is doing as it is looping so that we can see when it is done running or if it's frozen.

I will post my code in the comments. It is about 300 lines long, but 200 of that is two copies of my code. I don't know that this is the right way to implement the code...?

TIA!!
 
Master Rancher
Posts: 3270
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post the code (wrapped in code tags) so we can see what you are talking about?
 
Eric J Brown
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:Can you post the code (wrapped in code tags) so we can see what you are talking about?



Of course! I meant to but had to wait since OP as it would not let me double post. I did not want to clutter the OP. Here you go! This is my best attempt at it, you can probably see what my critical code is vs the GUI. This is a snippet, not sure it will work if you copy and paste as I removed the second option due to length issues with the post. I can select the option, but I have no way of "executing" the code once I select from the dropdown, and thus nothing happens. It doesn't help that I do not have a console popup to see what's going on if it does start working.

 
Norm Radder
Master Rancher
Posts: 3270
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you post the import statements needed to compile the posted code so it can be compiled and executed for testing?

I have no way of "executing" the code once I select from the dropdown, and thus nothing happens.


Look at the method that is called when an item is selected:
That method needs contents that does what you want.
 
Sheriff
Posts: 23646
49
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Eric J Brown wrote:I am attempting to construct a dropdown menu with a list of items. Each individual item will have its own public class program.



So for each entry in the dropdown list, you would like to associate with that entry an instance of a class which implements Scraper. Right?

The simplest way to do that is for the entry in the dropdown list to actually be that instance. You've fallen into the beginner trap (helped along by beginner tutorials perhaps) of assuming that only String objects can be used in a dropdown list. But in fact any object can be used. So for example the first element in the dropdown list would be an object of type Scraper1. But you want it to appear to the user as "First DD"; to do that you override the toString() method of Scraper1 to return "First DD".

Doing it that way simplifies your ItemChangeListener class because all you need to do is to get the selected item; it's already a Scraper object so you just have to run its runScraper() method.
 
Eric J Brown
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:Could you post the import statements needed to compile the posted code so it can be compiled and executed for testing?

I have no way of "executing" the code once I select from the dropdown, and thus nothing happens.


Look at the method that is called when an item is selected:
That method needs contents that does what you want.



 
Eric J Brown
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Eric J Brown wrote:I am attempting to construct a dropdown menu with a list of items. Each individual item will have its own public class program.



So for each entry in the dropdown list, you would like to associate with that entry an instance of a class which implements Scraper. Right?

The simplest way to do that is for the entry in the dropdown list to actually be that instance. You've fallen into the beginner trap (helped along by beginner tutorials perhaps) of assuming that only String objects can be used in a dropdown list. But in fact any object can be used. So for example the first element in the dropdown list would be an object of type Scraper1. But you want it to appear to the user as "First DD"; to do that you override the toString() method of Scraper1 to return "First DD".

Doing it that way simplifies your ItemChangeListener class because all you need to do is to get the selected item; it's already a Scraper object so you just have to run its runScraper() method.



That code is runable but difficult to test since you guys don't have the same setup. It uses a link to a site located in column B of a spreadsheet and goes through that page to look for specific data.

I think you may understand what I'm asking but not entirely sure. In the sample code I provide, both instances of the program are the same. But if you look closely, JSOUP pulls very specific elements from a specific website. So the dropdown will contain different site names and the program will change the elements its searching for based on site name (so instead of a.product-title, the program may need to search for div.product-name). Each site needs custom tailored code. If you compare my first and second example, product name isn't the only thing I wish to pull, but due to post character limits, I couldn't post the full code with two copies, so I snipped those extra items (SKUs and prices) out. Perhaps there's a more efficient way to do this? I'm really new and have pieced together this code from different resources and have custom tailored it to my needs so far.

After testing the most recent code I mentioned above, it's not running, even when I select another item in the dropdown. It used to print a line in console saying something, and it worked when that was its only function, but when I swapped to the actual code, it doesn't run the actual program within that needs to run.
 
Norm Radder
Master Rancher
Posts: 3270
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

it's not running

How do you know? 
There needs to be code in the empty methods at lines 124 and 166.  At least add a print statement to show that the method was executed.

Note: The main method in the Scraper classes will not be called.  You need to explicitly call them if you want the code in the main methods to executed.

 
Eric J Brown
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:

it's not running

How do you know? 
There needs to be code in the empty methods at lines 124 and 166.  At least add a print statement to show that the method was executed.



I was thinking the same, how could I know, so I put a link in the spreadsheet to test, clicked on the dropdown and gave it a few minutes. Came back and it did not add the data. So I'm positive it didn't run. I can add a print statement but worried it's not going to change that the main program isn't running.

Norm Radder wrote:Note: The main method in the Scraper classes will not be called.  You need to explicitly call them if you want the code in the main methods to executed.



Why is that and what could I do to resolve it? It seems that that would be the problem.
 
Eric J Brown
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's what I'm going for to further elaborate...

 
Norm Radder
Master Rancher
Posts: 3270
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

what could I do to resolve it?


Move the logic code out of the main method into another method in the Scraper classes.  Then have the runScper method call that method to do the desired work.
If you still want the Scraper classes to work standalone, Change the main method to have it create an instance of the class and call the newly created method where the work will be done.

Why is that


A method is not executed unless it is explicitly called.  An exception is when the JVM calls the main method when the class is started with the java command.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!