How can i add and remove list by using JTextField So I can a

How can i add and remove list by using JTextField?

So, I can add and remove the list by using JTextField to write something to add it or remove it in JTextArea and JList.

Solution

the following JAVA code demonstrates the usgage of JTextField to add and remove elements.

import javax.swing.*;
import java.awt.*;
import javax.swing.event.*;
import java.awt.event.*;
class myfram extends JFrame implements ActionListener
{
JList l1;
JTextField t1;
JButton b1,b2,b3;
int ret;
DefaultListModel listModel;

myfram()
{
setTitle(\"Adding or Removing Items From JList\");
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
listModel = new DefaultListModel();
listModel.addElement(\"Item 1\");
listModel.addElement(\"Item 2\");
listModel.addElement(\"Item 3\");
listModel.addElement(\"Item 4\");
//creating J List
l1 = new JList(listModel);
//Creating Text Field
t1 = new JTextField(10);
//Creating Buttons
b1 = new JButton(\"ADD\");
b1.addActionListener(this);
b2 = new JButton(\"REMOVE\");
b2.addActionListener(this);
b2.setActionCommand(\"remove\");
b3 = new JButton(\"Clear All\");
b3.addActionListener(this);
b3.setActionCommand(\"clear\");
//adding jlist to a scrollpane
JScrollPane js = new JScrollPane(l1);
js.setPreferredSize(new Dimension(300,100));
add(js);
add(t1);
add(b1);
add(b2);
add(b3);
setSize(700,500);
setVisible(true);
}

public void actionPerformed(ActionEvent ae)
{
if (\"clear\".equals(ae.getActionCommand()))
{
listModel.removeAllElements();

}

else if(\"remove\".equals(ae.getActionCommand()))
{
ret = l1.getSelectedIndex();
listModel.remove(ret);
}
else
{
String select =t1.getText();
Object obj = select;
listModel.add(0,obj);

}
}

}
public class simplelist
{

public static void main(String[] args)
{

myfram fr = new myfram();
}
}

How can i add and remove list by using JTextField? So, I can add and remove the list by using JTextField to write something to add it or remove it in JTextArea
How can i add and remove list by using JTextField? So, I can add and remove the list by using JTextField to write something to add it or remove it in JTextArea

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site