Hiding the focus box on Swing components [duplicate] - java

I`ve customized an jSlider facing a little problem-
After changing values, on my custom jSlider is a border shown. Sorry, I´ve made pictures but Iàm not allowed to post it. The not customized jSlider do not show such border after click on it.
Is there an Method i have to Override to get rid of this border? Thank you in advance!
Sorry for my english! Hope anyone can help me!
This is my Customized UI:
public class RedGreenSliderUI extends BasicSliderUI{
Image knobImage ;
private BasicStroke stroke = new BasicStroke(1f, BasicStroke.CAP_ROUND,
BasicStroke.JOIN_ROUND, 0f, new float[]{1f, 2f}, 0f);
private static final float[] fractions = {0.0f, 0.5f};
private static final Color[] fillColors = {
/* new Color(0x2687AE),*/
/* new Color(0x1658AE)*/
new Color(0xc8041b),
new Color(0xc8041b)
};
private static final Color[] backColors = {
new Color(0x04c814),
new Color(0x04c814)
};
private static final Paint hFillGradient = new LinearGradientPaint(0, 0, 0, 11,
fractions, fillColors, MultipleGradientPaint.CycleMethod.NO_CYCLE);
private static final Paint hBackGradient = new LinearGradientPaint(0, 0, 0, 11,
fractions, backColors, MultipleGradientPaint.CycleMethod.NO_CYCLE);
private static final Paint vFillGradient = new LinearGradientPaint(0, 0, 11, 0,
fractions, fillColors, MultipleGradientPaint.CycleMethod.NO_CYCLE);
private static final Paint vBackGradient = new LinearGradientPaint(0, 0, 11, 0,
fractions, backColors, MultipleGradientPaint.CycleMethod.NO_CYCLE);
private static final Stroke roundEndStroke = new BasicStroke(5,
BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND);
public RedGreenSliderUI( JSlider aSlider ) {
super( aSlider );
try {
this.knobImage = ImageIO.read(getClass().getResource("ringelblumensalbe- Updater-Grafiken/tongThumb01.png")); //.getClassLoader()
} catch ( IOException e ) {
e.printStackTrace();
}
}
public void paintThumb(Graphics g) {
g.drawImage( this.knobImage, thumbRect.x, thumbRect.y, 15, 30, null );
}
#Override
protected Dimension getThumbSize() {
return new Dimension(15, 30);
}
#Override
protected Color getHighlightColor() {
return new Color(98, 94, 0);
}
public void paintTrack(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
if (slider.getOrientation() == JSlider.HORIZONTAL) {
int cy = (trackRect.height / 2) - 2;
g.translate(trackRect.x, trackRect.y + cy);
g2.setStroke(roundEndStroke);
g2.setPaint(hBackGradient);
g2.drawLine(thumbRect.x, 2, trackRect.width, 2);
g2.setPaint(hFillGradient);
g2.drawLine(0, 2, thumbRect.x, 2);
g.translate(-trackRect.x, -(trackRect.y + cy));
} else {
int cx = (trackRect.width / 2) - 2;
g.translate(trackRect.x + cx, trackRect.y);
g2.setStroke(roundEndStroke);
g2.setPaint(vBackGradient);
g2.drawLine(2, 0, 2, thumbRect.y);
g2.setPaint(vFillGradient);
g2.drawLine(2, thumbRect.y, 2, trackRect.height);
g.translate(-(trackRect.x + cx), -trackRect.y);
}
}
EDIT: I`ve got it. The answer is to override getFocusColor().
#Override
protected Color getFocusColor(){
return new Color(255,255,255);
}

The focus rectangle is renderered by the slider's UI delegate, typically based on BasicSliderUI. Although I can't advocate defeating the focus indicator, you can try one of the following:
Use the UIManager to set the "Slider.focus" color to match the "Slider.background".
Override the paintFocus() method to do nothing; a related example is seen here.

Trashgod's answer works nicely. For noob's like me the syntax is;
UIManager.put("Slider.focus", UIManager.get("Slider.background"));

You can try using border:0px; property for particular class where border is shown.

Related

Weird issue updating a JLabel on JSlider change

