Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8235167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:42:50+00:00 2026-06-07T18:42:50+00:00

In a groovy swing application, I have a class that represents teachers like the

  • 0

In a groovy swing application, I have a class that represents teachers like the following:

Docente.groovy

public class Docente {
    String codigo
    String nombre
    String apellidoPaterno
    String apellidoMaterno
    String direccion
    String tipoDocumento    
    String sexo
    String telefono
    String correo

    String toString() {
        nombre
    }
}

I use the toString method to display the teachers name (with nombre) in a JTable, along with certain other values. The idea es to show some of them on the table and the rest of them on a JDialog window in order to preform son CRUD operations.

Assuming that sw is an instance of groovy’s SwingBuilder object, and grdDocentes is the id of the JTable, I use the following code to populate that table:

DocentesUI.groovy

...
def tm = sw.grdDocentes.model
tm.rowCount = 0
def doc = DocenteDespachador.obtenerDocentes()
doc.each {
    tm.addRow([it.codigo, it, it.apellidoPaterno, it.apellidoMaterno] as Object[])
}   

... 

ObtenerDocentes() is the method uses to get all the teachers from the databse. The second column (it) es the Docente instance itself and, as expected, it displays the nombre property calling the toString() method. I do this, because I find it convinient to get the second column of this table whenever I with to get the other properties of the object.

Now, on another user interface, I’d like to display these teachers in a JList, but in a diferent format. Here is where the metaClass comes in. In this other interface, I’d like to override the toString() on my Docente class. So, for that, I use the following:

AsignarDocenteUI.groovy

...
        def model = sw.lstDocentesDisponibles.model
        Docente.metaClass.toString = {
            return "No entiendo"
        }           
        def docentes = DocenteDespachador.obtenerDocentes()
        docentes.each {
            println it.toString()
            println it
            model.addElement it
        }

...

Here, lstDocentesDisponibles is the id of the JList. When the code reaches the println it.toString() line, it uses the overriden toString() and displays “no entiendo” to the default outputstream. However, when I look at the JList, the original toString() gets displayed. What am I missing here?

Any hints is appreciated.

Thanks,

Eduardo.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T18:42:53+00:00Added an answer on June 7, 2026 at 6:42 pm

    My bet: JList won’t go through metaClass. What about decorating your model?

    class DocenteForJList { 
      Docente docente
      String toString() { docente.with { "$nombre ($codigo)" } }
    }
    
    def docentes = DocenteDespachador.obtenerDocentes()
    docentes.each {
      model.addElement new DocenteForJList(docente:it)
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Groovy code something simple: #!/usr/bin/env groovy public class test { boolean val def
We are using grails-groovy with Hibernate, I have a SQL query like this in
i have a groovy application, i want to store a value to re use
I have several Groovy beans defined within my app context file, like so: <lang:groovy
In groovy if i have the code like this : def num = 9
I am experimenting with Groovy closures and delegates right now. I have the following
The application that I would like to revamp is pretty well organized. UI is
In a groovy tutorial, I encountered the following code: class DateTagLib { def thisYear
I have a Groovy app which uses a scrollPane built via swing builder: BinsicWindow(def
In Groovy, how do I extract a new list from the following: def people

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.