динамически добавляя TitledPanes в Accordion - PullRequest
0 голосов
/ 18 марта 2020

Итак, я создаю программу, которая заставляет чванца сделать c. json, извлекает все REST Apis, затем показывает их в Аккордеоне: проблема в том, что Аккордеон не будет работать для меня, потому что TitledPanes добавляются динамически: то есть: для каждой конечной точки API Rest, извлеченной из файла JSON, будет отображаться новый TitledPane.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import io.swagger.models.HttpMethod;
import io.swagger.models.Operation;
import io.swagger.models.Path;
import io.swagger.models.Response;
import io.swagger.models.Swagger;
import io.swagger.models.parameters.Parameter;
import io.swagger.parser.SwaggerParser;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class GroupOfTitledPane extends Application {
    JFrame frame;
    JTextField t1;
    JButton btn;
    int i;
    @Override
    public void start(final Stage stage) {
        frame=new JFrame();
        frame.setLayout(null);
        frame.setSize(400,250);
        frame. setLocation(500,300);
        t1=new JTextField();
        t1.setBounds(60,40,270,30);
        frame.add(t1);
        btn=new JButton("SUBMIT");
        btn.setBounds(140,90,100,30);
        frame.add(btn);
        frame.setVisible(true); 
        btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(e.getSource()==btn)
                {
                    String input=t1.getText();
                    if(input.isEmpty() || input.equals(" "))
                    {
                    JOptionPane.showMessageDialog(null,"Veuillez saisir l'adresse de l'URL");
                    }
                    else{
                        URL urldoc = null;
                        try {
                            urldoc = new URL(input);
                        } 
                        catch (MalformedURLException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }
                        String url = new String("https://"+urldoc.getAuthority()+"/v2/swagger.json"); 
                        System.out.println(url);
                        Swagger swagger = new SwaggerParser().read(url);
                        Map<String, Path> paths = swagger.getPaths(); 
                        // Create Root Pane.
                        VBox root = new VBox();
                        root.setPadding(new Insets(20, 10, 10, 10));
                        for (Map.Entry<String, Path> p : paths.entrySet()) {
                            Path path = p.getValue();
                            Map<HttpMethod, Operation> operations = path.getOperationMap();
                            for (java.util.Map.Entry<HttpMethod, Operation> o : operations.entrySet()) {
                                System.out.println("===");
                                System.out.println("PATH:" + p.getKey());
                                System.out.println("Http method:" + o.getKey());
                                System.out.println("Summary:" + o.getValue().getSummary());
                                System.out.println("Parameters number: " + o.getValue().getParameters().size());
                                for (Parameter parameter : o.getValue().getParameters()) {
                                    System.out.println(" - " + parameter.getName());
                                }
                                System.out.println("Responses:");
                                for (Map.Entry<String, Response> r : o.getValue().getResponses().entrySet()) {
                                    System.out.println(" - " + r.getKey() + ": " + r.getValue().getDescription());
                                }           
                                // Create First TitledPane.
                                TitledPane firstTitledPane = new TitledPane();
                                firstTitledPane.setText((o.getKey()).toString() + ":" + (p.getKey()).toString());

                                VBox content1 = new VBox();
                                content1.getChildren().add(new Label("Java Swing Tutorial"));
                                content1.getChildren().add(new Label("JavaFx Tutorial"));
                                content1.getChildren().add(new Label("Java IO Tutorial"));

                                firstTitledPane.setContent(content1);


                                root.getChildren().addAll(firstTitledPane);
                                Scene scene = new Scene(root, 300, 200);
                                stage.setScene(scene);
                                stage.show();

                            }
                        }

                    }
                }
            }
        });
    }
    public static void main(String[] args) {
    Application.launch(args);
    }
} 

А вот ошибка:

https://petstore.swagger.io/v2/swagger.json 0 [AWT-EventQueue-0] INFO io.swagger.parser.Swagger20Parser - reading from https://petstore.swagger.io/v2/swagger.json
=== PATH:/pet/{petId} Http method:GET Summary:Find pet by ID Parameters number: 1
 - petId Responses:
 - 200: successful operation
 - 400: Invalid ID supplied
 - 404: Pet not found Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: 
   Not on FX application thread; currentThread = AWT-EventQueue-0   
at com.sun.javafx.tk.Toolkit.checkFxUserThread(Toolkit.java:279)     
atcom.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(QuantumToolkit.java:444)   at javafx.stage.Window.setShowing(Window.java:939)  at javafx.stage.Window.show(Window.java:955)    at javafx.stage.Stage.show(Stage.java:259)  at GroupOfTitledPane$1.actionPerformed(GroupOfTitledPane.java:102)  at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)   at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)   at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)   at javax.swing.DefaultButtonModel.setPressed(Unknown Source)    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)     at java.awt.Component.processMouseEvent(Unknown Source)     at javax.swing.JComponent.processMouseEvent(Unknown Source)     at java.awt.Component.processEvent(Unknown Source)  at java.awt.Container.processEvent(Unknown Source)  at java.awt.Component.dispatchEventImpl(Unknown Source)     at java.awt.Container.dispatchEventImpl(Unknown Source)     at java.awt.Component.dispatchEvent(Unknown Source)     at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)     at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)     at java.awt.Container.dispatchEventImpl(Unknown Source)     at java.awt.Window.dispatchEventImpl(Unknown Source)    at java.awt.Component.dispatchEvent(Unknown Source)     at java.awt.EventQueue.dispatchEventImpl(Unknown Source)    at java.awt.EventQueue.access$500(Unknown Source)   at java.awt.EventQueue$3.run(Unknown Source)    at java.awt.EventQueue$3.run(Unknown Source)    at java.security.AccessController.doPrivileged(Native Method)   at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)    at java.awt.EventQueue$4.run(Unknown Source)    at java.awt.EventQueue$4.run(Unknown Source)    at java.security.AccessController.doPrivileged(Native Method)   at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)    at java.awt.EventQueue.dispatchEvent(Unknown Source)    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)  at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)     at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)  at java.awt.EventDispatchThread.pumpEvents(Unknown Source)  at java.awt.EventDispatchThread.pumpEvents(Unknown Source)  at java.awt.EventDispatchThread.run(Unknown Source)
...