my dog learned polymorphism*
The moose likes Swing / AWT / SWT and the fly likes Add item to JList from another class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Add item to JList from another class" Watch "Add item to JList from another class" New topic
Author

Add item to JList from another class

Dan Quartz
Greenhorn

Joined: Mar 29, 2013
Posts: 3
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

Joined: Oct 27, 2005
Posts: 19656
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Dan Quartz
Greenhorn

Joined: Mar 29, 2013
Posts: 3
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Add item to JList from another class
 
Similar Threads
BorderLayout Problem
How to organize panels?
JPANEL
Example 2 Jlists in a JDialog
Why are my JFrames closing?