Create a basic GUI Start with a JFrame! Which, obviously makes this an applicati
ID: 3529550 • Letter: C
Question
Create a basic GUI Start with a JFrame! Which, obviously makes this an application and not an applet. application should show three components, a JButton, a JTextField, and a JTextArea Clicking on the JButton opens a JFileChooser which allows you to select a Java source code file. Check and make sure the file ends in .java and is readable before continuing. If not, raise a dialog box to inform the user of the problem and returns them to your application so they can elect to click the button again.. The JTextField shows the full path (e.g: c:/log/Log.java) to the selected file. The JTextArea shows the results. The business process is simple. Read the selected file and return a count of the Java Keywords in it. Use a Set<> to store all the Java keywords, there are only 50 of them. Ignore any key words that are in comments or enclosed in a String. For example 'int' is a java keyword, but you would ignore //int or "list an int for display" as 'int' is embedded in a comment and a String respectively. The count is displayed in a list on your GUI screen in the JTextArea box. Use a Map<> for your results. The key and item dataTypes should be obvious. Make sure the list is ordered, natural ordering is fine. If you select a new source code file, the text area is blanked and the count for the new file is displayed. Pay close attention to coding conventions, indenting, comments, and naming conventions. Submit your Java and class files to this dropbox.Explanation / Answer
Hey this is the exact answer for your question..Check it out..But keep in mind that your java file must well formed it means there must space between every literal used in code Like: for ( int i=0 ; i < 10 ; i++) .. atleast try to give a space after every key word you use in your code..Man its a lot of work ...Plz rate it..
Code Begins here
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JDialog;
import javax.swing.filechooser.*;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JTextArea;
import java.awt.event.*;
import java.awt.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.HashSet;
public class JFrameTest {
JFrame frame= null;
JTextField textField= null;
JButton button= null;
JLabel label= null;
JDialog dialog= null;
JFileChooser fileChooser = null;
FileFilter filter= null;
JTextArea textArea= null;
String readString="";
private Set<String> javaKeyWords = new HashSet<String>(50);
private Map<Integer,String> javakeyWordFound = new HashMap<Integer,String>();
/**
* @param args
*/
public JFrameTest()
{
super ();
javaKeyWords.add("private");
javaKeyWords.add("public");
javaKeyWords.add("protected");
javaKeyWords.add("static");
javaKeyWords.add("final");
javaKeyWords.add("volatile");
javaKeyWords.add("transient");
javaKeyWords.add("synchronized");
javaKeyWords.add("native");
javaKeyWords.add("abstract");
javaKeyWords.add("try");
javaKeyWords.add("catch");
javaKeyWords.add("finally");
javaKeyWords.add("throw");
javaKeyWords.add("throws");
javaKeyWords.add("for");
javaKeyWords.add("while");
javaKeyWords.add("do");
javaKeyWords.add("if");
javaKeyWords.add("else");
javaKeyWords.add("switch");
javaKeyWords.add("case");
javaKeyWords.add("break");
javaKeyWords.add("continue");
javaKeyWords.add("return");
javaKeyWords.add("default");
javaKeyWords.add("byte");
javaKeyWords.add("char");
javaKeyWords.add("short");
javaKeyWords.add("int");
javaKeyWords.add("long");
javaKeyWords.add("float");
javaKeyWords.add("double");
javaKeyWords.add("boolean");
javaKeyWords.add("import");
javaKeyWords.add("class");
javaKeyWords.add("extends");
javaKeyWords.add("implements");
javaKeyWords.add("interface");
javaKeyWords.add("void");
javaKeyWords.add("instanceof");
javaKeyWords.add("strictfp");
javaKeyWords.add("enum");
javaKeyWords.add("this");
javaKeyWords.add("super");
javaKeyWords.add("const");
javaKeyWords.add("goto");
javaKeyWords.add("new");
javaKeyWords.add("package");
javaKeyWords.add("assert");
frame= new JFrame();
frame.setTitle("Sample JFrame Application");
frame.setSize(800,800);
JPanel jp= new JPanel();
dialog= new JDialog();
label= new JLabel("Please select the file to Upload");
textField= new JTextField(20);
button = new JButton("Upload Java File");
textArea= new JTextArea(40,40);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
fileChooser = new JFileChooser();
filter= new FileFilter() {
@Override
public String getDescription() {
// TODO Auto-generated method stub
return "Java files (*.java)";
}
@Override
public boolean accept(File f) {
// TODO Auto-generated method stub
return f.getName().toLowerCase().endsWith(".java")||f.isDirectory();
}
};
File sFile = null;
fileChooser.setFileFilter(filter);
int result =fileChooser.showSaveDialog(frame);
if(result == JFileChooser.APPROVE_OPTION){
sFile = fileChooser.getSelectedFile();
FileFilter selectedFilter = fileChooser.getFileFilter();
String file_name = sFile.getName();
String file_path = sFile.getParent();
textField.setText(file_path+"\"+file_name);
}
try
{
Scanner sc= new Scanner(sFile);
int j=0;
while ( sc.hasNext())
{
String s=sc.nextLine();
String[] s1=s.split(" ");
for ( int i=0;i<(s1.length);i++)
{
System.out.println(s1[i]);
if(javaKeyWords.contains(s1[i]))
{
javakeyWordFound.put(j,s1[i]);
j++;
}
}
}
for(int k=0;k<j;k++)
{
textArea.append(" "+k+" ---- "+javakeyWordFound.get(k));
}
}
catch(FileNotFoundException fe1)
{
JOptionPane.showMessageDialog(frame, "File Not Found may be due to access Problem.",
"Error", JOptionPane.ERROR_MESSAGE);
}
}
});
jp.add(label);
jp.add(button);
jp.add(textField);
jp.add(textArea);
frame.add(jp);
frame.setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new JFrameTest();
}
});
}
}