访问包含JTable的JScrollpane

时间:2014-02-26 11:59:42

标签: java swing jtable jscrollpane swingutilities

我在JScrollpane中有一个JTable。我无权访问JScrollpane变量。但我可以访问JTable。现在我如何使用JTable访问JScrollpane。

For Example -> mytable.getAncestor(...) or something?

2 个答案:

答案 0 :(得分:5)

如果您想从JTable使用

获取JScrollPane
JTable jTable = new JTable(rowData, colData);
JScrollPane scrollPane = new JScrollPane(jTable);
// now you have the ViewPort
JViewport parent = (JViewport)jTable.getParent();
JScrollPane enclosing = (JScrollPane)parent.getParent();

尝试以下代码..

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRootPane;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;

/**
 *
 * @author Patrick Ott <Patrick.Ott@professional-webworkx.de>
 * @version 1.0
 */
public class MainFrame extends JFrame {

    private String[][] rowData = 
    {
        {"Max", "Mustermann", "Musterhausen"},
        {"Max", "Mustermann", "Musterhausen"},
        {"Max", "Mustermann", "Musterhausen"},
        {"Max", "Mustermann", "Musterhausen"},
        {"Max", "Mustermann", "Musterhausen"},
        {"Max", "Mustermann", "Musterhausen"},
        {"Max", "Mustermann", "Musterhausen"},
        {"Max", "Mustermann", "Musterhausen"},
        {"Max", "Mustermann", "Musterhausen"},
        {"Max", "Mustermann", "Musterhausen"},
        {"Max", "Mustermann", "Musterhausen"},
        {"Max", "Mustermann", "Musterhausen"},
        {"Max", "Mustermann", "Musterhausen"},
        {"Max", "Mustermann", "Musterhausen"},
        {"Max", "Mustermann", "Musterhausen"},
        {"Max", "Mustermann", "Musterhausen"},
        {"Max", "Mustermann", "Musterhausen"},
        {"Max", "Mustermann", "Musterhausen"},
        {"Max", "Mustermann", "Musterhausen"},
        {"Petra", "Mustermann", "Musterhausen"}
    };

    private String[] columnData = 
    {
        "Firstname", "Lastname", "City"
    };
    private JTable jTable;

    public MainFrame() {
        jTable = new JTable(rowData, columnData);
        jTable.setName("CRM Table");
    }

    public void createAndShowGui() {
        this.setTitle("JTable in JScrollPane");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        this.getContentPane().add(new JLabel("CRM System"), BorderLayout.NORTH);
        JScrollPane scrollPane = new JScrollPane(jTable);
        this.getContentPane().add(scrollPane, BorderLayout.CENTER);
        this.setSize(new Dimension(1024, 768));
        this.setVisible(true);
        Container parent = jTable.getParent().getParent();
        JScrollPane enclosing = (JScrollPane)parent;
        parent.remove(jTable);
        parent.add(new JLabel("Test"));
        // System.out.println(enclosing.getClass().getSimpleName());
    }
}
帕特里克

答案 1 :(得分:2)

假设JTable像往常一样放在JScrollPane中:

JTable table = ...;
JScrollPane scrollPane = new JScrollPane(table);

您可以通过使用getParent()获取视口并使用视口上的getParent()获取滚动窗格来访问包装表的滚动窗格。

JScrollPane enclosingScrollPane = (JScrollPane) table.getParent().getParent()

最糟糕的是,如果由于某种原因不确定表是否在滚动窗格内,则必须在转换之前检查表'父组件的instanceof。