在2个不同的JtabbedPanes中创建相同的jtable [英] creating the same jtable in 2 different JtabbedPanes

查看:73
本文介绍了在2个不同的JtabbedPanes中创建相同的jtable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建2个JTabbedPanes,它们在一个列中具有相同的JTable但值不同.由于某种原因,现在仅显示一个选项卡,我不确定为什么.我也是通过创建2个不同的DefaultTableModel来做到这一点的最佳方法吗?

I want to create 2 JTabbedPanes that will have the same JTable but different values in one of the columns. Right now for some reason only one of the tabs is showing up and I am not sure why. Also am I doing this the best way by creating 2 different DefaultTableModel's?

public static void tableMaker(DefaultTableModel m, DefaultTableModel m1, final Map<String, NumberHolder> uaCount)
{
    final JFrame frame = new JFrame("Strings");     
    JPanel panel1 = new JPanel();
            JPanel panel2 = new JPanel();
    JTabbedPane tabbedPane = new JTabbedPane();

    m.addColumn("String");
    m.addColumn("Occurrences");
            m1.addColumn("String");
    m1.addColumn("Occurrences");
    JTable table = new JTable(m);
            JTable table = new JTable(m1);
    table.setAutoCreateRowSorter(true);
    JScrollPane scrollPane1 = new JScrollPane(table1);
            JScrollPane scrollPane2 = new JScrollPane(table2);
    frame.getContentPane().setLayout(new BorderLayout());                                                       
    panel1.add(scrollPane1);
            panel2.add(scrollPane2);
    frame.getContentPane().add(panel1, BorderLayout.CENTER);
            frame.getContentPane().add(panel2, BorderLayout.CENTER);        
    frame.add(tabbedPane, BorderLayout.NORTH);
    tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
    tabbedPane.addTab("Tab 1", null, panel1,
                "String length 2");
    tabbedPane.addTab("Tab 1", null, panel2,
                "String length 2");

推荐答案

我想创建2个具有相同JTable的JTabbedPanes,但是 列之一中的不同值.现在仅出于某种原因 其中一个标签页正在显示,我不确定为什么.我也在做 这是通过创建2个不同的DefaultTableModel的最佳方法?

I want to create 2 JTabbedPanes that will have the same JTable but different values in one of the columns. Right now for some reason only one of the tabs is showing up and I am not sure why. Also am I doing this the best way by creating 2 different DefaultTableModel's?

  • 仅创建一个DefaultTableModel

    • create only one DefaultTableModel,

      然后所有更改都被同步,

      then all changes are synchonized,

      每个JTables可以具有不同的ColumnModel,渲染器等

      each of JTables can have different ColumnModel, Renderers, etc

      尽可能使用简单的(SSCCE)代码

      import java.awt.BorderLayout;
      import java.awt.EventQueue;
      import javax.swing.JFrame;
      import javax.swing.JScrollPane;
      import javax.swing.JTabbedPane;
      import javax.swing.JTable;
      import javax.swing.table.DefaultTableModel;
      
      public class MyTabbedPane {
      
          private JTabbedPane tabbedPane = new JTabbedPane();
          private JFrame f = new JFrame();
          private String[] columnNames = {"First Name", "Last Name", "Sport",
              "# of Years", "Vegetarian"};
          private Object[][] data = {
              {"Kathy", "Smith", "Snowboarding", new Integer(5), (false)},
              {"John", "Doe", "Rowing", new Integer(3), (true)},
              {"Sue", "Black", "Knitting", new Integer(2), (false)},
              {"Jane", "White", "Speed reading", new Integer(20), (true)},
              {"Joe", "Brown", "Pool", new Integer(10), (false)}
          };
          private DefaultTableModel model = new DefaultTableModel(data, columnNames) {
              private static final long serialVersionUID = 1L;
      
              @Override
              public Class getColumnClass(int column) {
                  return getValueAt(0, column).getClass();
              }
          };
      
          public MyTabbedPane() {
              tabbedPane.addTab("Tab1", new JScrollPane(new JTable(model)));
              tabbedPane.addTab("Tab2", new JScrollPane(new JTable(model)));
              tabbedPane.addTab("Tab3", new JScrollPane(new JTable(model)));
              tabbedPane.addTab("Tab4", new JScrollPane(new JTable(model)));
              tabbedPane.setTabPlacement(JTabbedPane.TOP);
              tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
              f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              f.add(tabbedPane, BorderLayout.CENTER);
              f.pack();
              f.setVisible(true);
          }
      
          public static void main(String args[]) {
              EventQueue.invokeLater(new Runnable() {
                  @Override
                  public void run() {
                      MyTabbedPane frame = new MyTabbedPane();    
                  }
              });
          }
      }
      

      这篇关于在2个不同的JtabbedPanes中创建相同的jtable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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