从JFrame外部更改和更新JPanel组件不起作用,摆动 [英] Changing and Updating JPanel Components externally, from JFrame is not Working, Swing

查看:91
本文介绍了从JFrame外部更改和更新JPanel组件不起作用,摆动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JPanel(更改其组件),但是我想从外部JFrame进行更改.

I was working with JPanel (Changing its component), but I want to change it from External JFrame.

对不起,我使用Netbeans编写了这段代码(我知道它放了该问题不需要的一些东西),并尝试对其进行干净的编辑,因为它的真实代码更大了

这里是JPanel的代码,名称为"MyPanel".

Here the code of the JPanel, with name 'MyPanel'.

    public class MyPanel extends javax.swing.JPanel {

      private javax.swing.JButton filling = new javax.swing.JButton();
      private javax.swing.JScrollPane jScrollPane1 = new javax.swing.JScrollPane();
      private javax.swing.JTable myTable = new javax.swing.JTable();

      private final javax.swing.table.DefaultTableModel INITIAL_TABLE_MODEL = new javax.swing.table.DefaultTableModel(
          new Object[][]{},
          new String[]{"Text", "Integer"}
      );

      public MyPanel() {
        initComponents();
        //callFilling(); // INNER CALL!
      }

      private void initComponents() {
        filling.setText("filling");
        filling.addActionListener(new java.awt.event.ActionListener() {
          public void actionPerformed(java.awt.event.ActionEvent evt) {
            fillingActionPerformed(evt);
          }
        });

        myTable.setModel(new javax.swing.table.DefaultTableModel(
          new Object [][] { },
          new String [] { "Text", "Integer" }
       ) {
          Class[] types = new Class [] {
            java.lang.String.class, java.lang.Integer.class
          };

          public Class getColumnClass(int columnIndex) {
            return types [columnIndex];
          }
        });
        jScrollPane1.setViewportView(myTable);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
          layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(filling)
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
          .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
          layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(filling)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
      }                   

      public void callFilling() {
        myTable.setModel(INITIAL_TABLE_MODEL);
        INITIAL_TABLE_MODEL.setRowCount(0);
        ((javax.swing.table.DefaultTableModel) myTable.getModel()).setRowCount(0);
        Integer numberRows = new java.util.Random().nextInt((10 - 2) + 1) + 2;
        for (int i = 0; i < numberRows; i++) {
          Integer number = new java.util.Random().nextInt();
          ((javax.swing.table.DefaultTableModel) myTable.getModel()).addRow(
              new Object[]{
                number.toString(), number
              });
        }
        this.revalidate();
        this.repaint();
      }

      private void fillingActionPerformed(java.awt.event.ActionEvent evt) {                                        
        callFilling();
      }
    }

框架!!!

public class MainFrame extends javax.swing.JFrame {

  private javax.swing.JTabbedPane myTabbed = new javax.swing.JTabbedPane();

  public MainFrame() {
    initComponents();
    MyPanel myPanel = addNewTab();
    myPanel.callFilling(); //OUTER CALL!
    myPanel.revalidate();
    myPanel.repaint();
    addNewTab();
  }

  private MyPanel addNewTab() {
    int idx = myTabbed.getTabCount();
    String title = "myTabbed: " + idx + " ";
    MyPanel myPanel = new MyPanel();
    myTabbed.insertTab(title, null, new MyPanel(), null, idx);
    return myPanel;
  }

  private void initComponents() {
    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addComponent(myTabbed, javax.swing.GroupLayout.DEFAULT_SIZE, 394, Short.MAX_VALUE)
        .addContainerGap())
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addComponent(myTabbed, javax.swing.GroupLayout.DEFAULT_SIZE, 288, Short.MAX_VALUE)
        .addContainerGap())
    );

    pack();
  }

  public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(() -> {
      new MainFrame().setVisible(true);
    });
  }
}

当执行callFilling();时,可以从JPanel(查看内部调用)进行操作!但是,从JFrame( review//外部调用)可以进行操作!

When the callFilling(); is do it from JPanel (review INNER CALL) works!, but from JFrame (review //OUTER CALL) doesn't work!

审查此问题更新JPanel的内容,单击一个框架后单击另一个框架 我使用了建议的revalidate(); repaint();代码,但是没有用.

Reviewing this question Update content of JPanel on a frame on button click in another frame The proposed revalidate(); repaint(); code I used, but is not working.

如何解决这个问题?

推荐答案

MyPanel myPanel = addNewTab();
myPanel.callFilling(); //OUTER CALL!
myPanel.revalidate();
myPanel.repaint();
addNewTab();

不确定您要做什么.

您创建一个MyPanel对象,并在其上调用callFilling()来设置TableModel中的值.但是您实际上从未将面板添加到框架中.

You create a MyPanel object and invoke callFilling() on it to set the values in the TableModel. But you never actually add the panel to the frame.

在将组件添加到可见面板时,仅需要调用revalidate()和repaint()即可.

You only need to invoke revalidate() and repaint() when you add components to a visible panel.

然后您调用addNewTab(),它执行以下操作:

Then you invoke addNewTab() which does:

MyPanel myPanel = new MyPanel();
myTabbed.insertTab(title, null, new MyPanel(), null, idx);
return myPanel;

同样,创建2个MyPanel对象没有任何意义.将一个对象添加到选项卡式窗格中,然后返回第二个MyPanel对象.

Again, this doesn't make any sense you create 2 MyPanel objects. You add one object to a tabbed pane, but then you return the second MyPanel object.

因此,您已经创建了3个MyPanel对象.

So you have created 3 MyPanel objects.

我真的不确定您要做什么.我不知道您是要在面板中添加面板还是在选项卡式窗格中添加面板,因此我无法提出具体建议.

I really am not sure what you are trying to do. I don't know if you are trying to add a panel to the frame, or a panel to the tabbed pane, so I can't really make a specific suggestion.

无论如何,您都需要对代码进行结构化,以便仅在MyObject对象上创建,并且需要将该面板添加到选项卡式窗格或框架中,而不是两者都添加.

In any case you need to structure you code so that only on MyObject object is created and you need to add that panel to either the tabbed pane or the frame, but not both.

这篇关于从JFrame外部更改和更新JPanel组件不起作用,摆动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