I have a class that gets a JFrame as a parameter. I want to get a certain component from it and do some operations with it.
Something like this:

class A {
    A (JFrame frame){
        a_certain_Component jScrollPane = (a_certain_Component) frame.getThatComponent();
        jScrollPane.setViewPort(new JTextPane());
    }
}

Dani AI

Generated

Quick clarification that complements the thread: was correct that scanning the content pane for the scroll pane can work, and resolved the immediate problem. Two common pitfalls worth spelling out for future readers are (1) modifying Swing components off the Event Dispatch Thread (EDT) and (2) using the wrong API or layout calls so the viewport replacement never becomes visible. The Swing-specific calls to schedule work on the EDT and to revalidate the layout are the reliable way to make runtime changes stick.

A small, reusable helper to find a component of a given type inside a container (works for nested containers) avoids brittle index loops:

private static <T extends Component> T findComponent(Container root, Class<T> cls) {
    for (Component c : root.getComponents()) {
        if (cls.isInstance(c)) return cls.cast(c);
        if (c instanceof Container) {
            T found = findComponent((Container) c, cls);
            if (found != null) return found;
        }
    }
    return null;
}

When replacing the viewport view, run the change on the EDT and refresh the layout:

JScrollPane sp = findComponent(frame.getContentPane(), JScrollPane.class);
if (sp != null) {
    SwingUtilities.invokeLater(() -> {
        sp.getViewport().setView(new JTextPane());
        sp.revalidate();    // preferred in Swing
        sp.repaint();
    });
}

Design notes and troubleshooting: prefer passing a reference to the JScrollPane (or exposing a getter) instead of searching the frame at runtime. If identifying components dynamically, setName(...) and search by name. Use revalidate() (Swing) rather than validate() (AWT), and avoid calling plain add(...) on a JScrollPane when the intention is to change the viewport view. These practices make the behavior predictable and avoid the “it’s not showing” symptom that originally appeared in this thread.

Recommended Answers

All 3 Replies

getComponents and instanceof

Hey thanks. But i still can't add something to the scroll pane.
my code

Component[] comps = theFrame.getContentPane().getComponents();
        int i =-1;
        do{i++; }while(!(comps[i] instanceof JScrollPane) );
        JScrollPane scrollPane = (JScrollPane) comps[i];
        JTextArea ta = new JTextArea ("texttttt");
        ta.setText("testsetsetsetsetset");
        scrollPane.add(ta);
        scrollPane.setViewportView(ta);

Can you please tell me what i'm missing?

[EDIT]
Nevermind. I fixed it. :)

Call validate and/or repaint on the JScrollPane.

Edit: Didn't see the edit.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.