Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access the JScrollpane in which the JTable is contained

I have a JTable inside a JScrollpane. I do not have access to the JScrollpane variable. But I have access to the JTable. Now how can I access the JScrollpane using the JTable.

For Example -> mytable.getAncestor(...) or something?
like image 878
Dev Avatar asked Feb 13 '23 16:02

Dev


2 Answers

If you want to get the JScrollPane from your JTable use

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

Try the code below..

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 <[email protected]>
 * @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());
    }
}

Patrick

like image 129
Patrick Avatar answered Feb 19 '23 08:02

Patrick


Assuming that JTable is placed inside the JScrollPane as is the usual:

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

you can access the scroll pane that wraps the table by using the getParent() to get the viewport and using getParent() on viewport to get the scroll pane.

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

at worst, if you for some reason not sure whether the table is inside scroll pane or not, you will have to check instanceof of the table' parent component before casting.

like image 45
Oleg Estekhin Avatar answered Feb 19 '23 07:02

Oleg Estekhin