• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

doubt in JTable

 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ...
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how to override toString()method belongs to my code ,please help me its urgent....
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please Ease Up.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Maneesh Godbole:
You can find the code here

And more details here.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic