• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java swing GUI with separate class for JList, having trouble adding items to the list?

 
Louis Lewis
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I am trying to create a GUI interface in swing which consists of four classes: a GUI class, which creates a main JPanel, a label, and a JList, which it takes from the second class, a MovesList class that contains a JList and the stuff needed to interface with it. It also has a main class, which basically just creates an object of the GUI class for the main window, and an Other class from which I would like to be able to add an item to the JList. I created methods in the MovesList class to get each component (like getMoveslist, or getMovesListScrollPane), which I then used to create the JList in the GUI class. I also created an addMove method so that I can add an item to the JList from any class through a MovesList object. However, this addMove method only works when called from the GUI or MovesList classes -- it does nothing when I collect from the Other class.

Any idea what I'm doing wrong/how to redesign this program? Here is my code:

//Main class


//GUI class


//MovesList class


//Other class


When I run this code, I get a window with a JLabel that says "Moves," and a JList that contains five elements -- test 1-test 5, but not a sixth "test from other class." ( using the add move method ) However, when I click on the window, the addMove method is also called, and successfully adds "test 6" to the list.

I am stumped. Anyone know what's going on/how to solve it?
 
Rob Spoor
Sheriff
Posts: 20820
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 6 of class Other creates a new MovesList, and you add to this object, not the existing one. If you want to have your Other class add to the existing MoveList, then you must pass it as a parameter to the constructor.
 
Louis Lewis
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That did the trick thanks!
 
Rob Spoor
Sheriff
Posts: 20820
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!