Access the JScrollpane in which the JTable is contained
Asked Answered
U

2

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?
Unworthy answered 26/2, 2014 at 11:59 Comment(6)
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 upvotesHammel
... starting with fact that JScrollPane is designated to nest only one ObjectHammel
@Hammel So how would I get it from that "one object", i.e., the jTable ?Unworthy
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)Hammel
@Hammel , 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.??Unworthy
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 variableHammel
S
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 <[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

Sophocles answered 26/2, 2014 at 12:42 Comment(0)
B
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.

Biotic answered 26/2, 2014 at 12:34 Comment(3)
The code does not work... :( The error being-> javax.swing.JViewport cannot be cast to javax.swing.JScrollPaneUnworthy
Sorry, i forgot about viewport. Just use table.getParent().getParent() to get to the scroll pane. I will update the answer.Biotic
+1. Sorry, but Patrick gave me the correct answer earlier than you. So the tick goes to him.Unworthy

© 2022 - 2024 — McMap. All rights reserved.