I need to hide the arrow buttons of java.awt.Scrollbar(VERTICAL)
in an AWT application.
Does anyone know how this can be achieved?
I saw an example here, but the code just hides the buttons. The vacant space for the buttons still remains; it is not occupied by the scroll bar.
To be more exact, here is the screenshot of what I should achieve. I am not sure which direction to go about it.
Update : I was looking for a solution in AWT. But now I am open to suggestions in Swing as well.
Try this.. it replaces the regular buttons on the Vertical ScrollBar with buttons that are 0x0 in size.
It does limit your look and feel though :(
JScrollPane scroller = new JScrollPane(mainPane);
scroller.setPreferredSize(new Dimension(200,200));
// ... etc
scroller.getVerticalScrollBar().setUI(new BasicScrollBarUI()
{
@Override
protected JButton createDecreaseButton(int orientation) {
return createZeroButton();
}
@Override
protected JButton createIncreaseButton(int orientation) {
return createZeroButton();
}
private JButton createZeroButton() {
JButton jbutton = new JButton();
jbutton.setPreferredSize(new Dimension(0, 0));
jbutton.setMinimumSize(new Dimension(0, 0));
jbutton.setMaximumSize(new Dimension(0, 0));
return jbutton;
}
});
Update: sorry, this is a swing solution
Using Nimbus Look and Feel you can use this to remove the arrow buttons:
UIManager.getLookAndFeelDefaults().put(
"ScrollBar:\"ScrollBar.button\".size", 0);
UIManager.getLookAndFeelDefaults().put(
"ScrollBar.decrementButtonGap", 0);
UIManager.getLookAndFeelDefaults().put(
"ScrollBar.incrementButtonGap", 0);
Here is a full example:
public class ScrollDemo extends JFrame {
public ScrollDemo() {
String[] columnNames = {"Column"};
Object[][] data = {
{"A"},{"B"},{"C"},{"D"},{"E"},{"F"},
{"A"},{"B"},{"C"},{"D"},{"E"},{"F"},
{"A"},{"B"},{"C"},{"D"},{"E"},{"F"},
{"A"},{"B"},{"C"},{"D"},{"E"},{"F"},
{"A"},{"B"},{"C"},{"D"},{"E"},{"F"},
};
add(new JScrollPane(new JTable(data, columnNames)));
pack();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception e) {
// No Nimbus
}
UIManager.getLookAndFeelDefaults().put(
"ScrollBar:ScrollBarThumb[Enabled].backgroundPainter",
new FillPainter(new Color(127, 169, 191)));
UIManager.getLookAndFeelDefaults().put(
"ScrollBar:ScrollBarThumb[MouseOver].backgroundPainter",
new FillPainter(new Color(127, 169, 191)));
UIManager.getLookAndFeelDefaults().put(
"ScrollBar:ScrollBarTrack[Enabled].backgroundPainter",
new FillPainter(new Color(190, 212, 223)));
UIManager.getLookAndFeelDefaults().put(
"ScrollBar:\"ScrollBar.button\".size", 0);
UIManager.getLookAndFeelDefaults().put(
"ScrollBar.decrementButtonGap", 0);
UIManager.getLookAndFeelDefaults().put(
"ScrollBar.incrementButtonGap", 0);
new ScrollDemo();
}
});
}
}
Code for the Painter
used:
public class FillPainter implements Painter<JComponent> {
private final Color color;
public FillPainter(Color c) { color = c; }
@Override
public void paint(Graphics2D g, JComponent object, int width, int height) {
g.setColor(color);
g.fillRect(0, 0, width-1, height-1);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With