• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt in JTable

 
tadi raja
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Override the toString() method of your object class to return the string value you want displayed.
 
tadi raja
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in which class i want to override toString() method, CompatibilityChartDialog class or in my own class ??? please provide me the code..
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look in the API for Object; it says that just about every class ought to override toString.
 
tadi raja
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how to override toString()method belongs to my code ,please help me its urgent....
 
Rob Spoor
Sheriff
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please Ease Up.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Maneesh Godbole:
You can find the code here
And more details here.
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic