I have to change JScrollPane
to something like JLabel
. I used this code here:
myJScroll.setBorder(null);
myJScroll.setEnabled(false);
myJScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
myJScroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
But with setEnable(false)
, it has another color that covers this pane.
How can I change this color to another color?
A JScrollPane
contains a Viewport
component that is opaque by default. You can set a custom background color for the Viewport
:
myJScroll.getViewport().setBackground(Color.BLUE);
or make the viewport non-opaque:
myJScroll.getViewport().setOpaque(false);
and depending on your needs, you may also want to make the JScrollPane
non-opaque (to get the true background color of the parent component of the scrollpane:
myJScroll.setOpaque(false);
Also keep in mind the opacity and default background color of the component that is being added to the viewport.
For instance, try this as a test case:
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class ScrollPaneTest extends JPanel
{
private static final long serialVersionUID = 1L;
public ScrollPaneTest()
{
super(new BorderLayout());
JPanel myScrolledComponent = new JPanel(new BorderLayout());
myScrolledComponent.setBackground(Color.YELLOW);
myScrolledComponent.setOpaque(false);
myScrolledComponent.add(new JLabel("Some text"), BorderLayout.CENTER);
JScrollPane myJScroll = new JScrollPane(myScrolledComponent);
myJScroll.setBackground(Color.CYAN);
myJScroll.setOpaque(false);
myJScroll.setBorder(null);
myJScroll.setEnabled(false);
myJScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
myJScroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
myJScroll.getViewport().setBackground(Color.MAGENTA);
myJScroll.getViewport().setOpaque(false);
super.add(myJScroll, BorderLayout.CENTER);
return;
}
@SuppressWarnings("deprecation")
public static void main(String[] args)
{
JFrame f = new JFrame();
f.setBackground(Color.BLUE);
ScrollPaneTest test = new ScrollPaneTest();
test.setBackground(Color.RED);
f.add(test);
f.pack();
f.show();
}
}
The above class will display the JLabel
text with a red background (transparent all the way to the ScrollPaneTest
component.
A second example based on your comment:
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class ScrollPaneTest2 extends JScrollPane
{
private static final long serialVersionUID = 1L;
public ScrollPaneTest2()
{
super();
super.setBackground(Color.CYAN);
super.getViewport().setBackground(Color.MAGENTA);
super.getViewport().setOpaque(false);
return;
}
@SuppressWarnings("deprecation")
public static void main(String[] args)
{
JFrame f = new JFrame();
f.setBackground(Color.BLUE);
ScrollPaneTest2 scrollPane = new ScrollPaneTest2();
scrollPane.setBackground(Color.RED);
f.add(scrollPane);
JPanel myScrolledComponent = new JPanel(new BorderLayout());
myScrolledComponent.setBackground(Color.YELLOW);
myScrolledComponent.setOpaque(false);
myScrolledComponent.add(new JLabel("Some text"), BorderLayout.CENTER);
scrollPane.getViewport().setView(myScrolledComponent);
scrollPane.setBorder(null);
scrollPane.setEnabled(false);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
f.pack();
f.show();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.