在运行时动态创建JTable

问题描述 投票:0回答:1

我正在尝试创建jtable使用用户pojo类。用户创建pojo类,我在运行时加载它并创建jtable。请帮助我,我该如何实现?

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

customFilterClass-用户类类型

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

我可以使用reflection API来执行此操作,但是我不知道如何接收List<UserClass> list。以及如何指示我的List<???>

的类型

[抱歉,我的英语))

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 );
                        }

此测试变体,因为我不知道该怎么办

java swing reflection jtable runtime
1个回答
0
投票

欢迎,

我有同样的问题。使用从API获得的JSON响应,我使用jsonschema2pojo自动创建了主类和子类,以映射POJO和BeanTableModel来创建自定义JTable。

使用易于使用的GSON库,我只需要通过jsonschema2pojo创建的类传递API响应,提取一个List然后遍历该列表,并使用for()循环将每个元素添加到JTable中,就像您一样做到了。

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

检查我的问题:Getting POJO objects into JTable

http://www.jsonschema2pojo.org/

[签出https://www.youtube.com/watch?v=ou2yFJ-NWr8,Future Studio拥有有关使用GSON从JSON提取POJO的广泛教程系列。

© www.soinside.com 2019 - 2024. All rights reserved.