1 Replies - 576 Views - Last Post: 02 March 2012 - 12:26 AM Rate Topic: -----

#1 sudhamadhuri   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 01-March 12

to attach the attachment by using the jfile chooser

Posted 01 March 2012 - 11:05 PM

hi sir,i am working on java swings to enter all the employee details including the attachment that is it can be either a word document or any kind.For this i have developed the code.The code which i have written can be able to open the GUI part the problem is that when i click on the browse button the window must be opened inorder to attach the attachment by using the jfile chooser i wrote the code but it is not working please verify this.The following is my code please verify.


import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.regex.*;
import java.util.*;
import java.text.*;

class prog1 extends JFrame
{
JLabel lbl,lbl1,lbl2,lbl3,lbl4,lbl6,lbl7,lbl8,lbl9,lbl10,lbl11,lbl12;
JTextField txt,txt1,txt2,txt3,txt4,txt5,txt6,txt8,txt9,txt10,txt11;
JButton submit_btn,reset_btn;//,browse_btn;
JComboBox combo;
String items[] = {"Select","Casual Leave", "Earned Leave","Adoption Leave","Advance Earned Leave","Flexi Holiday","Maternity Leave", "Sick Leave", "Laeve without pay","Special maternity leave"};
SpringLayout lay;
public prog1(){
super("Employee Details");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(1370,1500);
setVisible(true);
widgets();
}
public class Test{
private Controller btnCtrl;
btnCtrl = new Controller
JButton browse_btn = new JButton();
browse_btn.addActionListener(btnCtrl);
private class Controller implements ActionListener{
public void actionPerformed(ActionEvent ae){
Object o= ae.getSource();
if(o==browse_btn)
{
JFileChooser chooser = new JFileChooser();
int returnVal = chooser.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("You chose to open this file: " +
chooser.getSelectedFile().getPath());
}
}

}

}



public static void main(String args[]){
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new prog1().setVisible(true);
}
});
}

public void widgets(){
lbl=new JLabel("Employee ID:");
txt=new JTextField(18);
txt.addKeyListener(new KeyAdapter()
{
public void keyReleased(KeyEvent e)
{
String st=txt.getText();
Pattern p = Pattern.compile("(\\d)");
Matcher m = p.matcher(st);
if(!m.find())
{
JOptionPane.showMessageDialog(null, "Only numbers are allowed!", "Error!", JOptionPane.ERROR_MESSAGE);
txt.setText("");
}
}
});

lbl1=new JLabel("First Name:");
txt1=new JTextField(18);
txt1.addKeyListener(new KeyAdapter()
{
public void keyReleased(KeyEvent e)
{
String st=txt1.getText();
char chr = st.charAt(0);
boolean check = Character.isUpperCase( chr);
Pattern p = Pattern.compile("^[a-zA-Z]+$");
Matcher m = p.matcher(st);
if(!m.find())
{
JOptionPane.showMessageDialog(null, "Only characters A-Z, a-z are allowed!","Error!", JOptionPane.ERROR_MESSAGE);
txt1.setText("");
}
else if(!check)
{
JOptionPane.showMessageDialog(null, "First character should start with capital letter!","Error!", JOptionPane.ERROR_MESSAGE);
txt1.setText("");
}
}
});

lbl2=new JLabel("Last Name:");
txt2=new JTextField(18);
txt2.addKeyListener(new KeyAdapter()
{
public void keyReleased(KeyEvent e)
{
String st=txt2.getText();
char chr = st.charAt(0);
boolean check = Character.isUpperCase( chr);
Pattern p = Pattern.compile("^[a-zA-Z]+$");
Matcher m = p.matcher(st);
if(!m.find())
{
JOptionPane.showMessageDialog(null, "Only characters A-Z, a-z are allowed!","Error!", JOptionPane.ERROR_MESSAGE);
txt1.setText("");
}
else if(!check)
{
JOptionPane.showMessageDialog(null, "First character should start with capital letter!","Error!", JOptionPane.ERROR_MESSAGE);
txt1.setText("");
}
}
});


lbl3=new JLabel("Won no:");
txt3=new JTextField(18);
txt3.addKeyListener(new KeyAdapter()
{
public void keyReleased(KeyEvent e)
{
String st=txt3.getText();
Pattern p = Pattern.compile("(\\d)");
Matcher m = p.matcher(st);
if(!m.find())
{
JOptionPane.showMessageDialog(null, "Only numbers are allowed!", "Error!", JOptionPane.ERROR_MESSAGE);
txt3.setText("");
}
}
});

lbl4=new JLabel("From Date:(MM/dd/yyyy)");
txt4=new JTextField(18);
txt4.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
String date=txt4.getText();
try{
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
df.setLenient(false);
Date dt2 = df.parse(date);
lbl4.setVisible(false);
}
catch(Exception ex){
lbl4.setText("* Invalid Date");
lbl4.setVisible(true);
lbl4.setForeground(Color.red);
}
}
});

lbl6=new JLabel("ToDate:");
txt5=new JTextField(18);

