I have to link an excel file with my application software using java swing.As soon as i click on the link button a window should open for selecting the excel file(just like when we search for file to upload)and i should be able to select the excel file and link it with the tool.The excel file will contain some questions for survey.After linking the tool must be able to take those questions and generate a template containing the questions.please help me with the code!
import java.io.*; import java.awt.*; import javax.swing.*; import java.awt.event.*; import javax.swing.filechooser.*; public class SelectFile extends JFrame{ public static void main(String[]args){ JFrame frame = new JFrame(); frame.setLayout(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(400, 100); Container container = frame.getContentPane(); container.setLayout(new GridBagLayout()); final JTextField text=new JTextField(20); JButton b=new JButton("Select"); text.setBounds(20,20,120,20); b.setBounds(150,20,80,20); b.setText("<html><font color='blue'><u>Select File</u></font></html>"); b.setHorizontalAlignment(SwingConstants.LEFT); b.setBorderPainted(false); b.setOpaque(false); b.setBackground(Color.lightGray); b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e){ JFileChooser fc = new JFileChooser(); fc.addChoosableFileFilter(new OnlyExt()); int returnval = fc.showOpenDialog(null); if (returnval == JFileChooser.APPROVE_OPTION) { File file = fc.getSelectedFile(); text.setText(file.getPath()); } } }); container.add(text); container.add(b); frame.setVisible(true); } } class OnlyExt extends javax.swing.filechooser.FileFilter{ public boolean accept(File file) { if (file.isDirectory()) return false; String name = file.getName().toLowerCase(); return (name.endsWith(".xls")); } public String getDescription() { return "Excel ( *.xls)"; } }
Thanks for the code.But this code is only able to open a Jpanel to select the file.After I select the file nothing is happening.I wanted to be able to generate a template based on the questions that are in the excel file(linking the file).could you please help me with this?
Ads