2 years ago
#11683
Zyaan
Loading images via JFileChooser
3 Classes - GUI, ImageLoader and Wizard
GUI is the main frame whereas Wizard is just a popup that changes settings and imageloader is image handler
What I want is to load image from wizard to Jlabel on my main GUI but I have no idea where to start and never had experience with loading images onto GUI. Searching for answer on here didn't really help, it just create more confusion for me. I would appreciate if you also can add explanations as well.
GUI -
public class GUI extends JPanel{
//variables
JFrame frame = new JFrame("All-in-one Application");
JMenuBar menuBar;
JMenu file, edit, help, about;
JMenuItem settings, startWizard, exit, save, load, readme;
JLabel imgLabel;
private Wizard wiz;
public void topMenuBar(){
//creating the menubar
menuBar = new JMenuBar();
//Menu
file = new JMenu("File");
edit = new JMenu("Edit");
help = new JMenu("Help");
about = new JMenu("About");
//MenuItems
startWizard = new JMenuItem("Start Wizard");
//listener for wizard
Wizard wiz = new Wizard();
// GUIListener gListener = new GUIListener();
startWizard.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
wiz.startWizardd();
}
});
settings = new JMenuItem("Settings");
readme = new JMenuItem("Read Me");
exit = new JMenuItem("Exit");
save = new JMenuItem("Save");
load = new JMenuItem("Load");
//jbutton
// test = new JButton("Test");
//adding MenuItems to the Menu
/**file**/
file.add(startWizard);
file.add(new JSeparator());
file.add(save);
file.add(load);
file.add(new JSeparator());
file.add(exit);
//end file
//edit
edit.add(settings);
//help
help.add(readme);
//about
//Adding Menu to the menu bar
menuBar.add(file);
menuBar.add(edit);
menuBar.add(help);
menuBar.add(about);
//exit action listener
exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
}
public void mainPanel(){
FlowLayout main = new FlowLayout();
JPanel mainPanel = new JPanel();
mainPanel.setLayout(main);
//jlabel
imgLabel = new JLabel("img");
mainPanel.add(imgLabel);
frame.add(mainPanel, BorderLayout.CENTER);
}
//intiate GUI
public void intGUI(){
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(300,0);
frame.setSize(1400,1000);
topMenuBar();
mainPanel();
frame.setJMenuBar(menuBar);
frame.setVisible(true);
}
}
Wizard:
public class Wizard extends JPanel{
//variables
private ImageLoader imgload = new ImageLoader(this);
private JButton applyButton, nextButton, backButton, cancelButton, openMapButton;
JDialog wizDialog;
JFrame wizFrame;
JLabel siteNameLabel, siteMapLabel, AisleNumLabel, laneNumLabel, label;
JTextField siteNameField, aisleNumField, laneNumField;
private JTextField mapURLField = new JTextField(20);
JFileChooser chooser;
private File file;
String fileName;
BufferedImage img; // private JPanel addInfoPanel, controlPanel;
public void startWizardd(){
System.out.println("Starting Wizard....");
wizardGUI();
}
public void wizardCmpt()
{
//setting the panel up with gridlayout settings
FlowLayout map = new FlowLayout();
JPanel mapPanel = new JPanel();
mapPanel.setLayout(map);
FlowLayout grid = new FlowLayout();
JPanel addInfoPanel = new JPanel();
addInfoPanel.setLayout(grid);
FlowLayout controls = new FlowLayout();
JPanel controlPanel = new JPanel();
controlPanel.setLayout(controls);
//buttons
JButton openMapButton = new JButton("Load Map");
openMapButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
imgload.fileChooser();
}
});
JButton applyButton = new JButton("Apply");
applyButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
JButton nextButton = new JButton("Next");
JButton backButton = new JButton("Back");
JButton cancelButton = new JButton("Cancel");
cancelButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
wizFrame.setVisible(false);
wizFrame.dispose();
}
});
//jlabels
JLabel siteNameLabel = new JLabel("Site Name :");
JLabel siteMapLabel = new JLabel("Site Map :");
JLabel AisleNumLabel = new JLabel("Number of Aisles :");
JLabel laneNumLabel = new JLabel("Number of Lanes :");
//jtextfield
JTextField siteNameField = new JTextField(5);
JTextField aisleNumField = new JTextField(5);
JTextField laneNumField = new JTextField(5);
//adding to GUI
addInfoPanel.add(siteNameLabel);
addInfoPanel.add(siteNameField);
addInfoPanel.add(AisleNumLabel);
addInfoPanel.add(aisleNumField);
addInfoPanel.add(laneNumLabel);
addInfoPanel.add(laneNumField);
mapPanel.add(siteMapLabel);
mapPanel.add(mapURLField);
mapPanel.add(openMapButton);
controlPanel.add(applyButton);
// controlPanel.add(nextButton);
// controlPanel.add(backButton);
controlPanel.add(cancelButton);
wizFrame.add(addInfoPanel, BorderLayout.NORTH);
wizFrame.add(mapPanel, BorderLayout.CENTER);
wizFrame.add(controlPanel, BorderLayout.SOUTH);
}
public void setMapUrlField(String text){
mapURLField.setText(text);
}
public File getFile(){
return file;
}
private void wizardGUI(){
System.out.println("it's loading");
wizFrame = new JFrame("Wizard Operation");
wizFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
wizFrame.setSize(600,600);
wizardCmpt();
wizFrame.pack();
wizFrame.setVisible(true); } }
Image Loader:
public class ImageLoader{
private JLabel label;
JFileChooser chooser;
File file;
private BufferedImage img;
private Wizard wiz;
public ImageLoader(Wizard wiz){
this.wiz = wiz;
}
public File fileChooser(){
File file = null;
JFileChooser fileChooser = new JFileChooser();
FileFilter imageFilter = new FileNameExtensionFilter("Image files ", ImageIO.getReaderFileSuffixes());
fileChooser.setFileFilter(imageFilter);
int retValue = fileChooser.showOpenDialog(wiz);
if(retValue == JFileChooser.APPROVE_OPTION){
file = fileChooser.getSelectedFile();
wiz.setMapUrlField(file.getAbsolutePath());
}else{
wiz.setMapUrlField("");
}
return file;
}
}
java
image
swing
panel
jfilechooser
0 Answers
Your Answer