1

我想创建 2 个 JTabbedPanes,它们将在其中一列中具有相同的 JTable 但不同的值。现在由于某种原因只显示了一个选项卡,我不知道为什么。我也是通过创建 2 个不同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");
4

1 回答 1

5

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

  • 只创建一个 DefaultTableModel,

  • 然后所有更改都会同步,

  • 每个 JTable 可以有不同的 ColumnModel、Renderer 等

尽可能简单的(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();    
            }
        });
    }
}
于 2013-04-25T18:41:47.013 回答