creating the same jtable in 2 different JtabbedPan

2019-08-04 13:59发布

问题:

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");

回答1:

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?

  • create only one DefaultTableModel,

  • then all changes are synchonized,

  • each of JTables can have different ColumnModel, Renderers, etc

simplest (SSCCE) code as is possible

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();    
            }
        });
    }
}