• Post Reply Bookmark Topic Watch Topic
  • New Topic

Adding to a JList, Interface Issue.

 
Richard Murray
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a JList of elements that are in a specific order. I already have a method that let's me remove things from any position within the JList while maintaining the integrity of the JList. The problem is that I also want to be able to add things at any given position in the JList. For example, let's say I have the following JList like so . . .

AAAA
BBBB
DDDD

Let's say I have a String element "CCCC" and I want to insert it into position 3 within the JList. I want to have an interface that will looking something like this so I can right click on the position where I want to insert the element. Example, where ---- stands for some kind of seperator that I can click on to select where I want the element to go . .

----
AAAA
----
BBBB
---- <---- I want to insert the element in here.
DDDD
----

I also want to insert the same seperator around the newly inserted element like so . . .

----
AAAA
----
BBBB
----
CCCC
----
DDDD
----

Can anyone help/suggest anything?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the the add(int index,Object element) method of the list's listModel
 
Richard Murray
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I know that. However, the issue I have is with designing an interface to do perform that action.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't see what the problem is
click on a ----
get the selectedIndex
add another ---- at the selectedIndex
add CCCC at selectedIndex + 1

and it would end up per your description
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!