aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes input 2 values as 2 seperate variable but in one element in Jlist Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "input 2 values as 2 seperate variable but in one element in Jlist" Watch "input 2 values as 2 seperate variable but in one element in Jlist" New topic
Author

input 2 values as 2 seperate variable but in one element in Jlist

shaon husain
Ranch Hand

Joined: Mar 20, 2013
Posts: 37
theres 2 classes,

Example.java


CustomCellRender.java

ive managed to get the images shown, according to 'key',
ive added a new textarea 'number2' how would i get both key and number2 into same element but be able to extract both number seperatly again, ie
1st element: key= 5, number2=10
2nd element: key= 18, number2=4
3nd element: key= 33, number2=9
in JList: image,5,10
image,18,4
image,33,9
but when clicked on element 2, i should be able to print both numbers seperatly System.err.println(key), System.err.println(number2)
thanks
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2318
    
  49
but when clicked on element 2, i should be able to print both numbers seperatly System.err.println(key), System.err.println(number2)

I'm not sure what you mean by this. Do you mean when you select an element in the list you what to extract the separate numbers from the displayed text?
If so, you can create a class which holds the two values, add instances of that to the List and have your custom renderer get the values from the data object.
shaon husain
Ranch Hand

Joined: Mar 20, 2013
Posts: 37
Tony Docherty wrote:
but when clicked on element 2, i should be able to print both numbers seperatly System.err.println(key), System.err.println(number2)

I'm not sure what you mean by this. Do you mean when you select an element in the list you what to extract the separate numbers from the displayed text?
If so, you can create a class which holds the two values, add instances of that to the List and have your custom renderer get the values from the data object.


thats exactly what i wanted, quite happy someone understood!

do you mean something like this:


i have added this to the CustonCellRenderer.java class, but how could i implement this
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2203
    
    7
do you mean something like this:


Yes.

i have added this to the CustonCellRenderer.java class


You add that Object to the ListModel. Then in you renderer you cast the "value" to a QueryResult. Then you can access both properties and display them in whatever format your want.

Also, don't read the Icon in the Renderer. The renderer is called multiple times. Now that you have a custom class you can also add the Icon to the QueryResult class.
shaon husain
Ranch Hand

Joined: Mar 20, 2013
Posts: 37
Rob Camick wrote:

You add that Object to the ListModel. Then in you renderer you cast the "value" to a QueryResult. Then you can access both properties and display them in whatever format your want.

Also, don't read the Icon in the Renderer. The renderer is called multiple times. Now that you have a custom class you can also add the Icon to the QueryResult class.


im not sure what you mean do you mean not have this ImageIcon images = createImageIcon("/images/" + value.toString() + ".jpg");

im struck on a bit: so heres the full code again:

i need it to send 'number' to the render to get the image, but show the description on the list wihtout showing just 'number' or 'numb2' by itself.

heres the render class just in case you need to see it
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2203
    
    7
Make the following change to your code:



How many times do you see the output? This code is called every time a cell needs to be rendered. It is not very efficient to keep reading the same file over and over again. The image should be part of the data.
shaon husain
Ranch Hand

Joined: Mar 20, 2013
Posts: 37
Rob Camick wrote:Make the following change to your code:



How many times do you see the output? This code is called every time a cell needs to be rendered. It is not very efficient to keep reading the same file over and over again. The image should be part of the data.


how would i put the image in the data?
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2203
    
    7
Add an Icon property to your QeuryResults object.
shaon husain
Ranch Hand

Joined: Mar 20, 2013
Posts: 37
Rob Camick wrote:Add an Icon property to your QeuryResults object.


what class should i put the queryresult, the customCellRederer.java or Example.java?

if customCellRederer.java how would i then add it, i cant do
setText(new QueryResult(key, num2, "<html>first number: " + key
+ "<br>2nd number " + num2
+ "<br>both added together " + addInt,images));
as it says it needs a string
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2203
    
    7
Right now your class has 3 properties: number, number2, description.

You need to add a fourth property: icon.

Either add the icon in the constructor or create a setIcon() method.
shaon husain
Ranch Hand

Joined: Mar 20, 2013
Posts: 37
Rob Camick wrote:Right now your class has 3 properties: number, number2, description.

You need to add a fourth property: icon.

Either add the icon in the constructor or create a setIcon() method.



here shows no need for the cell renderer, it shows the decription but no image, but doesnt pull an error saying no image file. so it has found the image!

Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2203
    
    7
You need to change the renderer. Something like:

shaon husain
Ranch Hand

Joined: Mar 20, 2013
Posts: 37
Rob Camick wrote:You need to change the renderer. Something like:



im getting an error
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to components.Example$QueryResult

Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2203
    
    7
The QueryResult should be added to the ListModel not the String. The point of creating a custom renderer is that you get the data from the QueryResult object and then format the data the way you want. Since the default renderer is a JLabel you can just extract the String and Icon from the QueryResult object.

If this doesn't help then I can't help you anymore. Read the Swing tutorial on "How to Use LIsts". It explains what a renderer is.
shaon husain
Ranch Hand

Joined: Mar 20, 2013
Posts: 37
Rob Camick wrote:The QueryResult should be added to the ListModel not the String. The point of creating a custom renderer is that you get the data from the QueryResult object and then format the data the way you want. Since the default renderer is a JLabel you can just extract the String and Icon from the QueryResult object.

If this doesn't help then I can't help you anymore. Read the Swing tutorial on "How to Use LIsts". It explains what a renderer is.


Solved read post 10 from the wonderful Rob Camick
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: input 2 values as 2 seperate variable but in one element in Jlist