Huy guys, I have a weird problem.
I'm kinda new to swing and java applications.
I'm trying to make a custom UI jslider that changes a jlabel text when you move the thumb.
My problem is: when I use the addChangeListener(), it creates a weird glitch when moving the thumb.
If I don't use it, it works perfectly fine.
How can I update the JLabel without using the change listener or how can I fix this graphic bug?
Most of this code comes from stackoverflow since I don't know much about painting components.
See pictures at the button to better understand the problem
Thanks!
The code in my jpanel
JSlider slider = new JSlider(SwingConstants.HORIZONTAL, 1, 10, 1) {
#Override
public void updateUI() {
setUI(new CustomSliderUI(this));
}
};
// If I comment this line the visual glitch is gone when moving the thumb, but the value doesnt update
slider.addChangeListener((event) -> RAM_LABEL.setText(slider.getValue() + " Gb"));
slider.setMinorTickSpacing(1);
slider.setMajorTickSpacing(10);
slider.setSnapToTicks(true);
slider.setBounds(96, 317, 300, 35);
slider.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
slider.setOpaque(false);
this.add(slider);
The custom slider ui class
private static class CustomSliderUI extends BasicSliderUI
{
private static final int TRACK_HEIGHT = 8;
private static final int TRACK_ARC = 5;
private static final Dimension THUMB_SIZE = new Dimension(22, 20);
private final RoundRectangle2D.Float trackShape = new RoundRectangle2D.Float();
private final Image knob;
public CustomSliderUI(final JSlider b)
{
super(b);
knob = Swinger.getResource("knob.png");
}
#Override
protected void calculateTrackRect() {
super.calculateTrackRect();
trackRect.y = trackRect.y + (trackRect.height - TRACK_HEIGHT) / 2;
trackRect.height = TRACK_HEIGHT;
trackShape.setRoundRect(trackRect.x, trackRect.y, trackRect.width, trackRect.height, TRACK_ARC, TRACK_ARC);
}
#Override
protected void calculateThumbLocation() {
super.calculateThumbLocation();
thumbRect.y = trackRect.y + (trackRect.height - thumbRect.height) / 2;
}
#Override
protected Dimension getThumbSize() {
return THUMB_SIZE;
}
#Override
public void paint(final Graphics g, final JComponent c) {
((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
super.paint(g, c);
}
#Override
public void paintTrack(final Graphics g) {
Graphics2D g2 = (Graphics2D) g;
Shape clip = g2.getClip();
boolean inverted = slider.getInverted();
// Paint shadow.
g2.setColor(new Color(170, 170 ,170));
g2.fill(trackShape);
// Paint track background.
g2.setColor(new Color(200, 200 ,200));
g2.setClip(trackShape);
trackShape.y += 1;
g2.fill(trackShape);
trackShape.y = trackRect.y;
g2.setClip(clip);
// Paint selected track.
boolean ltr = slider.getComponentOrientation().isLeftToRight();
if (ltr) inverted = !inverted;
int thumbPos = thumbRect.x + thumbRect.width / 2;
if (inverted) {
g2.clipRect(0, 0, thumbPos, slider.getHeight());
} else {
g2.clipRect(thumbPos, 0, slider.getWidth() - thumbPos, slider.getHeight());
}
g2.setColor(Swinger.getTransparentWhite(0));
g2.fill(trackShape);
g2.setClip(clip);
}
#Override
public void paintThumb(final Graphics g)
{
g.drawImage(knob, thumbRect.x, thumbRect.y, null);
}
#Override
public void paintFocus(final Graphics g) {}
}
Visual glitch when you are moving the cursor, as soon as you stop pressing the mouse it goes back to normal
Regular cursor / when I move it without the change listener
Solved. I went with drawing my own background it was simplier.
private static class CustomSliderUI extends BasicSliderUI
{
private static final Dimension THUMB_SIZE = new Dimension(22, 20);
private final Image thumb, background;
private final JSlider slider;
public CustomSliderUI(final JSlider b)
{
super(b);
slider = b;
thumb = Swinger.getResource("thumb.png");
background = Swinger.getResource("slider.png");
}
#Override
protected Dimension getThumbSize() {
return THUMB_SIZE;
}
#Override
public void paintTrack(final Graphics g) {
g.drawImage(background, 10, 11, 280, 10, null);
}
#Override
public void paintThumb(final Graphics g)
{
g.drawImage(thumb, thumbRect.x, thumbRect.y, null);
slider.repaint();
}
#Override
public void paintFocus(final Graphics g) {}
}
JSlider slider = new JSlider(SwingConstants.HORIZONTAL, 1, 10, 1) {
#Override
public void updateUI() {
setUI(new CustomSliderUI(this));
}
};
slider.addChangeListener((event) -> RAM_LABEL.setText(slider.getValue() + " Gb"));
slider.setBounds(96, 317, 300, 35);
slider.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
slider.setOpaque(false);
this.add(slider);

Redrawing a JLabel to get a Gradient Painted Text

I am trying to create a gradient paint for text that goes from the top of the word to the bottom, not from left to right. I was actually able to achieve this from the help of this link here. They took the shape of the text, and painted it on the panel. I simply edited their code and was able to apply the affect I am looking for. Here is what I edited their paint method to (where s is a Shape):
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.translate(100, 150);
Rectangle2D r = s.getBounds2D();
int x = (int) r.getX();
int y = (int) r.getY();
int h = (int) r.getHeight();
GradientPaint gp = new GradientPaint(x, y, Color.MAGENTA, x, h, Color.ORANGE);
g2.setPaint(gp);
g2.fill(s);
}
This worked, but this approach is overriding a paintComponent method of a JPanel. I am trying to recreate this by a new GradientLabel Class that extends JLabel. The issue I am having is that the g2d.fill(s) method is drawing the shape somewhere above the label, seemingly out of reach. I don't understand why it is doing this. Perhaps its from casting Graphics2D g.create();? I have had to add the g2.translate(x,y) method to pull the shape down into a viewable location.
I guess I have 2 questions.
Why doesn't the g2.fill(s) draw the shape over the text that was drawn by the JLabel super method? Could this be because of my layout manager?
Is this approach even the way I should go? Is there an easier way to apply a vertical paint gradient to text?
Here is the minimal code for testing:
public class test extends JFrame {
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
test frame = new test();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public test() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 516, 360);
contentPane = new JPanel();
contentPane.setBorder(null);
setContentPane(contentPane);
GridBagLayout gbl_contentPane = new GridBagLayout();
gbl_contentPane.columnWidths = new int[]{0, 0};
gbl_contentPane.rowHeights = new int[]{0, 100, 0};
gbl_contentPane.columnWeights = new double[]{1.0, Double.MIN_VALUE};
gbl_contentPane.rowWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
contentPane.setLayout(gbl_contentPane);
Component verticalStrut = Box.createVerticalStrut(20);
GridBagConstraints gbc_verticalStrut = new GridBagConstraints();
gbc_verticalStrut.insets = new Insets(0, 0, 5, 0);
gbc_verticalStrut.gridx = 0;
gbc_verticalStrut.gridy = 0;
contentPane.add(verticalStrut, gbc_verticalStrut);
GradientLabel lblTest = new GradientLabel("TEST");
lblTest.setHorizontalAlignment(SwingConstants.CENTER);
lblTest.setGradientColors(Color.GREEN, Color.WHITE);
lblTest.setFont(new Font("Tahoma", Font.PLAIN, 70));
GridBagConstraints gbc_lblTest = new GridBagConstraints();
gbc_lblTest.fill = GridBagConstraints.BOTH;
gbc_lblTest.gridx = 0;
gbc_lblTest.gridy = 1;
contentPane.add(lblTest, gbc_lblTest);
}
public class GradientLabel extends JLabel {
private Color c1;
private Color c2;
public GradientLabel(String text) {
setText(text);
this.setOpaque(false);
}
public void setGradientColors(Color c1, Color c2) {
this.c1 = c1;
this.c2 = c2;
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Font f = getFont();
GlyphVector v = f.createGlyphVector(getFontMetrics(f).getFontRenderContext(), getText());
Shape s = v.getOutline();
Rectangle2D r = s.getBounds2D();
int x = (int) r.getX();
int y = (int) r.getY();
int h = (int) r.getHeight();
int w = (int) r.getWidth();
//without this the shape is drawn almost out of view
g2d.translate(x, h);
g2d.drawRect(x, y, w, h);
//for some reason using only h as the second y doesn't show much of the second color.
//Subtracting 60 arbitrarily showed more of the second color in the gradient.
//Bonus points for an explanation on why that happens.
GradientPaint gp = new GradientPaint(x, y, c1, x, h - 60, c2);
g2d.setPaint(gp);
g2d.fill(s);
}
}
}
Well I was able to answer my own question! First time for everything I guess.
I tracked the code on the JLabel back to the class where it was actually being drawn on the screen to the BasicLabelUI Class. I studied and determined that if I could build a new class that extends this class, I could simply override the painting portion of this class. After testing and determining the best gradient that fully paints both colors from the top of the text to the bottom, this is what I came up with:
public class SBLabelUI extends BasicLabelUI {
#Override
protected void paintEnabledText(JLabel l, Graphics g, String s, int textX, int textY) {
if (l instanceof GradientLabel) {
GradientLabel gl = (GradientLabel) l;
Graphics2D g2d = (Graphics2D) g;
Font f = gl.getFont();
int h = gl.getFontMetrics(f).getHeight();
GradientPaint gp = new GradientPaint(textX, textY, gl.c2, textX, Math.abs(textY-h), gl.c1);
g2d.setPaint(gp);
g2d.drawString(s, textX, textY);
} else {
super.paintEnabledText(l, g, s, textX, textY);
}
}
}
And in my Constructor for the gradient Label, i simply set the UI:
public GradientLabel(String text) {
setText(text);
this.setOpaque(false);
setUI(new SBLabelUI());
}
It works charmingly:

