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