My Programme Not working .. as i used VECTOR and SWING.
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Vector;
import java.util.Enumeration;
import java.util.NoSuchElementException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class vector1 extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JLabel jlbString = new JLabel("Enter a String");
public vector1(){
super("Vector Class Program");
//Made final as it can be accessed by the inner classes
final JLabel jlbStatus = new JLabel();
Container contentPane = getContentPane();
final Vector<String> vector = new Vector<String>(1);
contentPane.setLayout(new FlowLayout());
contentPane.add(jlbString);
final JTextField jtfInput = new JTextField();
contentPane.add(jtfInput);
JButton jbnAdd = new JButton("Add");
jbnAdd.addActionListener( new ActionListener(){
public void actionPerformed(ActionEvent e){
vector.addElement(jtfInput.getText().trim()) ;
jlbStatus.setText("Append to end: " + jtfInput.getText().trim());
jtfInput.setText(" ");
}
});
contentPane.add(jbnAdd);
JButton jbnRemove = new JButton("Remove");
jbnRemove.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//Returns true if element is in vector
if(vector.removeElement(jtfInput.getText().trim()) )
jlbStatus.setText("removed: " + jtfInput.getText().trim() + " not in vector");
}
});
contentPane.add(jbnRemove);
JButton jbnFirst = new JButton();
jbnFirst.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
try{
jlbStatus.setText("First elemnt: " + vector.firstElement());
}catch(NoSuchElementException exception){
jlbStatus.setText(exception.toString());
}
}
});
contentPane.add(jbnFirst);
JButton jbnLast = new JButton("Last");
jbnLast.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
jlbStatus.setText("Last Element" + vector.lastElement());
}catch(NoSuchElementException exception){
jlbStatus.setText(exception.toString());
}
}
});
contentPane.add(jbnLast);
JButton jbnEmpty = new JButton("Empty Check");
jbnEmpty.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
jlbStatus.setText(vector.isEmpty() ? "Vector is Empty" : "Vector is not Empty");
}
});
contentPane.add(jbnEmpty);
JButton jbnContains = new JButton();
jbnContains.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String searchKey = jtfInput.getText().trim();
if(vector.contains(searchKey)){
jlbStatus.setText("Vector contains" + searchKey);
}else{
jlbStatus.setText("vector Does not contain " + searchKey);
}
}
});
contentPane.add(jbnContains);
JButton jbnFindElement = new JButton("Find");
jbnFindElement.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
jlbStatus.setText("Element found at location " + vector.indexOf(jtfInput.getText().trim()));
}
});
contentPane.add(jbnFindElement);
JButton jbnTrim = new JButton("Trim");
jbnTrim.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
vector.trimToSize();
jlbStatus.setText("vector trimmmed to size");
}
});
contentPane.add(jbnTrim);
JButton jbnSize = new JButton("Size/Capacity");
jbnSize.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
jlbStatus.setText("Size = " + vector.size() + " ; Capacity = " + vector.capacity());
}
});
contentPane.add(jbnSize);
JButton jbnDisplay = new JButton("Dispaly");
jbnDisplay.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
Enumeration<String> enum1 = vector.elements();
StringBuffer buff = new StringBuffer();
while(enum1.hasMoreElements())
buff.append(enum1.nextElement()).append(" ");
JOptionPane.showMessageDialog(null, buff.toString(), "Contents of vector" , JOptionPane.PLAIN_MESSAGE);
}
});
contentPane.add(jbnDisplay);
contentPane.add(jlbStatus);
}
public static void main(String [] args){
vector1 vector1 = new vector1();
vector1.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
}
It gives error....
vector1.java uses unsafe and unchecked operations.
recompile with -Xlint:unchecked for details.
wat does that mean... why my prgrammes is not running as it dosen;t have any errors or warnings.
I tried to run it through eclipse. still the program gets terminated before running.
please help me guyz....
Re: My Programme Not working .. as i used VECTOR and SWING.
Did you try compiling the program with the javac option: -Xlint to find the statements that the compiler is concerned about?
The warning message is because you are not using the generics syntax to define a class like Vector.
If you only get warnings when you compile the program, there should be a class file created that you can execute. Check that a .class file was created.
How are you executing the program?
Please Edit your post and wrap your code with
[code=java]
<YOUR CODE HERE>
[/code]
to get highlighting and preserve formatting.
Re: My Programme Not working .. as i used VECTOR and SWING.
[Code = java]
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Vector;
import java.util.Enumeration;
import java.util.NoSuchElementException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class vector1 extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JLabel jlbString = new JLabel("Enter a String");
public vector1(){
super("Vector Class Program");
//Made final as it can be accessed by the inner classes
final JLabel jlbStatus = new JLabel();
Container contentPane = getContentPane();
final Vector<String> vector = new Vector<String>(1);
contentPane.setLayout(new FlowLayout());
contentPane.add(jlbString);
final JTextField jtfInput = new JTextField();
contentPane.add(jtfInput);
JButton jbnAdd = new JButton("Add");
jbnAdd.addActionListener( new ActionListener(){
public void actionPerformed(ActionEvent e){
vector.addElement(jtfInput.getText().trim()) ;
jlbStatus.setText("Append to end: " + jtfInput.getText().trim());
jtfInput.setText(" ");
}
});
contentPane.add(jbnAdd);
JButton jbnRemove = new JButton("Remove");
jbnRemove.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//Returns true if element is in vector
if(vector.removeElement(jtfInput.getText().trim()) )
jlbStatus.setText("removed: " + jtfInput.getText().trim() + " not in vector");
}
});
contentPane.add(jbnRemove);
JButton jbnFirst = new JButton();
jbnFirst.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
try{
jlbStatus.setText("First elemnt: " + vector.firstElement());
}catch(NoSuchElementException exception){
jlbStatus.setText(exception.toString());
}
}
});
contentPane.add(jbnFirst);
JButton jbnLast = new JButton("Last");
jbnLast.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
jlbStatus.setText("Last Element" + vector.lastElement());
}catch(NoSuchElementException exception){
jlbStatus.setText(exception.toString());
}
}
});
contentPane.add(jbnLast);
JButton jbnEmpty = new JButton("Empty Check");
jbnEmpty.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
jlbStatus.setText(vector.isEmpty() ? "Vector is Empty" : "Vector is not Empty");
}
});
contentPane.add(jbnEmpty);
JButton jbnContains = new JButton();
jbnContains.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String searchKey = jtfInput.getText().trim();
if(vector.contains(searchKey)){
jlbStatus.setText("Vector contains" + searchKey);
}else{
jlbStatus.setText("vector Does not contain " + searchKey);
}
}
});
contentPane.add(jbnContains);
JButton jbnFindElement = new JButton("Find");
jbnFindElement.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
jlbStatus.setText("Element found at location " + vector.indexOf(jtfInput.getText().trim()));
}
});
contentPane.add(jbnFindElement);
JButton jbnTrim = new JButton("Trim");
jbnTrim.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
vector.trimToSize();
jlbStatus.setText("vector trimmmed to size");
}
});
contentPane.add(jbnTrim);
JButton jbnSize = new JButton("Size/Capacity");
jbnSize.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
jlbStatus.setText("Size = " + vector.size() + " ; Capacity = " + vector.capacity());
}
});
contentPane.add(jbnSize);
JButton jbnDisplay = new JButton("Dispaly");
jbnDisplay.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
Enumeration<String> enum1 = vector.elements();
StringBuffer buff = new StringBuffer();
while(enum1.hasMoreElements())
buff.append(enum1.nextElement()).append(" ");
JOptionPane.showMessageDialog(null, buff.toString(), "Contents of vector" , JOptionPane.PLAIN_MESSAGE);
}
});
contentPane.add(jbnDisplay);
contentPane.add(jlbStatus);
}
public static void main(String [] args){
vector1 vector1 = new vector1();
vector1.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
}
[/code]
i tried to compile with javac ....
i don't know how to compile using CXint option.
yeah the class file is created ..but more than 1 class file is created. one is vector1.class and rest are vector1$1 .......to .....vector1$11.. i don;t know y..
Re: My Programme Not working .. as i used VECTOR and SWING.
The classnames with the $ in them are for inner classes.
What warnings did the compiler output when you used the -Xlint option?
Please Edit your post and wrap your code with
[code=java]
<YOUR CODE HERE>
[/code]
to get highlighting and preserve formatting.