• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Add item to JList from another class

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have a problem that made me to spend 20hrs with no luck. Here is the problem. I have a program with 3 classes. Main, ClassA, ClassB. Main Class is just a simple main method to load JFrame in class A. In class A I got a JList and a JButton. So whenever I press this button, it loads Class B. In class B I got another button that its job is to add items to the JList in ClassA. Let's say when ever I press that button, it does Button.addElement("Item");

I made the layout but the problem is because they are in separate classes they cant link well. I guess I could not pass the instance. Anyway here is the code for each class:

Main Window:



ClassA:



ClassB:


So when I press the add button in classB it gives lots of errors and basically doesn't work. Any idea?

Thanks in advance.
 
Sheriff
Posts: 22849
132
Eclipse IDE Spring Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Call the other ClassB constructor.

Merge the two ClassB constructors into one. In the first you only set the fields you need to set. In the second you setup the UI. You need to do both.
 
Dan Quartz
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:Call the other ClassB constructor.

Merge the two ClassB constructors into one. In the first you only set the fields you need to set. In the second you setup the UI. You need to do both.



Wow thank you so much. Worked perfectly.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic