我在桌面窗格中有一个 JInternalframe (tab_items)。每当用户单击 tab_items 中的弹出菜单时,我想在同一桌面窗格中打开另一个 JInternalframe (frm_add_items)。我怎样才能做到这一点?
//class tab_items
public class tab_items extends JInternalFrame implements MouseListener,DocumentListener,ActionListener{
DS co= new DS ();
JPanel panel1;
JScrollPane pane;
DefaultTableModel model= new DefaultTableModel();
JTable tbl= new JTable(model);
JLabel lbl_search;
JTextField txt_search;
JPopupMenu pop;
JMenuItem mi_add,mi_edit, mi_del;
public tab_items(){
panel1= new JPanel();
panel1.setLayout(null);
//popupmenu
pop=new JPopupMenu();
mi_add=new JMenuItem("Add new record");
mi_edit=new JMenuItem("Edit record");
mi_del=new JMenuItem("Delete record");
add(pop);
pop.add(mi_add);
mi_add.addActionListener(this);
mi_add.addMouseListener(this);
pop.add(mi_edit);
mi_edit.addActionListener(this);
pop.add(mi_del);
mi_del.addActionListener(this);
// try{
// UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
// }catch(Exception ex){}
initcomponents();
setVisible(true);
setSize(700,500);
setLocation(100,150);
setTitle("List Of Items");
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void initcomponents(){
try{
model.addColumn("ID");
model.addColumn("Item Name");
model.addColumn("Cost Price");
model.addColumn("Selling Price");
model.addColumn("Qty");
tbl.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
PreparedStatement pstat=co.con.prepareStatement("Select * from tbl_items ORDER BY itemid ASC");
ResultSet rs=pstat.executeQuery();
while(rs.next()){
model.addRow(new Object[]{rs.getInt(1), rs.getString(2).trim(), rs.getString(3).trim(), rs.getString(4).trim(),rs.getInt(5)});
}
}catch(Exception ex){}
// SETTING COLUMN WIDTH
TableColumnModel model=tbl.getColumnModel();
model.getColumn(0).setPreferredWidth(50);
model.getColumn(1).setPreferredWidth(400);
model.getColumn(2).setPreferredWidth(100);
model.getColumn(3).setPreferredWidth(100);
model.getColumn(4).setPreferredWidth(50);
// adding panel
add(panel1);
panel1.setBounds(0,0,800,600);
//scrollpane initialize
int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;
int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS;
pane=new JScrollPane(tbl, v, h); //table included in scrollpane
panel1.add(pane);
pane.setBounds(0,50,700,400);
tbl.addMouseListener(this);
lbl_search= new JLabel();
lbl_search.setIcon(new ImageIcon(getClass().getResource("/img/btn_search_up.png")));
panel1.add(lbl_search);
lbl_search.setBounds(5,10,25,20);
txt_search=new JTextField();
panel1.add(txt_search);
txt_search.setBounds(35,10,200,20);
txt_search.getDocument().addDocumentListener(this);
/*
btn_add= new JButton();
btn_add.setIcon(new ImageIcon(getClass().getResource("/img/add-button-md.png")));
// btn_add.setHorizontalAlignment(SwingConstants.CENTER);
// btn_add.setText("Add");
add(btn_add);
btn_add.setBounds(665, 125, 65, 65);
btn_edit= new JButton();
btn_edit.setText("Update");
add(btn_edit);
btn_edit.setBounds(665, 200, 65, 65);
btn_delete=new JButton();
btn_delete.setText("Delete");
add(btn_delete);
btn_delete.setBounds(665, 275, 65, 65);
*/
}
public void mouseClicked(MouseEvent e) {}
public void mousePressed(MouseEvent me){maybeShowPopup(me);}
public void mouseReleased(MouseEvent me){maybeShowPopup(me);}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
private void maybeShowPopup(MouseEvent e){
if (e.isPopupTrigger()){
pop.show(e.getComponent(),e.getX(), e.getY());
}
}
public void update(DocumentEvent de){
Document doc=(Document)de.getDocument();
int length=doc.getLength();
String str=null;
try
{
str=doc.getText(0,length);
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(null, "Error in retreiving Search Length\n"+ex);
}
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/dstore","root","");
PreparedStatement pstat=con.prepareStatement("select * from tbl_items where itemid LIKe '"+str+"%' OR itemname LIKe '"+str+"%' ORDER BY itemid ASC");
ResultSet rs=pstat.executeQuery();
model.setRowCount(0);
while(rs.next()){
model.addRow(new Object[]{rs.getInt(1), rs.getString(2).trim(), rs.getString(3).trim(), rs.getString(4).trim(),rs.getInt(5)});
}
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(null,"ERROR IN DOCUMENT LISTENER.\nCannot Fetch Records"+ex);
}
}
public void insertUpdate(DocumentEvent de) {
update(de);
}
public void removeUpdate(DocumentEvent de) {
update(de);
}
public void changedUpdate(DocumentEvent de) {
update(de);
}
public void actionPerformed(ActionEvent ae) {
if(ae.getSource()==mi_add){
// frm_add_item ob= new frm_add_item();
// ob.show();
// dp.add(ob);
}else if(ae.getSource()==mi_edit) {
}else {
}
}
}
#//class frm_main ( it contains desktop pane)#