Getting input from a JOptionPane which is in a JDialog

For purposes that involve fancy looking components, I wrote a function that creates and shows a JOptionPane in a JDialog. Now, I need to get input from that JDialog but I can't. Is there a way that will let me get input from that JDialog without extending either JDialog or JOptionPane? (I also can't use UIManager to alter the appearance of JDialog that's why I'm having the problem in the first place)
public static final Color WHITE = Color.WHITE;
public static final Color RED = Color.RED;
public static final Color LIGHTER_RED = new Color(255, 0, 0, 100);
public static final Color LIGHT_RED = new Color(255, 0, 0, 160);
public static final Color DARK_BLUE = new Color(22, 44, 66);
public static final Font GEORGIA_BOLD_12 = new Font("Georgia", Font.BOLD, 12);
public static final BasicStroke STROKE_0 = new BasicStroke(0);
private static void recursivePaint(Container ct) {
for (Component c : ct.getComponents()) {
if (c instanceof Container) {
c.setBackground(DARK_BLUE);
c.setForeground(WHITE);
recursivePaint((Container) c);
}
}
}
public static int showInputDialog(final Container parent) {
int portNumber = 0;
final JLabel label = new JLabel("Enter an Open Port: ", SwingConstants.LEFT);
label.setOpaque(true);
label.setBackground(DARK_BLUE);
label.setForeground(WHITE);
final JButton button = new JButton("OK") {
private static final long serialVersionUID = -4808194362293478299L;
#Override
public int getWidth() {
return 51;
}
#Override
public int getHeight() {
return 26;
}
#Override
public void paintComponent(final Graphics g) {
final Graphics2D g2d = (Graphics2D) g;
g2d.clearRect(0, 0, getWidth(), getHeight());
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2d.setStroke(STROKE_0);
g2d.setColor(LIGHTER_RED);
if (this.getModel().isRollover()) {
g2d.setColor(LIGHT_RED);
}
if (this.getModel().isPressed()) {
g2d.setColor(RED);
}
g2d.fillRect(0, 0, getWidth(), getHeight());
g2d.setColor(RED);
g2d.drawRect(0, 0, getWidth(), getHeight());
g2d.setColor(WHITE);
g2d.setFont(GEORGIA_BOLD_12);
g2d.drawString("CONFIRM", 10, 18);
}
};
button.addActionListener(e -> {
//GET THE INPUT OF JOPTIONPANE TEXTFIELD
//portNumber = getTextOfJOptionPane();
SwingUtilities.getWindowAncestor((Component) e.getSource()).setVisible(false);
SwingUtilities.getWindowAncestor((Component) e.getSource()).dispose();
});
final JOptionPane optionPane = new JOptionPane(label, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_OPTION, null, new JButton[] {button}, button);
optionPane.setWantsInput(true);
optionPane.setOpaque(true);
optionPane.setBackground(DARK_BLUE);
optionPane.getInputValue();
recursivePaint(optionPane);
final JDialog d = optionPane.createDialog(parent, "Open port required!");
d.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
d.setContentPane(optionPane);
d.pack();
d.setLocationRelativeTo(parent);
d.setVisible(true);
return portNumber;
}
Thanks in advance.
JOptionPane on Windows
Since JOptionPane doesn't expose ALL the functionality you would need to replicate this, you're going to have to take more control.
The "appropriate" path would be to supply your own custom look and feel delegate, but that seems like a lot of extra work just so you can control the JTextField.
Instead, you could pass both the JLabel and JTextField to the OptionPane via the message parameter, contained in a single JPanel, for example...
public static final Color WHITE = Color.WHITE;
public static final Color RED = Color.RED;
public static final Color LIGHTER_RED = new Color(255, 0, 0, 100);
public static final Color LIGHT_RED = new Color(255, 0, 0, 160);
public static final Color DARK_BLUE = new Color(22, 44, 66);
public static final Font GEORGIA_BOLD_12 = new Font("Georgia", Font.BOLD, 12);
public static final BasicStroke STROKE_0 = new BasicStroke(0);
public static int showInputDialog(final Container parent) {
int portNumber = 0;
final JLabel label = new JLabel("Enter an Open Port: ", SwingConstants.LEFT);
label.setForeground(WHITE);
JPanel panel = new JPanel(new GridLayout(2, 1));
panel.setOpaque(true);
panel.setBackground(DARK_BLUE);
JTextField inputField = new JTextField(10);
panel.add(label);
panel.add(inputField);
final JButton button = new JButton("OK") {
private static final long serialVersionUID = -4808194362293478299L;
#Override
public int getWidth() {
return 51;
}
#Override
public int getHeight() {
return 26;
}
#Override
public void paintComponent(final Graphics g) {
final Graphics2D g2d = (Graphics2D) g;
g2d.clearRect(0, 0, getWidth(), getHeight());
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2d.setStroke(STROKE_0);
g2d.setColor(LIGHTER_RED);
if (this.getModel().isRollover()) {
g2d.setColor(LIGHT_RED);
}
if (this.getModel().isPressed()) {
g2d.setColor(RED);
}
g2d.fillRect(0, 0, getWidth(), getHeight());
g2d.setColor(RED);
g2d.drawRect(0, 0, getWidth(), getHeight());
g2d.setColor(WHITE);
g2d.setFont(GEORGIA_BOLD_12);
g2d.drawString("CONFIRM", 10, 18);
}
};
final JOptionPane optionPane = new JOptionPane(panel, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_OPTION, null, new JButton[]{button}, button);
button.addActionListener(e -> {
//GET THE INPUT OF JOPTIONPANE TEXTFIELD
optionPane.setInputValue(inputField.getText());
optionPane.setValue(JOptionPane.OK_OPTION);
});
optionPane.setOpaque(true);
optionPane.setBackground(DARK_BLUE);
final JDialog d = optionPane.createDialog(parent, "Open port required!");
d.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
d.setContentPane(optionPane);
d.pack();
d.setLocationRelativeTo(parent);
d.setVisible(true);
System.out.println(optionPane.getValue());
System.out.println(optionPane.getInputValue());
return portNumber;
}
So. In addition, I've made some additional changes. In the ActionListener, I've set the "value" of the operation to OK_OPTION and called setInputValue. It's important that you call setValue, otherwise the inputValue is not applied, because it thinks you've "cancelled" the dialog.
I don't what you're dialog looks like on your system, but this is what it looks like on mine....
This is why it's ill-advised to override things like getHeight and getWidth

