0

I'm trying to create jtable use users pojo class. User create pojo class, I load it in runtime and create jtable. Help me,please, how can I achieve that?

String loadedClass = array.getJSONObject( 0 ).getString( "value" );
Class customFilterClass = new LoaderDynClass( getContext(), pack ).loadClass( loadedClass, true );

customFilterClass - users class type

StringReader reader = new StringReader( filterList );
//customFilterClass extent FilterTableModel. FilterTableModel parent abstract class
List<FilterTableModel> filterTableModelList = objectMapper.readValue( reader, new TypeReference<List<FilterTableModel>>(){} );

I can do this with reflection API, but I don't know how to receive List<UserClass> list. And how to indicate type of my List<???>

So sorry for my english))

BGUTable table = (BGUTable) component;

                        ObjectMapper objectMapper = new ObjectMapper(  );
                        objectMapper.disable( DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
                        objectMapper.setVisibilityChecker( VisibilityChecker.Std.defaultInstance().withFieldVisibility( JsonAutoDetect.Visibility.ANY));

                        JSONArray array = fieldsJsonObject.optJSONArray( key );
                        String loadedClass = array.getJSONObject( 0 ).getString( "value" );
                        String filterList = array.getJSONObject( 1 ).getString( "value" );

                        Class customFilterClass = null;
                        try
                        {
                            String pack = loadedClass.substring( 0, loadedClass.lastIndexOf( "." ) );
                            customFilterClass = new LoaderDynClass( getContext(), pack ).loadClass( loadedClass, true );
                        }
                        catch( ClassNotFoundException ex )
                        {
                            ClientUtils.showErrorMessageDialog( ex );
                        }
                        StringReader reader = new StringReader( filterList );

                        try
                        {
                            List filterTableModelList = objectMapper.readValue( reader, new TypeReference<List>(){} );
                            Object[] objects = new Object[filterTableModelList.size()];
                            List<Field> fieldsList = new ArrayList<>();
                            for( int x =0; x < objects.length; x++ )
                            {
                                try
                                {
                                    Field[] fields = customFilterClass.getDeclaredFields();
                                    for( int i = 0; i < fields.length; i++ )
                                    {
                                        fieldsList.add( fields[i] );
                                    }

                                    objects[x] = customFilterClass.getDeclaredConstructor( fields[0].getType(), fields[1].getType(), fields[2].getType() ).newInstance( 77,"asdasd", 12 );
                                }
                                catch( InstantiationException e )
                                {
                                    e.printStackTrace();
                                }
                                catch( IllegalAccessException e )
                                {
                                    e.printStackTrace();
                                }
                                catch( InvocationTargetException e )
                                {
                                    e.printStackTrace();
                                }
                                catch( NoSuchMethodException e )
                                {
                                    e.printStackTrace();
                                }
                            }
                            List<FilterTableModel> list = new ArrayList<>();
                            for(int z = 0; z < objects.length; z++)
                            {
                                list.add( (FilterTableModel)objects[z] );
                            }
                            BGTableModel<FilterTableModel> model = new BGTableModel<FilterTableModel>("")
                            {
                                @Override
                                protected void initColumns()
                                {
                                    for( int x = 0; x < fieldsList.size(); x++ )
                                    {
                                        addColumn( fieldsList.get( x ).getName(), fieldsList.get( x ).getType(), false );
                                    }
                                }

                                @Override
                                public Object getValue( FilterTableModel val, int columnId)
                                throws BGException
                                {
                                    FilterTableModel filterTableModel = val;
                                    String data = filterTableModel.getCorrectData( columnId );
                                    if ( data != null )
                                    {
                                        return data;
                                    }
                                    return super.getValue( val, columnId );
                                }
                            };

                            table.setModel( model );
                            model.setData( list );
                        }
                        catch( IOException e )
                        {
                            ClientUtils.showErrorMessageDialog( e );
                        }

this test variant, because i don't have idea what is do

  • Welcome to StackOverflow :) So, you are trying to read a POJO and display that in the GUI, right? If so, why do you need reflection for that? Also, please have a look at [this help article](https://stackoverflow.com/help/how-to-ask) and clarify your question. Cheers :) – vatbub Jan 17 '20 at 10:59
  • Yes, i'm trying get List for my jtable. POJO class user created and I dont have information about him. – Valery Zernov Jan 17 '20 at 11:17
  • Ok, in that case, could you please post a little more code? It would be especially great if you could post what you have attempted so far. Also, do have a look at the help article I linked before. Thanks :) – vatbub Jan 17 '20 at 11:21
  • ok. im added code) – Valery Zernov Jan 17 '20 at 12:26
  • *POJO class user created and I dont have information about him.* Maybe you can use the [Bean Table Model](https://tips4java.wordpress.com/2008/11/27/bean-table-model/). This model uses reflection to get the properties of the POJO to display in the table. – camickr Jan 17 '20 at 15:45

1 Answers1

0

Welcome,

I had that same problem. Using the JSON response I had from the API, I used jsonschema2pojo to automatically create the main class and sub-sequent classes to map the POJOs and BeanTableModel to create a custom JTable.

Using the easy to use GSON library I then only had to pass the API response through the class created with jsonschema2pojo, and extract a List then iterate through that list and add each element to the JTable using a for() loop much like you did.

BeanTableModel: https://tips4java.wordpress.com/2008/11/27/bean-table-model/

Check my question: Getting POJO objects into JTable

http://www.jsonschema2pojo.org/

Check out https://www.youtube.com/watch?v=ou2yFJ-NWr8, Future Studio has an extensive tutorial series on extracting POJOs from JSON using GSON.

ADSquared
  • 207
  • 3
  • 12