Win a copy of Event Streams in Action this week in the Java in General forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Adding to a JList, Interface Issue.

 
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?
 
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!