This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Add item to JList from another class

 
Dan Quartz
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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.
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic