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 cod
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 {
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);
}
}