views:

67

answers:

4

I have a simple app, showing picture made of tiled images(named u1, u2,...,u16.jpg). Now I'd like to add some Events to it, so that I can show these images only when proper button is clicked. I've tried doing it on my own, but it's not working. Where am I doing something wrong?

Original code :

import java.awt.GridLayout;
import javax.swing.*;
import javax.swing.border.BevelBorder;

public class Tiles_2 {

    public static void main(String[] args) {
        final JFrame f = new JFrame("Usmiech");
        JPanel panel = new JPanel(new GridLayout(4, 4, 3, 3));
        JLabel l = new JLabel();

        for (int i = 1; i < 17; i++) {
            String path = "u"+ i+".jpg";
            l = new JLabel(new ImageIcon(path));
            l.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
            panel.add(l);
        }

        f.setContentPane(panel);
        f.setSize(300, 300);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }
}

New code :

import java.awt.GridLayout;
import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.event.*;

public class Zad_8_1 implements ActionListener {

    public void actionPerformed(ActionEvent e) {
        JButton b = (JButton)(e.getSource());
        String i = b.getText();
        b = new JButton(new ImageIcon("u"+i+".jpg"));
    }

    public static void main(String[] args) {
        final JFrame f = new JFrame("Smile");
        JPanel panel = new JPanel(new GridLayout(4, 4, 3, 3));
        JButton l = null;

        for (int i = 1; i < 17; i++) {
            String path = "u"+ i+".jpg";
            l = new JButton(""+i);
            l.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
            l.setSize(53,53);
            panel.add(l);
        }

        f.setContentPane(panel);
        f.setSize(300, 300);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }
}

This should work like this : this

+1  A: 

Try registering the buttons to listen for the event like this:

 for (int i = 1; i < 17; i++) {
        String path = "u"+ i+".jpg";
        l = new JButton(""+i);
        l.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
        l.setSize(53,53);
        l.addActionListener(this);
        panel.add(l);
    }
Vincent Ramdhanie
+1, for being the first to mention adding the ActionListener (I assumed that was done when I posted my answer).
camickr
A: 

Instead of reassigning b a new instance of JButton, you should simply reset the icon:

b.setIcon(new ImageIcon("u"+i+".jpg"));

(take a look at pass by reference/pass by value concepts in Java, well explained here)

on top of that, you need to follow Vincent's advice and actually register with all of the buttons as an ActionListener

l.addActionListener(this);
akf
A: 
b = new JButton(new ImageIcon("u"+i+".jpg")); 

Creating a new button doesn't do anything the button just sits there in memory as it hasn't been added to the GUI.

The simple solution is to just reset the icon:

b.setIcon( new ImageIcon(...) );
camickr
A: 

You need to have constructor to add event listeners.

import java.awt.GridLayout;
import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.event.*;
import java.awt.*;

public class Demo {

    public static void main(String args[]) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                render("Window");
            }
        });
    }

    public static void render(String s){
        JFrame f = new JFrame("Smile");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Pane pan = new Pane();
        f.add(pan);
        f.setSize(300,300);
        f.setVisible(true);
    }
}

    class Pane extends JPanel implements ActionListener {

        Pane(){
            setLayout(new GridLayout(4, 4, 3, 3));
            JButton l = null;
            for (int i = 1; i < 17; i++) {
                l = new JButton(""+i);
                l.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
                l.addActionListener(this);
                l.setSize(53,53);
                add(l);
            }
        }

    public void actionPerformed(ActionEvent e) {
        JButton b = (JButton)(e.getSource());
        String i = b.getText();
        b.setIcon(new ImageIcon("u"+i+".jpg"));
    }
    }
owca