Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>It changes the color the way I want it, but hides the arrowbuttons.</p> </blockquote> <p>Here is the problem:</p> <pre><code>private JButton b = new JButton() { @Override public Dimension getPreferredSize() { return new Dimension(0, 0); // why (0,0) ??? } }; </code></pre> <p>In your code <code>b</code> button is the responsible for painting the arrows through <code>createDecreaseButton</code> and <code>createIncreaseButton</code> methods. If its preferred size is <code>(0,0)</code> then logically it won't be visible.</p> <blockquote> <p>What I want is to make them visible again and change them with a custom image.</p> </blockquote> <p>You need to modify <code>createDecreaseButton</code> and <code>createIncreaseButton</code> to make them return a new <code>JButton</code> with the desired icon.</p> <p><strong>Update</strong></p> <blockquote> <p>I already tried playing with the prefferedsize (making them the same size as the custom image), but the custom arrowimages are still not showing up. Im clueless</p> </blockquote> <p>Look at this working example. <code>MyScrollbarUI</code> extends from <a href="http://docs.oracle.com/javase/7/docs/api/javax/swing/plaf/basic/BasicScrollBarUI.html" rel="nofollow noreferrer">BasicScrollBarUI</a> just as your class does. You'll see the key is overrding the button's <code>getPreferredSize()</code> method and setting the appropriate icon as needed.</p> <p>In this regard I should say <a href="http://docs.oracle.com/javase/7/docs/api/javax/swing/plaf/basic/BasicScrollBarUI.html#createDecreaseButton%28int%29" rel="nofollow noreferrer">BasicScrollBarUI.createDecreaseButton(int orientation)</a> and <a href="http://docs.oracle.com/javase/7/docs/api/javax/swing/plaf/basic/BasicScrollBarUI.html#createIncreaseButton%28int%29" rel="nofollow noreferrer">BasicScrollBarUI.createIncreaseButton(int orientation)</a> methods are poorly documented (there's no javadoc). But if you dive into this class using an IDE then you'll see <code>orientation</code> paramenter can take one of these values: <code>SwingConstants.NORTH, SwingConstants.SOUTH, SwingConstants.EAST, SwingConstants.WEST</code>. Keep this in mind when look at <code>getAppropriateIcon(int orientation)</code> method.</p> <p>These are used icons: <img src="https://i.stack.imgur.com/D960s.png" alt="Up arrow"><img src="https://i.stack.imgur.com/13euX.png" alt="Down arrow"><img src="https://i.stack.imgur.com/Unv09.png" alt="Left arrow"><img src="https://i.stack.imgur.com/zGjyC.png" alt="Right arrow"></p> <pre><code>import java.awt.Dimension; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.SwingConstants; import javax.swing.SwingUtilities; import javax.swing.plaf.basic.BasicScrollBarUI; public class Demo { private void initGUI(){ JScrollPane scrollPane = new JScrollPane(new JTextArea(10, 20)); scrollPane.getHorizontalScrollBar().setUI(new MyScrollbarUI()); scrollPane.getVerticalScrollBar().setUI(new MyScrollbarUI()); JFrame frame = new JFrame("Demo"); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frame.getContentPane().add(scrollPane); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new Demo().initGUI(); } }); } class MyScrollbarUI extends BasicScrollBarUI { private ImageIcon downArrow, upArrow, leftArrow, rightArrow; public MyScrollbarUI(){ try { upArrow = new ImageIcon(new java.net.URL("http://icons.iconarchive.com/icons/oxygen-icons.org/oxygen/16/Actions-arrow-up-icon.png")); downArrow = new ImageIcon(new java.net.URL("http://icons.iconarchive.com/icons/oxygen-icons.org/oxygen/16/Actions-arrow-down-icon.png")); rightArrow = new ImageIcon(new java.net.URL("http://icons.iconarchive.com/icons/oxygen-icons.org/oxygen/16/Actions-arrow-right-icon.png")); leftArrow = new ImageIcon(new java.net.URL("http://icons.iconarchive.com/icons/oxygen-icons.org/oxygen/16/Actions-arrow-left-icon.png")); } catch (java.net.MalformedURLException ex) { ex.printStackTrace(); } } @Override protected JButton createDecreaseButton(int orientation) { JButton decreaseButton = new JButton(getAppropriateIcon(orientation)){ @Override public Dimension getPreferredSize() { return new Dimension(22, 22); } }; return decreaseButton; } @Override protected JButton createIncreaseButton(int orientation) { JButton increaseButton = new JButton(getAppropriateIcon(orientation)){ @Override public Dimension getPreferredSize() { return new Dimension(22, 22); } }; return increaseButton; } private ImageIcon getAppropriateIcon(int orientation){ switch(orientation){ case SwingConstants.SOUTH: return downArrow; case SwingConstants.NORTH: return upArrow; case SwingConstants.EAST: return rightArrow; default: return leftArrow; } } } } </code></pre> <p><strong>Screenshot</strong></p> <p><img src="https://i.stack.imgur.com/arFbf.jpg" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload