This is the automatically created code when adding the Jlist in Netbeans design mode:
jListResult = new javax.swing.JList();
jListResult.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
jScrollPane1.setViewportView(jListResult);
I don't understand why an object of type Jlist would not be able to use the method .addElement How can I fix this?
addElement is provided by the DefaultListModel not the JList itself
DefaultListModel<String> model = new DefaultListModel<>();
JList jListResult = new JList(model);
model.addElement(...);
Related
So, I have this code in my JFrame, and it doesn't work for some reason:
private void jList1MouseEntered(java.awt.event.MouseEvent evt) {
DefaultListModel jList1Model = (DefaultListModel) jList1.getModel();
File f=new File("/home");
File[] allSubFiles=f.listFiles();
for (File file : allSubFiles) {
jList1Model.addElement(file.getAbsolutePath());
}
}
What am I doing wrong (ignore MouseEntered event, I'll change it)? It doensn't update anything when I hover over the active this list.
because It gives me an exception javax.swing.JList$3 cannot be cast to javax.swing.DefaultListModel
Don't you think that was an important piece of missing information from the question?
So basically that means you need to create your JList with code like:
DefaultListModel<String> model = new DefaultListModel<String>();
JList<String> list = new JList<String)(model);
Now you can dynamically try to add data to the model.
I'm having trouble adding items to a JList called lstContacts whenever a button is pressed.
When I press my new contact button, a line should be added to lstContacts but when I press the button nothing happens.
Here is my action listener for the new contact button:
newContactButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
Contacts contacts = new Contacts();
contacts.createContact();
}
});
Here is my createContact method in the contacts class:
ArrayList<String> contactsList = new ArrayList<String>();
public void createContact() {
ContactsDB database = new ContactsDB();
System.out.println("new contact");
contactsList.add("New Contact");
listModel.addElement("New Contact");
}
And lastly here is my createCustomUIComponent() method:
private void createUIComponents() {
// TODO: place custom component creation code here
listModel = new DefaultListModel();
lstContacts = new JList<String>(listModel);
}
Why aren't the new contacts getting added to my JList?
The problem is here:
Contacts contacts = new Contacts();
contacts.createContact();
You are creating a new contacts object every time you click the button, but the JList doesn't have a reference to the Contacts object. I don't know how the contacts object is defined, but you should make sure that the object you are adding the contact to has reference to the DefaultListModel.
Another problem I see (but isn't causing the problem you posted about) is that you call new DefaultListModel(), but list model is a parameterized type. You probably want to call new DefaultListModel<String>().
Don't create an ArrayList of Contacts. All the Contact data should be stored in the ListModel.
So when you click on the button all you do is create a Contact and add the Contact to the DefaultListModel. However, your code doesn't make much sense because you're just creating an empty Contact. I would expect the Contact to have a name or something. So really what you want to do is have a text field were the user can enter a name and then when you click the button you create a Contact using the name data and then add the Contact to the list.
Read the section from the Swing tutorial on How to Use Lists. The ListDemo example show you how to dynamically add/remove an item from the DefaultListModel. Download the example and modify the working example to meet your specific requirements. When learning a new concept, start with working code.
First of all you have to have new a default ListView:
DefaultListModel listModel = new DefaultListModel();
Thenm build your JList with this list:
JList jList = new JList(listModel);
Anytime you want to change your list for each action you considered, call
addElement on your jList:
listModel.addElement("This is a test contact");
Hi i am newbie to java and i am trying to add the item from JComboBox to the JList, but when i run the program, i am getting this error.
How to do this?
error:
cannot find symbol
symbol : method addElement(java.lang.String)
location: class javax.swing.JList
openTaskBox.addElement(taskItem);
code:
public static void addSelectedItemToTaskList(String taskItem)
{
openTaskBox.addElement(taskItem);
}
Here openTaskBox is JList.
Code:
JList openTaskBox = new JList();
openTaskBox.setPreferredSize(new Dimension(350, 50));
pnlInnerTL.add(openTaskBox);
For JComboBox
You can use JComboBox#addItem(E)
See How to use Combo Boxes for more details
For JList
You have to use the ListModel. DefaultListModel supplies a addElement method
See How to use lists for more details
Add the item to the JList's model, not to the JList itself.
DefaultListModel model = (DefaultListModel) openTaskBox.getModel();
model.addElement(taskItem);
I need some help about adding items to JList. I work on some "library" kind of project. And I need to add readers to already existing JList. But when I try to add it, JList just resets, removes all the readers and starts adding readers to a new blank JList. But I don't need it to make new list but add it to the already existing one.
I know it's something about creating new model after adding, but i don't know where to fix it.
panelHorni = new JPanel();
listModel = new DefaultListModel();
listCtenaru = new JList(listModel);
FileInputStream fis = new FileInputStream("myjlist.bin");
ObjectInputStream ois = new ObjectInputStream(fis);
listCtenaru = (JList)ois.readObject();
listScroll = new JScrollPane();
listScroll.add(listCtenaru);
listCtenaru.setPreferredSize(new Dimension(350, 417));
listCtenaru.setBackground(new Color(238,238,238));
panelHorni.add(listCtenaru);
listener
public void actionPerformed(ActionEvent e) {
String jmeno = pole1.getText();
String prijmeni = pole2.getText();
listModel.addElement(jmeno +" "+ prijmeni);
listCtenaru.setModel(listModel);
pole1.setText("");
pole2.setText("");
pole1.requestFocus();
listModel.addElement(jmeno +" "+ prijmeni);
//listCtenaru.setModel(listModel);
There is no need to use the setModel() method if you are trying to update the existing model. The fact that you are trying to do this would seen to indicate you are creating a new model instead of updating the existing model.
See the Swing tutorial on How to Use Lists for a working example that updates the existing model.
The default model of JList is ListModel you must firstly change it inside the constructor to DefaultListModel. That solves your problem:
private JList list ;
private DefaultListModel model;
public ListModelTest(){//default constructor
//....
list = new JList();
model = new DefaultListModel();
list.setModel(model);
//....
}
public void actionPerformed(ActionEvent ev){
model.addElement("element");
//....
}
Using the example TableFilterDemo, I'm trying to create a RowFilter in NetBeans, but I can't. I tried the code in JCreator; I need help.
I understand that I do not need to create class MyTableModel extends AbstractTableModel because I already manually did this in NetBeans GUI tools. Now, I face a problem in defining the model with RowSorter.
MyTableModel model = new MyTableModel();
sorter = new TableRowSorter<MyTableModel>(model);
table = new JTable(model);
table.setRowSorter(sorter);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);
Above is the example, and I change it to this:
private TableRowSorter<javax.swing.table.DefaultTableModel> sorter;
/** Creates new form NewJFrame */
public NewJFrame() {
initComponents();
javax.swing.table.DefaultTableModel model = new DefaultTableModel();
sorter = new TableRowSorter<javax.swing.table.DefaultTableModel>(model);
JTable jTable = new JTable(model);
jTable.setRowSorter(sorter);
Is it correct? I can't get this to work. I suspect is the sorter is not added into the table. Which maybe because of defining model part. Please give advice.
I think it is not nessesary to you create your abstract table model .
you just use defaultModel and use following code
voterTable.getColumnModel().getColumn(0).setPreferredWidth(65);
I supposed you create the JTable in the GUI Builder. You have too add the model to this table and not to a new one you create.
Replace the method with :
public NewJFrame() {
javax.swing.table.DefaultTableModel model = new DefaultTableModel();
sorter = new TableRowSorter<javax.swing.table.DefaultTableModel>(model);
initComponents();
}
And add the rowsorter from the GUI Builder (will be added in initComponent()):
Right click on the JTable -> Properties -> Pane "Code"
In Custom Creation code write -> new JTable(model);
In Variable name you will see the variable name ([table_name] in following point)
In Post Creation Code write -> [table_name].setRowSorter(sorter);