aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes doubt in JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "doubt in JTable" Watch "doubt in JTable" New topic
Author

doubt in JTable

tadi raja
Ranch Hand

Joined: Jul 07, 2008
Posts: 109
Hi

here is the code


the above code is for creating SolutionCompatibilityMatrix of type DefaultTableModel .

Here drugs is an ArrayList object it can pass in String[] and dmodel is an object of SolutionCompatibilityMatrix.

ArrayList drugs = new ArrayList();

drugs.add("Drug1");
drugs.add("Drug2");
drugs.add("Drug3");
drugs.add("Drug4");

String[] rowData=null;

rowData = new String[drugs.size()+1];

rowData[0] = row.getDescription().toString();

dmodel.addMatrixToTable(rowData);

Here my quesion is i am passing String[](rowData) to the method (addMatrixToTable) of SolutionCompatibilityMatrix,it can displaying some object code like abc@54432 but not the actual string value.how to resolve this problem ??? its an urgent!!!

thank you in advance ...
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Override the toString() method of your object class to return the string value you want displayed.


GenRocket - Experts at Building Test Data
tadi raja
Ranch Hand

Joined: Jul 07, 2008
Posts: 109
in which class i want to override toString() method, CompatibilityChartDialog class or in my own class ??? please provide me the code..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38349
    
  23
Look in the API for Object; it says that just about every class ought to override toString.
tadi raja
Ranch Hand

Joined: Jul 07, 2008
Posts: 109
if i am using in the code dmodel.addMatrixToTable(rowData.toString());

it gives error message ..how to resolve this problem in the program
tadi raja
Ranch Hand

Joined: Jul 07, 2008
Posts: 109
how to override toString()method belongs to my code ,please help me its urgent....
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Please Ease Up.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38349
    
  23
What parameters does the addMatrixToTable method take? You have to pass that type of argument. If it is a String, which appears unlikely, you would have to pass the String in exactly the correct format.
tadi raja
Ranch Hand

Joined: Jul 07, 2008
Posts: 109
hi i am passing String[] parameters to addMatrixToTable() mehtod but it returns in object format not in exact string format, uhave already told to override toString() of Object class.can you give me the code how to override toString() method belongs to my program.

thank you in advance.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10248
    
    8

Originally posted by tadi raja:
can you give me the code how to override toString() method belongs to my program.


You can find the code here


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38349
    
  23
Originally posted by Maneesh Godbole:
You can find the code here
And more details here.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

On a side but related note for you moving forward, a lot of swing components will require the toString() method to be overriden in order for it to display correctly in the widget. This is a real PITA if you are using a domain model that wasn't really written specifically for Swing and your existing toString() methods are already written but not friendly enough for Swing components.

In these cases you can do what I have had to do and write a model wrapper around your object to be used. So for example, Say I have a Person class and its toString() method returns a bunch of junk I don't want displayed. So I'll write class like this...



And then I'll use that object, instead of Person, to place in a JList, for example.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

I'd rename it because it's not a ListModel. ListModelPerson would be better.

Most of the time I write a subclass of DefaultListCellRenderer though:

For tables it would be a subclass of DefaultTableCellRenderer of course, but it would work the same. I then combine it with my model and table:

[ October 06, 2008: Message edited by: Rob Prime ]
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

DefaultListCellRenderer seems like overkill unless you need to present the data different that just a simple String, doesn't it? For example, adding an image or changing the foreground/background color of a cell? Of course, in those cases, I would use DefaultListCellRenderer but othewrise a simple wrapper class is, well, simpler.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubt in JTable