I'd like to add a non-scrolling element, always visible element (a "pinned" element) in a JScrollPane of a Swing GUI. Taking this example code:
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame();
JEditorPane editorPane = new JEditorPane();
editorPane.setText("""
d
d
d
d
d
d
d
d
d
d
d
d
d
d
d
d
d
d
d
d
d
""");
JScrollPane scroll = new JScrollPane(editorPane);
editorPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
editorPane.add(new JLabel("this shouldn't scroll!"));
frame.getContentPane().add(scroll);
frame.setSize(600, 400);
frame.setVisible(true);
}
}
I need a way to:
- Make sure that the "this shouldn't scroll!" JLabel doesn't move when the scroll pane scrolls.
- Pin the "this shouldn't scroll!" JLabel to the right side of the panel (currently, it becomes hidden with a resize of the panel. I'd like it to move left when the right side of the panel is pushed smaller, to make sure it's always on screen).
I've been able to make an awful implementation of this using swing's glass pane, but it means a lot more manual updating for me, and position always being the same, no matter how the swing panel is resized.
Thank you for any help!


As a personal preference, I'd probably use a compound layout, simular to this example as it gives you more control over the the component been rendered (ie much easier to deal with live components like buttons) and it's position.
You "could" make use of the
JLayerAPI, for example...See How to Decorate Components with the JLayer Class for more details.