0

I'm new to Java and want to fix this problem. Here I'm placing the test code.

I have got two JFrames "Frame 1" and "Settings". Frame 1 has a Button to run Settings and a JLabel to preview images.

"Settings" has a JComboBox to select Image_1, Image_2...... How can I change the image on Frame 1 by using Action Performed for JCombobox placed on Settings ? I'm sorry if this is senseless.. Thank you.

I know how to do it withing the same class. The problem I'm facing because of these separated classes. That duplicate marked question asks for do it within the same JFrame. Can you help ?

Frame 1 :

import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.EventQueue;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class Frame1 extends JFrame {

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Frame1 frame = new Frame1();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Frame1() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        Image imgUniLogo = new ImageIcon(this.getClass().getResource("/Image1.png")).getImage();

        JLabel lblNewLabel = new JLabel("");
        lblNewLabel.setBounds(280, 71, 79, 93);
        contentPane.add(lblNewLabel);
        lblNewLabel.setIcon(new ImageIcon(imgUniLogo));

        JButton btnNewButton = new JButton("Settings");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                Frame2 frame = new Frame2();
                frame.setVisible(true);
            }
        });
        btnNewButton.setBounds(54, 209, 107, 23);
        contentPane.add(btnNewButton);
    }
}

Settings :

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;


public class Settings extends JFrame {

    private JPanel contentPane;

    public Settings() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JComboBox comboBox = new JComboBox();
        comboBox.setModel(new DefaultComboBoxModel(new String[] {"Image 1", "Image 2"}));
        comboBox.setBounds(140, 117, 151, 20);
        contentPane.add(comboBox);
    }
}
  • This very same problem is all well explained in the [JComboBox Tutorial](http://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html), or if that doesn't make sense to you, consider checking out the [many other similar questions and their answers to be found on this site](http://stackoverflow.com/search?q=%5Bjava%5D+%5Bswing%5D+change+image+jcombobox+selection). – Hovercraft Full Of Eels Jul 21 '15 at 18:09
  • Hi, I know how to do it withing the same class. The problem I'm facing because of these separated classes. That duplicate marked question asks for do it within the same JFrame. Can you help ? – Sasith Priyankara Jul 21 '15 at 18:14
  • The issue then is how to call methods from one class in another which is basic Java. Just pass a reference of the one instance into the other than needs it, and call public methods on the first instance. – Hovercraft Full Of Eels Jul 21 '15 at 18:16
  • 1
    Your image-displaying JLabel should not be declared in a method or constructor but should be declared as a private instance variable in the class, and you should give the same class a public method that allows the JComboBox holding class to call allowing the first class to change the Icon displayed in the JLabel. Also, learn and use the Swing layout managers and avoid using `null` layouts. While this latter issue is not the cause of your current problem, it will definitely cause future problems as it will leave you with a GUI that is very difficult to upgrade or enhance. – Hovercraft Full Of Eels Jul 21 '15 at 18:18
  • Thank you very much. I'm trying to do in that way... – Sasith Priyankara Jul 21 '15 at 18:46
  • See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Jul 22 '15 at 09:30

0 Answers0