java JDialog draw called double

I've got a problem which makes me go crazy. I call a Method via this line of code:
private void btnGetFolderMouseClicked(MouseEvent e) {
MessageController.showAlert(AlertBox.AlertType.OK);
}
and this method is called:
public class MessageController {
public static void showAlert(AlertBox.AlertType alertType) {
AlertBox alertBox = new AlertBox("Test12345678910dfkjsdgmdgbu<xdfg<bdxgj ghfhftz rgdx", alertType);
alertBox.getRootPane().setOpaque(false);
//alertBox.getContentPane ().setBackground (new Color(0, 0, 0, 0));
//alertBox.setBackground (new Color(0, 0, 0, 0));
alertBox.setVisible(true);
// BDialog dialog = new BDialog();
// dialog.setVisible(true);
System.out.println("Nach Box");
//alertBox.invalidate();
}
}
And here is my JDialog class:
public class AlertBox extends JDialog {
private ImageIcon downSide = new ImageIcon(getClass().getResource("/AlertBox/Down_Side.png"));
private ImageIcon leftDownCorner = new ImageIcon(getClass().getResource("/AlertBox/Left_Down_Corner.png"));
private ImageIcon leftSide = new ImageIcon(getClass().getResource("/AlertBox/Left_Side.png"));
private ImageIcon leftUpCorner = new ImageIcon(getClass().getResource("/AlertBox/Left_Up_Corner.png"));
private ImageIcon rightDownCorner = new ImageIcon(getClass().getResource("/AlertBox/Right_Down_Corner.png"));
private ImageIcon rightSide = new ImageIcon(getClass().getResource("/AlertBox/Right_Side.png"));
private ImageIcon rightUpCorner = new ImageIcon(getClass().getResource("/AlertBox/Right_Up_Corner.png"));
private ImageIcon upSide = new ImageIcon(getClass().getResource("/AlertBox/Up_Side.png"));
private String text;
private Rectangle2D fontSize;
private Font font = new Font("Arial", Font.BOLD, 13);
private ImagedButton btnOK;
public enum AlertType{
OK,
Warning,
Error
}
private AlertType alertType;
public AlertBox(String text, AlertType alertType){
// Set JDialog properties
super(new JFrame(), true);
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
setUndecorated(true);
setLocationRelativeTo(null);
this.text = text;
this.alertType = alertType;
btnOK = new ImagedButton("OK");
btnOK.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("Button Clicked");
exitAlertBox();
}
});
//this.add(btnOK);
}
#Override
public void paint(Graphics g) {
// Create Graphics2D Object
Graphics2D g2d = (Graphics2D) g.create();
// Get the Size
fontSize = StringMetrics.getBounds(g2d, font, text);
// set the fontSize
int width = (int) fontSize.getWidth() + 5 + leftSide.getIconWidth() + 5 + rightSide.getIconWidth();
int height = leftUpCorner.getIconHeight() + 5 + (int)fontSize.getHeight() + 5 + btnOK.getHeight() + 5 + leftDownCorner.getIconHeight();
this.setSize(width, height);
//this.replaceButton(width, height);
// Draw Left Upper Corner
System.out.println(leftDownCorner.getIconWidth() + " ; " + leftDownCorner.getIconHeight());
g.drawImage(leftUpCorner.getImage(), 0, 0, this);
g.drawImage(new ImageIcon(getClass().getResource("/Buttons/Button_Down.png")).getImage(), 21, 20, this);
// Draw Upper Side
//g.drawImage(upSide.getImage(), leftUpCorner.getIconWidth(), 0, 5 + (int) fontSize.getWidth() + 5, upSide.getIconHeight(), this);
// Draw Right Upper Corner
//g.drawImage(rightUpCorner.getImage(), this.getWidth() - rightUpCorner.getIconWidth(), 0, this);
// Draw Left Side
//g.drawImage(leftSide.getImage(), 0, leftUpCorner.getIconHeight(), leftSide.getIconWidth(), 5 + (int)fontSize.getHeight() + 5, this);
// Draw Right Side
//g.drawImage(rightSide.getImage(), width - rightSide.getIconWidth(), rightUpCorner.getIconHeight(), rightSide.getIconWidth(), 5 + upSide.getIconHeight() + 5, this);
// Draw Left Downer Corner
//g.drawImage(leftDownCorner.getImage(), 0, height - leftDownCorner.getIconHeight(), this);
// Draw Right Downer Corner
//g.drawImage(rightDownCorner.getImage(), this.getWidth() - rightUpCorner.getIconWidth(), height - rightDownCorner.getIconHeight(), this);
// Draw Downer Side
//g.drawImage(downSide.getImage(), leftDownCorner.getIconWidth(), height - downSide.getIconHeight(), 5 + (int) fontSize.getWidth() + 5, downSide.getIconHeight(), this);
// Draw the text
//g2d.drawString(text, 10, 10);
//g2d.drawString(text, leftUpCorner.getIconWidth() + 5, (int)fontSize.getHeight() + 8);
if (this.alertType == AlertType.OK){
}
}
private void replaceButton(int width, int height){
// paint OK-Button
int testWidth = width - (btnOK.getWidth() / 2);
int testHeight = height - btnOK.getHeight() - 5;
btnOK.setBounds(testWidth, testHeight, btnOK.getWidth(), btnOK.getHeight());
}
private void exitAlertBox(){
this.dispose();
}
}
The problem is, that the window should (at this version of code) display 2 Images. Well it does.. sometimes.. But most of the runs it doesn't.
When I debug my code, it seems like the 'paint' Method is called twice, but i don't find the line where this happens.
Did i do anything wrong?
Thanks for your help :)

