Java Swing: how does JToolbar change the look of a button?

When you add a JButton to a JToolbar, the button takes a specific look (not the same if you add it to a Jpanel). I created some component similar to JToolbar, and I would like the same behavior. Problem. I checked the JToolbar class to find specific code to change the appearance of added components (change the processing method of individual components or user interface delegates, etc.). I did not find anything! I do not understand how JToolbar works. Can someone explain to me how this works?

Thank you very much,

Herve Guillaume

+4
source share
2 answers

This is apparently handled by the update() method of the MetalButtonUI class. Here is the code from JDK5_07:

 public void update(Graphics g, JComponent c) { AbstractButton button = (AbstractButton)c; if ((c.getBackground() instanceof UIResource) && button.isContentAreaFilled() && c.isEnabled()) { ButtonModel model = button.getModel(); if (!MetalUtils.isToolBarButton(c)) { if (!model.isArmed() && !model.isPressed() && MetalUtils.drawGradient( c, g, "Button.gradient", 0, 0, c.getWidth(), c.getHeight(), true)) { paint(g, c); return; } } else if (model.isRollover() && MetalUtils.drawGradient( c, g, "Button.gradient", 0, 0, c.getWidth(), c.getHeight(), true)) { paint(g, c); return; } } super.update(g, c); } 

The isToolBarButton() method simply checks if the parent Container is a JToolBar, so I assume that one solution is to always add the JButton to the JToolBar and then add the toolbar to the real container.

Otherwise, I think you will need to write your own user interface and override the update () method.

+4
source

I think this is nothing but a shutdown button. if you make your button disabled, it will look like a unit in the toolBar (except for the color of the black text). To change the text color of a disabled button, you can override the default UIManager property. And to make the button work more likely, since in the toolBar add a mouseListener to it and change its enable status in the mouseEnter and Exit methods.

Example:

  JFrame frame = new JFrame("tool bar button demo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(150, 150); frame.setLayout(new FlowLayout()); JToolBar bar = new JToolBar(); bar.add(new JButton("A button")); frame.add(bar); // to make text black in disabled button. UIManager.getDefaults().put("Button.disabledText",Color.BLACK); JButton button = new JButton("A button"); button.setEnabled(false); // if you are setting true or not changing the roll over property // of toolBar then following listerner help to give similar effect button.addMouseListener(new MouseListener() { @Override public void mouseReleased(MouseEvent me) { } @Override public void mousePressed(MouseEvent me) { } @Override public void mouseExited(MouseEvent me) { ((JButton)me.getSource()).setEnabled(false); } @Override public void mouseEntered(MouseEvent me) { ((JButton)me.getSource()).setEnabled(true); } @Override public void mouseClicked(MouseEvent me) { } }); frame.add(button); frame.setVisible(true); 
+1
source

Source: https://habr.com/ru/post/1387554/


All Articles