将当前的JScrollPane替换为JPanel中的另一个JScrollPane

时间:2017-09-21 10:21:31

标签: user-interface jpanel containers jscrollpane

我想将当前的JScrollpane更改为新的。按下我的按钮后,我希望它能够改变,并且调用方法actionPerformed。 我目前遇到的问题是它只在应用程序开始时绘制Jscroll,当我想要更改它时,它起作用。(当应用程序运行时)。

我的工作是: 在应用程序的开头,我创建了一个新的JscrollPane,这个是空的。如果按下该按钮:显示另一个包含内容的JscrollPane。

if(btnPressed == true){

//set current empty jscroll pane to a filled one.
jscrollpane = View.createScrollPlane();


//View.createScrollPlane = This method fills the JscrollPane with text.
}
else { //show a empty one
 jscrollpane = new JscrollPane();

}

我试过了: - 去掉 - 加 - 重新验证 - 重新粉刷

还有: JscrollPane.setViewPortView(JScrollPane的);

我看过CardLayout,但我不愿意,因为只有空容器可以更改为CardLayout,所以它不允许我。目前它在GridBagLayout上。

提前致谢

重新编辑:Create-UI方法将当前空的Jscrollpanel更改为新的Jscrollpanel。它只初始化一次(在开头)但是更新Jscroll面板。 (当我试图把它放在假的时候)它起作用了,布尔值确实改为true但是更新了jscroll面板。

package readDataPluginPackage;

import com.change_vision.jude.api.inf.AstahAPI;
import com.change_vision.jude.api.inf.project.ProjectAccessor;
import javafx.embed.swing.JFXPanel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.beans.PropertyChangeListener;

public class Application {
    JPanel mainJPanel;
    private JPanel leftJPanel;
    private JPanel rightJPanel;
    private JButton btnSynchronise;
    private JButton btnPreview;
    private JScrollPane JScrollPaneReport;
    public JScrollPane JScrollPanePreview;
    private boolean btnPreviewClicked = false;

    public Application() {
        $$$setupUI$$$();

        btnPreview.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                btnPreviewClicked = true;



                JOptionPane.showMessageDialog(null, "Showing..." + btnPreviewClicked);


                //   ShowXMLFileView showXMLFileView = new ShowXMLFileView();
                //     JScrollPanePreview = showXMLFileView.createLabelPane();

                //   if (btnPreview.isEnabled()) {
                //  ShowXMLFileView showXMLFileView = new ShowXMLFileView();
                // JScrollPanePreview = showXMLFileView.createLabelPane();
                JOptionPane.showMessageDialog(null, "XML File Preview has been  updated.");


               createUIComponents();
                //   JScrollPanePreview.revalidate();
                // JScrollPanePreview.repaint();
                JOptionPane.showMessageDialog(null, "Components are created again.");

            }


            //  }


        });
        btnSynchronise.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null, "Synchronising...");
            }
        });


    }


    public static void main(String[] args) {
        try {

            //Save Astah Project as XML File
            ProjectAccessor prjAccessor = AstahAPI.getAstahAPI().getProjectAccessor();
            prjAccessor.open("C:\\Users\\delina\\generated\\test.asta");
            prjAccessor.exportXMI("C:\\Users\\delina\\generatedXMI\\temp.xml");
            prjAccessor.close();

            //Show the most recent version of the xml file of the Astah Project
            ReadXMLFile rd = new ReadXMLFile();
            rd.showXMLFileLines();

        } catch (Exception e) {
            e.printStackTrace();
        }


        JFrame frame = new JFrame("Application");
        frame.setContentPane(new Application().mainJPanel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    private void createUIComponents() {
        if (btnPreviewClicked == true) {


       //  ShowXMLFileView showXMLFileView = new ShowXMLFileView();
          // JScrollPanePreview = showXMLFileView.createLabelPane();
                 //  JScrollPanePreview.setViewportView(JScrollPanePreview);







            leftJPanel.remove(JScrollPanePreview);

            ShowXMLFileView showXMLFileView = new ShowXMLFileView();
            JScrollPane JScrollPanePreview = showXMLFileView.createLabelPane();


            leftJPanel.add(JScrollPanePreview);
        JScrollPanePreview.revalidate();
        JScrollPanePreview.repaint();







       JOptionPane.showMessageDialog(null, "JScrollPanel changed");
     } else {
            JScrollPanePreview = new JScrollPane();
     }


    }


    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        createUIComponents();
        mainJPanel = new JPanel();
        mainJPanel.setLayout(new GridBagLayout());
        leftJPanel = new JPanel();
        leftJPanel.setLayout(new GridBagLayout());
        GridBagConstraints gbc;
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        gbc.fill = GridBagConstraints.BOTH;
        mainJPanel.add(leftJPanel, gbc);
        btnSynchronise = new JButton();
        btnSynchronise.setText("Synchronise");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.weightx = 1.0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        leftJPanel.add(btnSynchronise, gbc);
        btnPreview = new JButton();
        btnPreview.setText("Preview");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.weightx = 1.0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        leftJPanel.add(btnPreview, gbc);
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        gbc.fill = GridBagConstraints.BOTH;
        leftJPanel.add(JScrollPanePreview, gbc);
        JScrollPanePreview.setBorder(BorderFactory.createTitledBorder("XML File Preview"));
        rightJPanel = new JPanel();
        rightJPanel.setLayout(new GridBagLayout());
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        gbc.fill = GridBagConstraints.BOTH;
        mainJPanel.add(rightJPanel, gbc);
        JScrollPaneReport = new JScrollPane();
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        gbc.fill = GridBagConstraints.BOTH;
        rightJPanel.add(JScrollPaneReport, gbc);
        JScrollPaneReport.setBorder(BorderFactory.createTitledBorder("Synchronise report"));
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return mainJPanel;
    }
}


    package readDataPluginPackage;

import com.change_vision.jude.api.inf.project.ProjectAccessor;
import com.change_vision.jude.api.inf.project.ProjectAccessorFactory;
import com.change_vision.jude.api.inf.project.ProjectEvent;
import com.change_vision.jude.api.inf.project.ProjectEventListener;
import com.change_vision.jude.api.inf.ui.IPluginExtraTabView;
import com.change_vision.jude.api.inf.ui.ISelectionListener;

import javax.swing.*;
import java.awt.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;


public class ShowUserInterface extends JPanel implements IPluginExtraTabView, ProjectEventListener {

    public ShowUserInterface() {
        initComponents();
    }

    private void initComponents() {
        setLayout(new BorderLayout());
        add(createLabelPane());
        addProjectEventListener();
    }

    private void addProjectEventListener() {
        try {
            ProjectAccessor projectAccessor = ProjectAccessorFactory.getProjectAccessor();
            projectAccessor.addProjectEventListener(this);
        } catch (ClassNotFoundException e) {
            e.getMessage();
        }
    }

    private Container createLabelPane() {
        JLabel label = new JLabel("AuguSoft Synchronise");
        JScrollPane pane = new JScrollPane(label);


        Method privateMethod = null;
        Application app = null;
        Object o = null;
        JComponent jComponent = null;
        try {
            app = new Application();
            privateMethod = Application.class.getDeclaredMethod("$$$setupUI$$$");
            privateMethod.setAccessible(true);
            o = privateMethod.invoke(app);
            jComponent = app.$$$getRootComponent$$$();



        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return jComponent;
    }





    @Override
    public void projectChanged(ProjectEvent e) {
    }

    @Override
    public void projectClosed(ProjectEvent e) {
    }

    @Override
    public void projectOpened(ProjectEvent e) {
    }

    @Override
    public void addSelectionListener(ISelectionListener listener) {
    }

    @Override
    public Component getComponent() {
        return this;
    }

    @Override
    public String getDescription() {
        return "Show AuguSoft Synchronise here";
    }

    @Override
    public String getTitle() {
        return "AuguSoft View";
    }

    public void activated() {

    }

    public void deactivated() {

    }
}

1 个答案:

答案 0 :(得分:1)

所以,除了查看代码之外别无其他,我注意到在您的createUIComponents方法中,您正在隐藏JScrollPanePreview属性......

public class Application {

    //...    
    public JScrollPane JScrollPanePreview;
    //...    
    public Application() {..}

    private void createUIComponents() {
        if (btnPreviewClicked == true) {
            //...
            leftJPanel.remove(JScrollPanePreview);

            ShowXMLFileView showXMLFileView = new ShowXMLFileView();
            JScrollPane JScrollPanePreview = showXMLFileView.createLabelPane();
            //...
        } else {
            JScrollPanePreview = new JScrollPane();
        }

    }

这意味着下次您更换JScrollPanePreview时,您将没有正确的引用来删除它。

在我看来(我没有完整的代码库或意图),我只需更换JScrollPanePreview视图端口(此外,我不知道如何分配{{1}无论如何,到Container:P)

JScrollPane

正如观察一样;)