JFileChooser+FileNameExtensionFilter 文件选择器+过滤器------上传图片功能 表格内部操作
2012-10-20 10:08
411 查看
package bean; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Vector; import javax.swing.ButtonGroup; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenuBar; import javax.swing.JRadioButton; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JTextField; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; import javax.swing.filechooser.FileNameExtensionFilter; import javax.swing.table.DefaultTableModel; public class MainFrame extends JFrame { public final static int MAX_X=620; public final static int MAX_Y=480; private JMenuBar jmb ; private JLabel nameLbl; private JLabel sexLbl; private JLabel ageLbl; private JLabel classLbl; private JLabel photoLbl; private JButton selectJbt; private JButton deleteJbt; private JButton udpateJbt; private JButton insertJbt; private JRadioButton maleJrb; private JRadioButton femaleJrb; private ButtonGroup bg; private JTextField nameJtf; private JTextField ageJtf; private JTextField classJtf; private JTextField searchJtf; private JTable jtbl; private DefaultTableModel dtm; //表格用的数据模型 private JScrollPane jsp;//存放表格的,表格必须放在里面 private String fileName; //图片路径 public MainFrame(){ init(); this.setBounds(100, 100,MAX_X,MAX_Y); this.setTitle("学生信息管理"); this.setResizable(false); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } public void init(){ this.setLayout(null); this.add(this.getNameLbl()); this.add(this.getNameJtf()); this.add(this.getSexLbl()); this.getBg(); this.add(this.getMaleJrb()); this.add(this.getFemaleJrb()); this.add(this.getPhotoLbl()); this.add(this.getAgeLbl()); this.add(this.getAgeJtf()); this.add(this.getClassLbl()); this.add(this.getClassJtf()); this.add(this.getUdpateJbt()); this.add(this.getJsp()); } public JMenuBar getJmb() { return jmb; } public void setJmb(JMenuBar jmb) { this.jmb = jmb; } //-------------------------------------name------------------------ public JLabel getNameLbl() { if(nameLbl==null){ nameLbl = new JLabel("姓名:"); nameLbl.setBounds(30, 30, 50, 30); } return nameLbl; } public void setNameLbl(JLabel nameLbl) { this.nameLbl = nameLbl; } public JLabel getSexLbl() { if(sexLbl == null){ sexLbl = new JLabel("性别:"); sexLbl.setBounds(250, 30, 50, 30); } return sexLbl; } public void setSexLbl(JLabel sexLbl) { this.sexLbl = sexLbl; } //-----------------------------------------age-------------------------- public JLabel getAgeLbl() { if(ageLbl==null){ ageLbl = new JLabel("年龄:"); ageLbl.setBounds(30, 100, 50, 30); } return ageLbl; } public void setAgeLbl(JLabel ageLbl) { this.ageLbl = ageLbl; } //----------------------------------------class--------------------------------- public JLabel getClassLbl() { if(classLbl == null){ classLbl = new JLabel("班级:"); classLbl.setBounds(250, 100, 50, 30); } return classLbl; } public void setClassLbl(JLabel classLbl) { this.classLbl = classLbl; } //-----------------------------------------photo------------------------------ public JLabel getPhotoLbl() { if(photoLbl == null){ photoLbl = new JLabel(); photoLbl.setBounds(430, 10, 180, 180); setDefaultPhoto();//设置默认图片 photoLbl.setToolTipText("点我改图片"); photoLbl.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent arg0) { // TODO Auto-generated method stub JFileChooser chooser = new JFileChooser();//文件选择器 FileNameExtensionFilter filter = new FileNameExtensionFilter( "请选择图片文件", "png", "jpg");//文件名过滤器 chooser.setFileFilter(filter);//给文件选择器加入文件过滤器 int returnVal = chooser.showOpenDialog(MainFrame.this); if(returnVal == JFileChooser.APPROVE_OPTION) { File file = chooser.getSelectedFile();//得到文件对象 fileName = chooser.getSelectedFile().getName();//得到文件名 //得到要存入的路径 String newFile = "c:/pic/"+fileName; //得到文件后,上传到我们统一文件夹下,并显示出来 //使用二进制流进行操作 try { BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newFile)); int i = bis.read(); while(i!=-1){ bos.write(i); i = bis.read(); } bos.close(); bis.close(); //将上传完的图片显示出来 photoLbl.setIcon(new ImageIcon(newFile)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }); } return photoLbl; } public void setPhotoLbl(JLabel photoLbl) { this.photoLbl = photoLbl; } public JButton getSelectJbt() { return selectJbt; } public void setSelectJbt(JButton selectJbt) { this.selectJbt = selectJbt; } public JButton getDeleteJbt() { return deleteJbt; } public void setDeleteJbt(JButton deleteJbt) { this.deleteJbt = deleteJbt; } public JButton getUdpateJbt() { if(udpateJbt==null){ udpateJbt = new JButton("修改"); udpateJbt.setBounds(350, 150, 60, 30); udpateJbt.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub String name = nameJtf.getText(); String sex = maleJrb.isSelected()?"male":"female"; int age = Integer.parseInt(ageJtf.getText()); String clazz = classJtf.getText(); Student s = new Student(); s.setName(name);s.setSex(sex);s.setAge(age);s.setClazz(clazz);s.setPhoto(fileName); System.out.println(s);//存入数据库了 //更新表格 //jtbl.setModel(arg0); } }); } return udpateJbt; } public void setUdpateJbt(JButton udpateJbt) { this.udpateJbt = udpateJbt; } public JButton getInsertJbt() { return insertJbt; } public void setInsertJbt(JButton insertJbt) { this.insertJbt = insertJbt; } //------------------------------------------------namejtf---------------------- public JTextField getNameJtf() { if(nameJtf==null){ nameJtf = new JTextField(); nameJtf.setBounds(80, 30, 120, 30); } return nameJtf; } public void setNameJtf(JTextField nameJtf) { this.nameJtf = nameJtf; } //---------------------------------------------agejtf------------------------------ public JTextField getAgeJtf() { if(ageJtf==null){ ageJtf = new JTextField(); ageJtf.setBounds(80, 100, 120, 30); } return ageJtf; } public void setAgeJtf(JTextField ageJtf) { this.ageJtf = ageJtf; } public JTextField getClassJtf() { if(classJtf == null){ classJtf = new JTextField(); classJtf.setBounds(300, 100, 120, 30); } return classJtf; } public void setClassJtf(JTextField classJtf) { this.classJtf = classJtf; } public JTextField getSearchJtf() { return searchJtf; } public void setSearchJtf(JTextField searchJtf) { this.searchJtf = searchJtf; } //---------------------------------------------jtbl-------------------- public JTable getJtbl() { if(jtbl == null){ jtbl = new JTable(this.getDtm()); jtbl.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent arg0) { // TODO Auto-generated method stub //1.得到选中的行 int row = jtbl.getSelectedRow(); //2.将选中行的列加入到指定控件中 //System.out.println(dtm.getValueAt(jtbl.getSelectedRow(), 0)); nameJtf.setText((String)dtm.getValueAt(row, 0)); ageJtf.setText(((Integer)dtm.getValueAt(row, 2)).toString()); String sex = (String)dtm.getValueAt(row, 1); if(sex.equals("male")){ maleJrb.setSelected(true); }else{ femaleJrb.setSelected(true); } classJtf.setText((String)dtm.getValueAt(row, 3)); photoLbl.setIcon(new ImageIcon("c:/pic/"+(String)dtm.getValueAt(row, 4))); fileName = (String)dtm.getValueAt(row, 4);//存下当前显示的图片的路径 } }); } return jtbl; } public void setJtbl(JTable jtbl) { this.jtbl = jtbl; } public DefaultTableModel getDtm() { Vector cols = new Vector(); cols.add("姓名");cols.add("性别");cols.add("年龄");cols.add("班级");cols.add("照片"); Vector data = new Vector(); StudentDao sd = new StudentDao(); ArrayList alist = sd.getAllStudent(); for(Object obj : alist){ Vector v = new Vector(); Student s = (Student)obj; v.add(s.getName()); v.add(s.getSex()); v.add(s.getAge()); v.add(s.getClazz()); v.add(s.getPhoto()); data.add(v); } dtm = new DefaultTableModel(data,cols); return dtm; } public void setDtm(DefaultTableModel dtm) { this.dtm = dtm; } //------------------------------------jsp------------------------ public JScrollPane getJsp() { if(jsp == null){ jsp = new JScrollPane(this.getJtbl()); jsp.setBounds(0, 240, MAX_X, 240); } return jsp; } public void setJsp(JScrollPane jsp) { this.jsp = jsp; } public static void main(String[] args) { new MainFrame(); } public JRadioButton getMaleJrb() { if(maleJrb == null){ maleJrb = new JRadioButton("男"); maleJrb.setBounds(300, 30, 60, 30); } return maleJrb; } public void setMaleJrb(JRadioButton maleJrb) { this.maleJrb = maleJrb; } public JRadioButton getFemaleJrb() { if(femaleJrb == null){ femaleJrb = new JRadioButton("女"); femaleJrb.setBounds(360, 30, 60, 30); } return femaleJrb; } public void setFemaleJrb(JRadioButton femaleJrb) { this.femaleJrb = femaleJrb; } public ButtonGroup getBg() { if(bg == null){ bg = new ButtonGroup(); bg.add(this.getMaleJrb());bg.add(this.getFemaleJrb()); } return bg; } public void setBg(ButtonGroup bg) { this.bg = bg; } //设置photoLbl中的默认照片 private void setDefaultPhoto() { // TODO Auto-generated method stub getPhotoLbl().setIcon(new ImageIcon("src/default.jpg")); } }
相关文章推荐
- ueditor 编辑器的配置 实现了上传图片与文件功能---附效果图
- 过滤器、监听器、批量操作、文件上传下载
- java操作ftp实现文件的上传下载(适用于图片文档服务器)
- ExtAspNet新春贺岁版 - 文件上传控件、复选框列表控件、完善布局管理、表格的行扩展列、完整的100多个中文示例、30多处BUG修正与功能增强、《ExtAspNet秘密花园》系列文章
- 实现图片上传预览和取消文件上传功能
- FCKeditor是使用非常广泛的HTML编辑器,本文从 ASP.NET 的使用场景对 FCKeditor 与 FCKeditor.NET 的配置、功能扩展(如自定义文件上传子目录、自定义文件名、上传图片的后期处理等)、以及安全性进行初步的阐述。
- 完美实现 IE10 下利用HTML5实现文件上传(图片上传)功能
- PHP使用curl请求实现post方式上传图片文件功能示例
- java模拟表单上传文件,java通过模拟post方式提交表单实现图片上传功能实例
- 单个文件(图片)上传功能
- JQ图片文件上传之前预览功能的简单实例(分享)
- 【Vue】vue 嵌入app内部,调用app的选择图片和录制视频功能的时候,当前的.vue文件方法失效
- 资料 文件上传 图片预览 功能
- Android Webview实现图片、文件上传及启动相机功能
- 【转】[原创]ExtAspNet新春贺岁版 - 文件上传控件、复选框列表控件、完善布局管理、表格的行扩展列、完整的100多个中文示例、30多处BUG修正与功能增强、《ExtAspNet秘密花园》系列文章
- 通过WebClient的Postdata将上传文件功能放到另一个网站上去操作
- jqm文件上传,上传图片,jqm的表单操作,jqm的ajax的使用,jqm文件操作大全,文件操作demo
- jqm文件上传,上传图片,jqm的表单操作,jqm的ajax的使用,jqm文件操作大全,文件操作demo
- 图片上传功能之~百度编辑器单独作为图片上传文件上传功能,百度编辑器做缩略图