Who owns the color of the edges of a Java Swing scrollable viewport?
I have a JScrollPane with a JPanel that implements Scrollable as its viewport view. When I set ScrollableTracksViewportHeight and ScrollableTracksViewportWidth to false, my panel stays at its preferred size. Good. The problem is that I can't figure out who owns the space around the scrollable panel. I feel like I've tried changing the background of every component and dialog, and nothing seems to do it. How can I change the ugly grey to a fun color like neon yellow?
Here's a screenshot of what I'm talking about:
Here's what I want:
And here's the code to duplicate the first image:
package components;
import java.awt.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class DialogWithScrollPane extends JFrame {
ScrollablePanel scrollablePanel;
public DialogWithScrollPane() {
super();
setResizable(true);
Container pane = getContentPane();
scrollablePanel = new ScrollablePanel();
final JScrollPane scrollPane = new JScrollPane();
scrollPane.setPreferredSize(new Dimension(300,300));
scrollPane.setViewportView(scrollablePanel);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
pane.add(scrollPane);
setMinimumSize(new Dimension(300, 300));
setVisible(true);
}
class ScrollablePanel extends JPanel implements Scrollable {
public ScrollablePanel() {
super();
setBackground(Color.red);
setPreferredSize(new Dimension(200, 100));
}
public Dimension getPreferredScrollableViewportSize() {
return getPreferredSize();
}
public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
return 0;
}
public int getScrollableUnitIncrement(Rectangle visibleRect, 开发者_JS百科int orientation, int direction) {
return 0;
}
public boolean getScrollableTracksViewportHeight() {
return false;
}
public boolean getScrollableTracksViewportWidth() {
return false;
}
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new DialogWithScrollPane();
}
});
}
}
scrollPane.getViewport().setBackground(Color.yellow);
It all belongs to the JViewport
, which you can paint any desired color, as shown here.
精彩评论