Hi, I created a program using the linkedlist package. I encountered an error when I tried to insert an element... By the way here's my code...
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
class guiListOperations3 extends JFrame implements ActionListener
{
static LinkedList <String> list=new LinkedList <String>();
static JLabel lblElement,lblDisplay,lblPos;
static JTextField txtInput1,txtDisplay1,txtDisplay2,txtPos;
static JButton btnAdd,btnRemove,btnPeek,btnCancel,btnClear;
static int pos=0;
public guiListOperations3()
{
Container drum=getContentPane();
drum.setLayout(new FlowLayout());
lblElement=new JLabel("Enter Element: ");
drum.add(lblElement);
txtInput1=new JTextField(10);
drum.add(txtInput1);
lblDisplay=new JLabel("List: ");
drum.add(lblDisplay);
txtDisplay1=new JTextField(10);
txtDisplay1.setEditable(false);
drum.add(txtDisplay1);
lblPos=new JLabel("Position:");
drum.add(lblPos);
txtPos=new JTextField(1);
drum.add(txtPos);
btnAdd=new JButton("Insert");
drum.add(btnAdd);
lblDisplay=new JLabel("To delete element:");
drum.add(lblDisplay);
btnRemove=new JButton("Remove");
drum.add(btnRemove);
lblDisplay=new JLabel("To view element:");
drum.add(lblDisplay);
btnPeek=new JButton("Peek");
drum.add(btnPeek);
lblDisplay=new JLabel("--------------------");
drum.add(lblDisplay);
btnClear=new JButton("Clear");
drum.add(btnClear);
btnCancel=new JButton("Cancel");
drum.add(btnCancel);
setSize(165,360);
setLocation(600,300);
setVisible(true);
btnAdd.addActionListener(this);
btnRemove.addActionListener(this);
btnPeek.addActionListener(this);
btnClear.addActionListener(this);
btnCancel.addActionListener(this);
}
public void actionPerformed(ActionEvent a)
{
String b;
if(a.getSource()==btnAdd)
if(txtInput1.getText().equals("")||txtInput1.getText().equals(null))
JOptionPane.showMessageDialog(null,"Invalid Entry!","Error",0);
else
{
pos=Integer.parseInt(txtPos.getText());
list.add(txtInput1.getText(),pos);
txtDisplay1.setText(""+list);
}
if(a.getSource()==btnRemove)
if((txtDisplay1.getText().equals(null))||(txtDisplay1.getText().equals(""))||(txtDisplay1.getText().equals("[]")))
{
JOptionPane.showMessageDialog(null,"List is empty!","Error",0);
}
else
{
list.remove();
txtDisplay1.setText(""+list);
}
if(a.getSource()==btnPeek)
if((txtDisplay1.getText().equals(null))||(txtDisplay1.getText().equals(""))||(txtDisplay1.getText().equals("[]")))
{
JOptionPane.showMessageDialog(null,"Error! List is empty!","Error",0);
}
else
JOptionPane.showMessageDialog(null,"The top value is "+list.peek());
if(a.getSource()==btnCancel)
{
setVisible(false);
guiMenu z=new guiMenu();
}
if(a.getSource()==btnClear)
{
txtInput1.setText("");
txtDisplay1.setText("");
}
}
public static void main(String[]args)
{
guiListOperations3 app=new guiListOperations3();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
I hope you can help me find solution and thanks in advance :)