Adding a JButton when another JButton is pressed - java

I have a JButton which I want to create a new JButton with when it is pressed I have added an ActionListener that looks like this, but it doesn't add another JButton.
public void actionPerformed(ActionEvent e){
Object command = e.getSource();
if(command.equals(play)){
ImageIcon i1 = new ImageIcon("NewGame.png");
width = i1.getIconWidth();
height = i1.getIconWidth();
newGame = new JButton(i1);
newGame.setBorderPainted(false);
newGame.setContentAreaFilled(false);
newGame.setSize(width, height);
newGame.setLocation(600,100);
add(newGame);
System.out.println("It Works");
}
}
How would I make it so that when I press the already existing JButton this one will be added?

Make sure to revalidate and repaint after adding the button
revalidate();
repaint();
From the use of setSize and setLocation, it appears you are using a absolute positioning or null layout. Use a layout manager.

You must call repaint() after adding it.

if you are not getting exception, then you will need to refresh or repaint the container which will old the new button.

Related

Repaint TextFile in JPanel

I quite new in java and I encounter a problem with repaint a TextFile in my JPanel, this is code:
JPanel paneldol = new JPanel();
paneldol.add(new JButton(new AbstractAction("Oblicz pole i obwód")
{
#Override
public void actionPerformed(ActionEvent e) {
paneldol.repaint();
paneldol.revalidate();
}
}
));
paneldol.add(new TextField(model.getPole(), 10));
paneldol.add(new TextField(model.getObwod(), 10));
this.add(paneldol, BorderLayout.PAGE_END);
As you see TextField have metod that generate string, so when i click in button I want to repain panel to have new value in my Textfield, Is this possibly?
so when i click in button I want to repain panel to have new value in my Textfield, Is this possibly?"`
If all you want to do is to change the text in text field on button push, then you should give your class a JTextField variable (or multiple JTextField variables), assign a JTextField object to the variable, and this to the GUI. Then when within your button's listener, simply set the text of the JTextField via its setText(...) method. There's no need to call repaint() or revalidate() as they will do nothing useful in this situation.
Also don't mix AWT with Swing components. Use JTextFields not TextFields.

How can I click on a picture and then make that picture appear in a new JFrame? [duplicate]

This question already has answers here:
How to find JLabel that was clicked and show ImageIcon from it?
(2 answers)
Closed 7 years ago.
I have a JFrame opening with 12 different pictures. I want to click on one and then in the new JFrame, show that same picture. In my code, shirts is my JFrame with the 12 pictures and I want the clicked picture to appear in the new JFrame called sizes. I made an ArrayList of type JLabel called select. Here is my code:
final JFrame shirts = new JFrame("T-shirts");
JPanel panel = new JPanel(new GridLayout(4, 4, 3, 3));
for (int i = 1; i < 13; i++) {
l = new JLabel(new ImageIcon("T-shirts/"+i+".jpg"), JLabel.CENTER);
l.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
l.setFont(l.getFont().deriveFont(20f));
panel.add(l);
select.add(l);
}//end of for loop
panel.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
sizes = new JFrame("Shopping");
sizes.setVisible(true);
sizes.setSize(500, 500);
sizes.setLocation(100,200);
shirts.dispose();
for(int i=0; i<12; i++){
if(e.getSource()==select.get(i)){
JLabel addition = newJLabel(newImageIcon(select.get(i).getText()));
//sizes.add(select.get(i));//Picture isn't added!!
}//end of if
}//end of for
}//end of method
});//end of listener
shirts.setContentPane(panel);
shirts.setSize(1000, 1000);
shirts.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
shirts.setVisible(true);
Start with How to Write a Mouse Listener.
Basically, you want to register a MouseListener with the JLabel which represents your picture and implement it's mouseClicked method.
You then want to get the Icon property of the JLabel that was clicked and pass that to your new frame, which you can simply use to set the Icon property of another JLabel
Something like...
addition.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
JLabel label = (JLabel) e.getComponent();
Icon icon = label.getIcon();
// Create new frame and pass it the icon value
}
});
as an example
You might also like to take a look at The Use of Multiple JFrames, Good/Bad Practice? and consider using either a CardLayout or JDialog instead of another JFrame ... which could become messy
You can use a JButton and make it look like a JLabel:
JButton button = new JButton( new ImageIcon("..." ));
button.setBorderPainted( false );
button.setContentFilled( false );
button.setFocusPainted( false );
button.addActionListener( new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
JButton button = (JButton)e.getSource();
Icon icon = button.getIcon();
// do something with the Icon.
}
});
Then you can add an ActionListener to the button.
An ActionListener is more reliable than using a MouseListener and handling the mouseClicked event. The mouseClicked event is only generated when a mousePressed and mouseReleased event is generated at the same mouse point. So if the mouse moves by even pixel between the two event you will not get a mouseClicked event which users think is a random problem.
Try making each picture a JButton, or at least placing an invisible button behind each picture. Then, when they are pressed in the ActionListener extended class, have the new JFrame be created, with the picture large.

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()
}
}

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.

adding components dynamically in a JPanel

how can i add components dynamically in a jpanel?
I am having add button when i click the button the components should be added to the JPanel.
my question is that adding a textfield and button to jpanel when i click on the add button the user can click on the add button any number of times according to that i have to add them to the jpanel. i have added to scrollerpane to my jpanel,and jpanel layout manager is set to null.
Just as you always do, except that you have to call:
panel.revalidate();
when you are done, since the container is already realized.
Use an ActionListener, you can use an anonymous class like this:
JPanel myJPanel = new JPanel();
...
b = new Button("Add Component");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JLabel someLabel = new JLabel("Some new Label");
myJPanel.add(someLabel);
myJPanel.revalidate();
}
});

Categories