Creating a custom button in Java with JButton

I am trying to create a button that has a custom shape (hexagon), but otherwise acts like a normal JButton would (that is, works with an ActionListener).
I have created a class that extends AbstractButton, but it doesn't seem to be sending events to the ActionListener when I click it. If I change the class to extend JButton it works perfectly, but it screws up the way the button is displayed. I'm assuming that there is a method that I need to override to get it to fire events, but I can't figure out what it is.
If you want to create a CustomButtonUI then you have to look
deepest to the BasicXxxUI.
overide List of Colors from JButton
Note there isn't paintComponent(). It's wrong, just use the paint() method,
Below is just a simple example if that is possible (for Metal JButton). Note just for Metal LaF, I'm so lazy and there isn't something about override paintText, paintIcon, paintFocus, paintBorder (for all funcionalities you have to check available methods from BasicButtonUI), and something I put to the ButtonModel, just for my enjoyment.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.AbstractBorder;
import javax.swing.border.Border;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.metal.MetalButtonUI;
public class TextAreaInButton {
private JFrame frame = new JFrame("sssssssss");
private JButton tip1Null = new JButton(" test button ");
public TextAreaInButton() {
Border line, raisedbevel, loweredbevel, title, empty;
line = BorderFactory.createLineBorder(Color.black);
raisedbevel = BorderFactory.createRaisedBevelBorder();
loweredbevel = BorderFactory.createLoweredBevelBorder();
title = BorderFactory.createTitledBorder("");
empty = BorderFactory.createEmptyBorder(1, 1, 1, 1);
final Border compound;
Color crl = (Color.blue);
compound = BorderFactory.createCompoundBorder(empty, new OldRoundedBorderLine(crl));
Color crl1 = (Color.red);
final Border compound1;
compound1 = BorderFactory.createCompoundBorder(empty, new OldRoundedBorderLine(crl1));
Color crl2 = (Color.black);
final Border compound2;
compound2 = BorderFactory.createCompoundBorder(empty, new OldRoundedBorderLine(crl2));
tip1Null.setFont(new Font("Serif", Font.BOLD, 14));
tip1Null.setForeground(Color.darkGray);
tip1Null.setPreferredSize(new Dimension(50, 30));
tip1Null.addActionListener(new java.awt.event.ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
}
});
tip1Null.setBorderPainted(true);
tip1Null.setFocusPainted(false);
tip1Null.setBorder(compound);
tip1Null.setHorizontalTextPosition(SwingConstants.CENTER);
tip1Null.setVerticalTextPosition(SwingConstants.BOTTOM);
tip1Null.setUI(new ModifButtonUI());
tip1Null.getModel().addChangeListener(new ChangeListener() {
#Override
public void stateChanged(ChangeEvent e) {
ButtonModel model = (ButtonModel) e.getSource();
if (model.isRollover()) {
tip1Null.setBorder(compound1);
} else {
tip1Null.setBorder(compound);
}
if (model.isPressed()) {
tip1Null.setBorder(compound2);
}
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(tip1Null, BorderLayout.CENTER);
frame.setLocation(150, 150);
frame.setPreferredSize(new Dimension(310, 75));
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
}
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
TextAreaInButton taib = new TextAreaInButton();
}
});
}
}
class OldRoundedBorderLine extends AbstractBorder {
private final static int MARGIN = 5;
private static final long serialVersionUID = 1L;
private Color color;
OldRoundedBorderLine(Color clr) {
color = clr;
}
public void setColor(Color clr) {
color = clr;
}
#Override
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
((Graphics2D) g).setRenderingHint(
RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(color);
g.drawRoundRect(x, y, width, height, MARGIN, MARGIN);
}
#Override
public Insets getBorderInsets(Component c) {
return new Insets(MARGIN, MARGIN, MARGIN, MARGIN);
}
#Override
public Insets getBorderInsets(Component c, Insets insets) {
insets.left = MARGIN;
insets.top = MARGIN;
insets.right = MARGIN;
insets.bottom = MARGIN;
return insets;
}
}
class ModifButtonUI extends MetalButtonUI {
private static final ModifButtonUI buttonUI = new ModifButtonUI();
ModifButtonUI() {
}
public static ComponentUI createUI(JComponent c) {
return new ModifButtonUI();
}
#Override
public void paint(Graphics g, JComponent c) {
final Color color1 = new Color(230, 255, 255, 0);
final Color color2 = new Color(255, 230, 255, 64);
final Color alphaColor = new Color(200, 200, 230, 64);
final Color color3 = new Color(
alphaColor.getRed(), alphaColor.getGreen(), alphaColor.getBlue(), 0);
final Color color4 = new Color(
alphaColor.getRed(), alphaColor.getGreen(), alphaColor.getBlue(), 64);
super.paint(g, c);
Graphics2D g2D = (Graphics2D) g;
GradientPaint gradient1 = new GradientPaint(
0.0F, (float) c.getHeight() / (float) 2, color1, 0.0F, 0.0F, color2);
Rectangle rec1 = new Rectangle(0, 0, c.getWidth(), c.getHeight() / 2);
g2D.setPaint(gradient1);
g2D.fill(rec1);
GradientPaint gradient2 = new GradientPaint(
0.0F, (float) c.getHeight() / (float) 2, color3, 0.0F, c.getHeight(), color4);
Rectangle rec2 = new Rectangle(0, c.getHeight() / 2, c.getWidth(), c.getHeight());
g2D.setPaint(gradient2);
g2D.fill(rec2);
}
#Override
public void paintButtonPressed(Graphics g, AbstractButton b) {
paintText(g, b, b.getBounds(), b.getText());
g.setColor(Color.red.brighter());
g.fillRect(0, 0, b.getSize().width, b.getSize().height);
}
public void paintBorder(Graphics g) {
}
#Override
protected void paintFocus(Graphics g, AbstractButton b,
Rectangle viewRect, Rectangle textRect, Rectangle iconRect) {
}
}
You will have to extend JButton class not AbstractButton. Try the following things and you will get idea.
The first move is to subclass JButton.
Then, in your subclass, start by redefining the paintComponent(Graphics) method. If you want any changes.
Then, override paintBorder(Graphics) to give it a shape of hexagon.
I know this question has been answered, but you might want to look at using the built-in methods, and using images to draw your button in different states.
Here is a bit of code I used to generate a custom button.
BufferedImage startButton = ImageIO.read(getClass().getResource("/icons/standard/buttons/start_backup.png"));
BufferedImage startButtonHover = ImageIO.read(getClass().getResource("/icons/standard/buttons/start_backup_hover.png"));
BufferedImage startButtonActive = ImageIO.read(getClass().getResource("/icons/standard/buttons/start_backup_active.png"));
JButton startBackupButton = new JButton(new ImageIcon(startButton));
startBackupButton.setRolloverIcon(new ImageIcon(startButtonHover));
startBackupButton.setPressedIcon(new ImageIcon(startButtonActive));
startBackupButton.setBorder(BorderFactory.createEmptyBorder());
startBackupButton.setContentAreaFilled(false);
startBackupButton.setFocusable(false);
You can then add an action listener to it as normal.
try a Jlabel and use an image for any shape!!
JLabel lbl = new JLabel("");
lbl.setIcon(new ImageIcon("shape.png"));
lbl.setBounds(548, 11, 66, 20);
contentPane.add(lbl);
lbl.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent arg0) {
System.exit(0);
}
});

Categories