Hi i have MainFrame class:
public class MainFrame extends JFrame{
private JLabel originalLabel;
private JLabel filteredImage;
public MainFrame(){
setExtendedState(JFrame.MAXIMIZED_BOTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
initComponents();
setVisible(true);
}
private void initComponents(){
ImageFilterMenuBar menuBar = new ImageFilterMenuBar();
originalLabel = new JLabel("Test1");
filteredImage = new JLabel("Test2");
Component verticalStrut = Box.createVerticalStrut(10);
JPanel central = new JPanel();
central.add(originalLabel);
central.add(verticalStrut);
central.add(filteredImage);
add(new RadioButtonsPanel(), BorderLayout.SOUTH);
add(central, BorderLayout.CENTER);
setJMenuBar(menuBar);
}
}
and MenuBar class:
public class ImageFilterMenuBar extends JMenuBar{
private JMenu fileMenu;
private JMenuItem openImage;
private JMenuItem exit;
public ImageFilterMenuBar(){
initCompoments();
}
private void initCompoments() {
fileMenu = new JMenu("File");
setMenuItems();
add(fileMenu);
}
private void setMenuItems(){
openImage = new JMenuItem("Open Image");
exit = new JMenuItem("Exit");
openImage.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,
InputEvent.CTRL_MASK));
exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_CANCEL,InputEvent.SHIFT_MASK));
openImage.addActionListener(new OpenListener());
exit.addActionListener(new ExiteListener());
fileMenu.add(openImage);
fileMenu.add(exit);
}
}
in MenuBar class you can sea OpenButton. it’s open JFileChooser and returns me url of file which I chose.
So now I don’t know how to send this url into my MainFrame class where I want to display this file..
Any ideas??
The easiest solution is to pass a reference to
MainFrameto theImageFilterMenuBar:Then, add a method like below in the MainFrame:
In the
ImageFilterMenuBaryou would keep a reference to theMainFramein a member variable and use it to callsetImageFile()once theJFileChooserreturns a File.A more difficult to implement solution would be to implement the observer pattern. This is the way the listeners work in Swing.
You would register the main frame as a listener and the other class would be the one to notify about file selection changes.
To open a file chooser and get the selected file: