JTable没有显示 [英] JTable not showing

查看:101
本文介绍了JTable没有显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,一切都是分发的。

In my application everything is distributed.


  • 在某个操作上,应用程序从数据库中检索数据并保存在 ArrayList< T>

  • 我创建了一个 RelativeTableModel 的对象,其中我传递了 ArrayList< T>

  • On a action, application retrieves data from DB and saves in ArrayList<T>.
  • I create an object of RelativeTableModel where I pass the ArrayList<T>.

public void RelationsClicked() {
    ArrayList<Relation> data = myParent.dbOperation.getRelations();
    RelativeTableModel tModel = new RelativeTableModel(data);  // subclass of AbstractTableModel
    myParent.SetBrowsePanelData(tModel);
    myParent.SetMainPanel(CashAccountingView.BROWSEPANEL);
}




  • 我有 BrowseListPanel JScrollPane 中具有 JTable 的类。它的实例已在主应用程序中创建。

  • 我将模型传递给 BrowseListPanel ,最后显示面板。

    • I have a BrowseListPanel class that has a JTable in JScrollPane. Its instance is already created in the main application.
    • I pass the model to BrowseListPanel and finally show the panel.
    • 代码:

      public void SetBrowsePanelData(AbstractTableModel tModel) {
          browsePanel.setTModel(tModel);
      }
      
      // BrowseListPanel's Code
      public void setTModel(AbstractTableModel tModel) {
          this.tModel = tModel;  // tModel = AbstractTableModel
      }
      
      // Show the Panel
      public void SetMainPanel(String panel) {
          activePanel = panel;
          SetFontSize();
          cards.show(mainPanel, panel);
          mainPanel.revalidate();
          mainPanel.repaint();
      }
      

      但是我看不到。我相信已经创建了 BrowseListPanel (包含 JTable )的对象。之后添加 TableModel 。所以应该在 setTModel()中触发某种事件。

      But I don't see the Table. I believe as the object of BrowseListPanel (containing the JTable) is already created & later the TableModel is added. So some sort of event should be fired in setTModel().

      我是对的吗?如果是这样,应该抛出什么事件以及它的实现应该是什么。

      Am I right? If so, what event should be thrown and what should be its implementation.

      推荐答案

      调用 setModel()表上的 应该足够了,但你可以在模型上明确地调用 fireTableStructureChanged()作为一种帮助解决问题的方法。

      Invoking setModel() on the table should be sufficient, but you might call fireTableStructureChanged() on the model explicitly as a way to help sort things out.

      另外,请确认您正在使用事件发送线程

      Also, verify that you are working on the event dispatch thread.

      附录:这是一个显示基本方法的sscce。

      Addendum: Here's an sscce that shows the basic approach.

      import java.awt.Dimension;
      import java.awt.EventQueue;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      import javax.swing.JComboBox;
      import javax.swing.JFrame;
      import javax.swing.JPanel;
      import javax.swing.JScrollPane;
      import javax.swing.JTable;
      import javax.swing.table.DefaultTableModel;
      
      /** @see http://stackoverflow.com/questions/8257148 */
      public class SwapTableModel extends JPanel {
      
          public SwapTableModel() {
              final JTable table = new JTable(Model.Alpha.model);
              table.setPreferredScrollableViewportSize(new Dimension(128, 32));
              this.add(new JScrollPane(table));
              final JComboBox combo = new JComboBox();
              for (Model model : Model.values()) {
                  combo.addItem(model);
              }
              this.add(combo);
              combo.addActionListener(new ActionListener() {
      
                  @Override
                  public void actionPerformed(ActionEvent e) {
                      Model model = (Model) combo.getSelectedItem();
                      table.setModel(model.model);
                  }
              });
          }
      
          private enum Model {
      
              Alpha(), Beta();
              private DefaultTableModel model;
      
              private Model() {
                  Object[] data = {this.toString()};
                  this.model = new DefaultTableModel(data, 1);
                  model.addRow(data);
              }
          }
      
          private void display() {
              JFrame f = new JFrame("SwapTableModel");
              f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              f.add(this);
              f.pack();
              f.setLocationRelativeTo(null);
              f.setVisible(true);
          }
      
          public static void main(String[] args) {
              EventQueue.invokeLater(new Runnable() {
      
                  @Override
                  public void run() {
                      new SwapTableModel().display();
                  }
              });
          }
      }
      

      这篇关于JTable没有显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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