I am creating MDI application in swing I want to use hash map to display one JInternalFrame at one time. I am unable pass keys and values. I am passining key as JInternalJframe name
and value as object of JInternalJframe
. I am unble to proceed. I have pasted incomplete code. To make you understand.
import java.util.*;
import javax.swing.*;
public class HashMap {
static HashMap <String, JInternalFrame> myMap = new HashMap <>();
//String(key) is the name of JInternalFrame
//JInternalFrame(value) is name of object of JInternalFrame
public static void main(String [] ashu){
myMap.put("CityMaster",cm);
myMap.put("TransportMaster",tm);
myMap.put("AccountMaster",am);
myMap.put("BankMaster",bm);
for (String str: myMap.keySet()){
System.out.println(str);
}
for (JInternalFrame jf: myMap.values()){
System.out.println(jf);
}
}
}
I have created JInternalFrame named above as CityMaster,BankMaster, etc.
Try this out. Not much to explain. I just added some InternalFrames to a DesktopPane, as you should do. Disregard the setSize()
everywhere. It was just for brevity, and my laziness. You shouldn't be using set size
import java.awt.BorderLayout;
import java.awt.event.*;
import java.beans.PropertyVetoException;
import java.util.*;
import java.util.logging.*;
import javax.swing.*;
public class HashMapFrame {
Map<String, MyInternalFrame> iFrames;
String[] keys = {"TransportMaster", "AccountMaster", "BankMaster",
"GrandMaster", "WebMaster", "StackOverflowMaster"};
JDesktopPane desktop;
JButton button = new JButton("Add Frame");
int index = 0;
int x = 0;
int y = 0;
public HashMapFrame() {
desktop = new JDesktopPane();
iFrames = new HashMap<>();
for (String s : keys) {
iFrames.put(s, new MyInternalFrame(s, x, y));
x += 30;
y += 30;
}
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (index < 6) {
desktop.add(iFrames.get(keys[index]));
iFrames.get(keys[index]).setVisible(true);
try {
iFrames.get(keys[index]).setSelected(true);
} catch (PropertyVetoException ex) {
Logger.getLogger(HashMapFrame.class.getName()).log(Level.SEVERE, null, ex);
}
index++;
}
}
});
JFrame frame = new JFrame();
frame.add(desktop);
frame.add(button, BorderLayout.PAGE_END);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(550, 550);
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new HashMapFrame();
}
});
}
}
class MyInternalFrame extends JInternalFrame {
public MyInternalFrame(String title, int x, int y) {
super(title);
setSize(300, 300);
setLocation(x, y);
setClosable(true);
}
}