Remove border from JButton - java

I would have a problem with the edges of my jbutton. In practice, in the code below I inserted a button that should not have edges but instead appear as in the photo below.
JButton btnRes = new JButton();
btnRes.setBorderPainted(false);
btnRes.setContentAreaFilled(false);
btnRes.setOpaque(false);
btnRes.setBorder(null);
btnRes.setIcon(new ImageIcon(Main.class.getResource(image1)));
btnRes.setPressedIcon(new ImageIcon(Main.class.getResource(image2)));
btnRes.setRolloverIcon(new ImageIcon(Main.class.getResource(image3)));
btnRes.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//TODO
}
});
btnRes.setBounds(496, 342, 138, 48);
frame.getContentPane().add(btnRes);
and this is the result:
Image of this JButton
The border is however visible, how I can fix this?

You see the border that is added to the button because it is selected. Try:
btnRes.setFocusPainted(false);

Related

Align icon of JButton with JButton

I am working with a JButton and wanted to add an icon image to it.
When I went to add an image the image has an offset to the left for some reason.
The image is exactly the same size as the button but it is slightly moved to the left for some reason. (The offset is about 13 pixels)
Here is the code from the button:
private JButton getBtnReset() {
if (btnReset == null) {
btnReset = new JButton("Reset");
btnReset.setIcon(new ImageIcon(FrontEnd.class.getResource("/resources/Reset1.png")));
btnReset.setFont(new Font("OptimusPrinceps", Font.BOLD, 17));
btnReset.addActionListener(new BtnResetActionListener());
btnReset.setBounds(400, 11, 167, 69);
}
return btnReset;
}

How to add a icon in JButton

I'm trying to create a icon in a button in JButton, the button should look like this
But when i add this in my code it looks like this
Here is the button portion of my code
private JButton backButton = new JButton();
backButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
actionBack();
}
});
backButton.setIcon(new ImageIcon(Hello.class.getResource("/239706184.png")));
backButton.setEnabled(false);
buttonPanel.add(backButton);
Your button is dark because you set eneable to false
remove this line:
backButton.setEnabled(false);
If you set backButton.setEnabled(false); then it will be showing in dark state.
Try it set to true.

How do I remove this "selected" border off my button?

I am trying to create a undecorated JFrame, but I am having some issues with my Closing button, It has this ugly "selected" border around it, is there any way of removing it? (Top right corner of image)
This is what I did to remove all borders and backgrounds:
JButton btnX = new JButton("");
btnX.setIcon(new ImageIcon(GameHubMain.class.getResource("/Resources/Close-icon.png")));
btnX.setForeground(Color.WHITE);
btnX.setOpaque(false);
btnX.setContentAreaFilled(false);
btnX.setBorderPainted(false);
btnX.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
frame.dispose();
}
});
Maybe try this ?
Border emptyBorder = BorderFactory.createEmptyBorder();
btnX.setBorder(emptyBorder);
You should also try adding:
btnX.setFocusPainted(false);
btnX.setMargin(new Insets(0, 0, 0, 0));
Add following line in your code and check
btnX.setBorder(BorderFactory.createEmptyBorder());

How can I remove JButton from JFrame?

I want to remove JButton when user click JButton.
I know that I should use remove method, but it did not work.
How can I do this?
Here is my code:
class Game implements ActionListener {
JFrame gameFrame;
JButton tmpButton;
JLabel tmpLabel1, tmpLabel2, tmpLabel3, tmpLabel4;
public void actionPerformed(ActionEvent e) {
gameFrame.remove(tmpLabel1);
gameFrame.getContentPane().validate();
return;
}
Game(String title) {
gameFrame = new JFrame(title);
gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gameFrame.setBounds(100, 100, 300, 500);
gameFrame.setResizable(false);
gameFrame.getContentPane().setLayout(null);
tmpLabel4 = new JLabel(new ImageIcon("./images/bomber.jpg"));
tmpLabel4.setSize(200, 200);
tmpLabel4.setLocation(50, 100);
tmpButton = new JButton("Play");
tmpButton.setSize(100, 50);
tmpButton.setLocation(100, 350);
tmpButton.addActionListener(this);
gameFrame.getContentPane().add(tmpLabel4);
gameFrame.getContentPane().add(tmpButton);
gameFrame.setVisible(true);
}
}
If hiding the button instead of removing works for your code then you can use:
public void actionPerformed(ActionEvent event){
tmpButton.setVisible(false);
}
for the button.But the button is just hidden not removed.
The simplest solution might be to...
Attach an ActionListener to the button, see How to Use Buttons, Check Boxes, and Radio Buttons and How to Write an Action Listeners for more details
When the ActionListener is clicked, extract the source of the event, JButton buttonThatWasClicked = (JButton)actionEvent.getSource()
Remove it from it's parent...
For example...
Container parent = buttonThatWasClicked.getParent();
parent.remove(buttonThatWasClicked);
parent.revaidate();
parent.repaint();
As some ideas...
First of all in your actionPerformed method you need to check that the button is clicked or not. And if the button is clicked, remove it. Here's how :
if(e.getSource() == tmpButton){
gameFrame.getContentPane().remove(tmpButton);
}
add this to your actionPerformed Method
don't add your button to jframe but add each component you want!
public void actionPerformed(ActionEvent event)
{
//gameFrame.getContentPane().add(tmpButton); -=> "Commented Area"
gameFrame.getContentPane().validate();
}
or hide your button like this
public void actionPerformed(ActionEvent event)
{
tmpButton.setVisible(false);
}

How to place an image on the screen from an event?

I want to add an image on the screen when an event occurs. In my case, this would be from a button being clicked. I attempted to solve this myself, but the image isn't appearing. I don't know what is wrong.
My code:
JButton button2 = new JButton("+");
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
//Execute when button is pressed
System.out.println("button #2 working");
label2 = new JLabel(new ImageIcon(this.getClass().getResource("50.png")));
label2.setLocation(10, 60);
label2.setSize(300, 532);
add(label2);
//? Not working
}
});
button2.setSize(50, 40);
button2.setFont(new Font("Arial", 1, 20));
button2.setLocation(110, 592);
button2.setBackground(Color.ORANGE);
content.add(button2);
I actually have done something similar to what you are trying to achieve. However, I achieved this by using a different method instead. You could for example create a class MyPanel which extends JPanel and override the paintComponent() method. In the paintComponent() method you can call g.drawImage(yourImage, 0, 0,null); to add the image to your panel. Then it is a matter of creating an instance of MyPanel and adding it wherever you want on the GUI. Also, do not forget to call repaint() after you have clicked the button or you will not see the change.

Categories