5

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?
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Deval Khandelwal
  • 3,458
  • 1
  • 27
  • 38
  • no idea what did you talking about, you can get the access to any Object in current JVM from any of corners, question is about two downvotes not upvotes – mKorbel Feb 26 '14 at 12:26
  • ... starting with fact that JScrollPane is designated to nest only one Object – mKorbel Feb 26 '14 at 12:27
  • @mKorbel So how would I get it from that "one object", i.e., the jTable ? – Deval Khandelwal Feb 26 '14 at 12:33
  • there are three ways 1. define JScrollPane as local variable, 2. use parent/ancesor from SwingUtilities, 3. test for parent (as showing answer by @Oleg Estekhin) – mKorbel Feb 26 '14 at 12:38
  • @mKorbel , the code by oleg did not work. I am left with using SwingUtilities. But cannot find which method to use...i.e., JscrollPane pane = SwingUtilities.?? – Deval Khandelwal Feb 26 '14 at 12:41
  • by default you would need to only Point from Mouse/MouseInfo, a few similair questions here (I'm bet that about JTabbedPane), again correcr way is to define JScrollPane as local variable – mKorbel Feb 26 '14 at 12:58

2 Answers2

6

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

Patrick

Patrick
  • 4,532
  • 2
  • 26
  • 32
3

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.

Oleg Estekhin
  • 8,063
  • 5
  • 49
  • 52