How coud I create these kind of Button using Java Swing? - java

I want to create button without any borders or shadow, but an icon instead using java swing component. How can I accomplish this?

Real Button
JButton btnNewButton = new JButton("");
btnNewButton.setContentAreaFilled(false);
btnNewButton.setBorderPainted(false);
btnNewButton.setBorder(null);
btnNewButton.setIcon(new ImageIcon(path));
This will give you a real button without any borders around the given image to work with. Note that in this state the button doesn't have a "click animation" anymore. For such an animation you could use the .setSelectedIcon(selectedIcon);
Clickable Image
ImageIcon img = new ImageIcon(path);
JLabel button = new JLabel(img);
button.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
//Set pressed or something else
}
});
But this one just provide you a clickable Image and should only be used when a clickable Image without any other intentions is needed.
Note that this way is just a workaround.

Related

Changing to color of a JToggleButton only when the button is selected, afterwards it will go back to the default color

This may end up being noted as a formerly asked question, however the other questions have yet to answer the question that I have asked properly, to what I need them to do.
I want it to be that whenever I click the ToggleButton it will set the color of the ToggleButton to black (in this case), and then when I unclick the button it will return to the default color.
Here is the code for the individual button, its possible Ill have to create a new class (I am using Eclipse) but if anyone could help id be extremely thankful. (I also have Jigloo)
jToggleButton4 = new JToggleButton();
getContentPane().add(jToggleButton4);
FlowLayout jToggleButton4Layout = new FlowLayout();
jToggleButton4.setLayout(jToggleButton4Layout);
jToggleButton4.setText("Black");
jToggleButton4.setPreferredSize(new java.awt.Dimension(133, 249));
You can take the default color/ the color you keep initially and can toggle the background color with an ActionEvent,
JToggleButton jtb = new JToggleButton("My Button");
Color defaultColor=jtb.getBackground();
jtb.addActionListener(new ActionListener( ) {
public void actionPerformed(ActionEvent ev) {
if(jtb.getBackground()==defaultColor)
jtb.setBackground(Color.BLACK);
else
jtb.setBackground(defaultColor);
repaint();//repaint your frame
System.out.println("BackGround color changed!");
}
});

Issue with button icons, Java 8-Queens puzzle

The problem I am running into is this: I have a grid of buttons in a JPanel, these buttons are supposed to change to an image of a queen when I click them. The code looks like this:
private Component createButtonBlack() {
final JButton button = new BoardButton();
final ImageIcon queen = new ImageIcon("/images/queen.png");
button.setBackground(Color.BLACK);
button.setPreferredSize(new Dimension(40, 40));
class QueenClick implements ActionListener {
public void actionPerformed(ActionEvent event) {
button.setIcon(queen);
button.repaint();
}
} // end QueenClick
ActionListener queenClicker = new QueenClick();
button.addActionListener(queenClicker);
return button;
} // end createButtonBlack
The problem (image not appearing) occurs on both the methods for creating black and white buttons but the methods are the same except for the color. Ideally I would like to be able to un-click the buttons and the image disappears but I do not know how to do that.
I am having difficulty with other parts of my 8queens GUI based problem so if you have any suggestions let me know!
Also if you need more code I will certainly supply it. Thank you.
State the exact problem when asking a question.
These buttons are supposed to change to an image of a queen when I click them.
So I'm guessing the icon doesn't change?
Did you:
Verify the ActionListener code is executed?
Verify the Icon was read properly?
You can easily add a System.out.println(...) to verify both of the above.
final ImageIcon queen = new ImageIcon("/images/queen.png");
I'm guessing the problem is the leading "/" in the path. The "/" tells the file system to look at the root of the drive.
if you have any suggestions let me know!
There is no need to create two methods. You can just do:
Component button = createButton();
button.setBackground( Color.BLACK );
There is no need to create individual ActionListeners. You can create a single generic listener with code like:
ActionListener queenClicker = new ActionListener()
{
#Override
public void actionPerformed(Action Event e)
{
JButton button = (JButton)e.getSource();
button.setIcon( queen );
//button.repaint(); // not needed the setIcon method will do the repaint()
}
}

Image button created from JLabel is not working