lbl7=new JLabel("No Of Days:");
txt6=new JTextField(18);
txt6.addKeyListener(new KeyAdapter()
{
public void keyReleased(KeyEvent e)
{
String st=txt6.getText();
Pattern p = Pattern.compile("(\\d)");
Matcher m = p.matcher(st);
if(!m.find())
{
JOptionPane.showMessageDialog(null, "Only numbers are allowed!", "Error!", JOptionPane.ERROR_MESSAGE);
txt6.setText("");
}
}
});

lbl8=new JLabel("Leave Type:");
combo = new JComboBox(items);

lbl9=new JLabel("Project:");
txt8=new JTextField(18);

lbl10=new JLabel("Approver Name:");
txt9=new JTextField(18);


lbl11=new JLabel("Send Mail To:");
txt10=new JTextField(18);
//txt10.JTextField( String [email protected]);

lbl12=new JLabel("Attachment:");
txt11=new JTextField(18);

JButton submit_btn=new JButton("Submit");
JButton reset_btn = new JButton("Reset");
JButton browse_btn = new JButton("Browse");


add(lbl);
add(txt);
add(lbl1);
add(txt1);
add(lbl2);
add(txt2);
add(lbl3);
add(txt3);
add(lbl4);
add(txt4);
add(lbl6);
add(txt5);
add(lbl7);
add(txt6);
add(lbl8);
add(lbl9);
add(txt8);
add(lbl10);
add(txt9);
add(lbl11);
add(txt10);
add(lbl12);
add(txt11);
add(submit_btn);
add(reset_btn);
add(combo);
add(browse_btn);


lay=new SpringLayout();
lay.putConstraint(SpringLayout.WEST,lbl,370,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,lbl,60,SpringLayout.NORTH,this);

lay.putConstraint(SpringLayout.WEST,txt,520,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,txt,60,SpringLayout.NORTH,this);

lay.putConstraint(SpringLayout.WEST,lbl1,370,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,lbl1,110,SpringLayout.NORTH,this);

lay.putConstraint(SpringLayout.WEST,txt1,520,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,txt1,110,SpringLayout.NORTH,this);

lay.putConstraint(SpringLayout.WEST,lbl2,750,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,lbl2,110,SpringLayout.NORTH,this);

lay.putConstraint(SpringLayout.WEST,txt2,820,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,txt2,110,SpringLayout.NORTH,this);

lay.putConstraint(SpringLayout.WEST,lbl3,370,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,lbl3,160,SpringLayout.NORTH,this);

lay.putConstraint(SpringLayout.WEST,txt3,520,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,txt3,160,SpringLayout.NORTH,this);

lay.putConstraint(SpringLayout.WEST,lbl4,370,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,lbl4,210,SpringLayout.NORTH,this);

lay.putConstraint(SpringLayout.WEST,txt4,520,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,txt4,210,SpringLayout.NORTH,this);

lay.putConstraint(SpringLayout.WEST,lbl6,750,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,lbl6,210,SpringLayout.NORTH,this);

lay.putConstraint(SpringLayout.WEST,txt5,820,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,txt5,210,SpringLayout.NORTH,this);

lay.putConstraint(SpringLayout.WEST,lbl7,370,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,lbl7,280,SpringLayout.NORTH,this);

lay.putConstraint(SpringLayout.WEST,txt6,520,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,txt6,280,SpringLayout.NORTH,this);



lay.putConstraint(SpringLayout.WEST,combo,520,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,combo,320,SpringLayout.NORTH,this);

lay.putConstraint(SpringLayout.WEST,lbl8,370,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,lbl8,320,SpringLayout.NORTH,this);

lay.putConstraint(SpringLayout.WEST,txt8,520,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,txt8,380,SpringLayout.NORTH,this);

lay.putConstraint(SpringLayout.WEST,lbl9,370,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,lbl9,380,SpringLayout.NORTH,this);



lay.putConstraint(SpringLayout.WEST,lbl10,370,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,lbl10,450,SpringLayout.NORTH,this);

lay.putConstraint(SpringLayout.WEST,txt9,520,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,txt9,450,SpringLayout.NORTH,this);

lay.putConstraint(SpringLayout.WEST,lbl11,370,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,lbl11,520,SpringLayout.NORTH,this);

lay.putConstraint(SpringLayout.WEST,txt10,520,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,txt10,520,SpringLayout.NORTH,this);

lay.putConstraint(SpringLayout.WEST,lbl12,370,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,lbl12,580,SpringLayout.NORTH,this);

lay.putConstraint(SpringLayout.WEST,txt11,520,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,txt11,580,SpringLayout.NORTH,this);

lay.putConstraint(SpringLayout.WEST,submit_btn,520,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,submit_btn,660,SpringLayout.NORTH,this);

lay.putConstraint(SpringLayout.WEST,reset_btn,620,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,reset_btn,660,SpringLayout.NORTH,this);

lay.putConstraint(SpringLayout.WEST,browse_btn,750,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,browse_btn,580,SpringLayout.NORTH,this);
setLayout(lay);
}
}

Is This A Good Question/Topic? 0
  • +

Replies To: to attach the attachment by using the jfile chooser

#2 sudhamadhuri   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 01-March 12

Re: to attach the attachment by using the jfile chooser

Posted 02 March 2012 - 12:26 AM

my problem is being solved.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1