I'm trying to make a buttom from an image on the JFrame using the ImageIcon and the addMouseListener that will replace the current image with another image by clicking it.
static JPanel jp = new JPanel();
final JLabel jl = new JLabel();
final JFrame jf = new JFrame();
ImageIcon image = new ImageIcon("image1.jpg");
jl.setIcon(image);
jp.add(jl);
jf.add(jp);
jf.validate();
JLabel button = new JLabel(image);
button.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
jl.setIcon( null );
ImageIcon image = new ImageIcon("image2.jpg");
jl.setIcon(image);
}
});
The GUI is displayed with image1.jpg, but the button does not work at all and I can't even test whether the replacement from image1 to image2 works. GUI will not do anything even if I attempt to click the image1.jpg displayed on the window.
Edit: Adjusted JLabel varaible to be final for now. Other similar questions intimate that this method should be working but I can't figure out what is wrong with the code.
Not really sure ActionListener works with JLabel either.
No you can't add an ActionListener to a JLabel. An easier approach is to make a JButton look like a JLabel, then you can add the ActionListener to the button:
JButton button = new JButton(...);
button.setBorderPainted(false);
button.setContentAreaFilled(false);
button.addActionListener(...);
but the button does not work at all
A mouse click is generated when a mousePressed and mouseReleased is received for the same mouse point. So if you move the mouse slightly the event will not get generated. Instead listen for the mousePressed() event.

image does not open when button is pressed in actionListener in my GUI?

I am trying to get an image to open when the PlaySci button is pressed so I put the image in the PlaySci action listener, however it only opens when the exit button is pressed?
I have looked at it for hours and still dont understand why, I have tried to get rid of the exit button alltogether but then the image does not show at all.
I made the image into a JLabel at the top:
ImageIcon scis1 = new ImageIcon("scis.jpg");
private JLabel picture = new JLabel(scis1);
Here is the code for my PlaySci button ActonListener:
class PlaySciHandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
String computerRand = sps.computerChoice();
txtComputerRand.setText(computerRand);
String result = sps.play(Sps.SCISSORS);
txtResult.setText(result);
picture.setBounds(60, 200, 400, 400);// this is the image I want displayed when the PlaySci button is pressed
panel.add(picture);
}
}
This is the exit button ActionListener (That for some reason is the only way to display the image):
class exitHandler implements ActionListener{
public void actionPerformed(ActionEvent e) {
int n = JOptionPane.showConfirmDialog(frame, //when this button is pressed the image comes up?
"Are you sure you want to exit?",
"Exit?",
JOptionPane.YES_NO_OPTION);
if(n == JOptionPane.YES_OPTION){
System.exit(0);
}
}
}
This is the code creating the button and adding the ActionListener:
btnPlaySci = new JButton ("Scissors!");
btnPlaySci.setBounds(180, 40, 110, 20);
btnPlaySci.addActionListener(new PlaySciHandler());
panel.add (btnPlaySci);
btnPlaySci.setForeground(Color.MAGENTA);
Any help would be appreciated.
You should repaint your panel after you add picture to it. See the code for PlaySciHandler actionPerformed method.
class PlaySciHandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
String computerRand = sps.computerChoice();
txtComputerRand.setText(computerRand);
String result = sps.play(Sps.SCISSORS);
txtResult.setText(result);
picture.setBounds(60, 200, 400, 400);// this is the image I want displayed when the PlaySci button is pressed
panel.add(picture);
panel.repaint();//Must repaint the panel .
}
}
Note: As a side note I would suggest you to never use null Layout for JPanel.Use the inbuilt Layouts provided by Swing. You can get more information about Layouts usage at this official site. Another one is that always stick with java naming conventions. Class exitHandler should be written as ExitHandler instead.To know more have a look at this official site.
Don't add the JLabel in the class PlaySciHandler implements ActionListener block.Add it in your createForm() method and make it invisible: picture.setVisible(false);
and when you want to display after a button click, make it visible : picture.setVisible(true);

Using mouse Motion Listener on JButton?

hii
I have used Image as JButton for set in to panel
but now i want to use mouse motion listener actions on that image
for that purpose what can i do
following is the code for image
JButton buttonForPicture = new JButton();
buttonForPicture.setBorder(new EmptyBorder(0, 0, 0, 0));
buttonForPicture.setOpaque(false);
buttonForPicture.setIcon(new ImageIcon("/Users/hussainalisyed/Documents/Images/pic9.jpg"));
panel5.add(buttonForPicture,BorderLayout.CENTER);
is there any another way to do that
or
...
I'm not sure exactly what you're asking?
Your button is like any other JButton:
buttonForPicture.addMouseMotionListener(new MouseMotionListener() {
#Override
public void mouseMoved(MouseEvent e) {
}
#Override
public void mouseDragged(MouseEvent e) {
}
});
That catches movement events for the whole button, not just the image.
Read the JButton API there are methods to change the icon on a mouse rollover, if thats what you are trying to do. Search the API for methods containing "icon" to see what your options are.
If you just want to know how to write a MouseMotionListener, then read the section from the Swing tutorail on How to Write a Mouse Motion Listener for a working example.

Categories