Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Jlist doesn't update
    primarykey
    data
    text
    <p>I have a JList in a large gui, I cut out all other code not relevant. I can initialize the list but I can not add/remove items from it when I use a button. Everything I have read says to use a DefaultListModel. Then it will automatically update the list when you re initialize it.<br> I have also tried refocusing and repainting the panels and frame and that doesn't work either.</p> <pre><code>public static void main(String[] args) { Play worker=new Play(); worker.GUI(); } public void GUI(){ frame=new JFrame(); mainPanel=new JPanel(new GridBagLayout()); filePanel=new JPanel(new GridBagLayout()); allFilePanel=new JPanel(new GridBagLayout()); fileInputLabel=new JLabel("Enter the directory of the csv file:"); fileNameField=new JTextField(25); readFileButton=new JButton("Read File"); ReadFileButtonListener buttonListener=new ReadFileButtonListener(); readFileButton.addActionListener(buttonListener); addItem(filePanel,fileInputLabel , 0, 0, 1, 1, GridBagConstraints.CENTER); addItem(filePanel,fileNameField , 1, 0, 2, 1, GridBagConstraints.CENTER); addItem(filePanel,readFileButton , 3, 0, 1, 1, GridBagConstraints.CENTER); allFileLabel=new JLabel("List of all Data"); String [] name={"joe"}; allFileList=new JList&lt;String&gt;(name); allFileList.setVisibleRowCount(10); addItem(allFilePanel,allFileLabel , 0, 0, 1, 1, GridBagConstraints.CENTER); addItem(allFilePanel,allFileList , 0, 1, 1, 1, GridBagConstraints.CENTER); addItem(mainPanel, filePanel, 0, 0, 4, 1, GridBagConstraints.CENTER); addItem(mainPanel, allFilePanel, 1, 2, 1, 1, GridBagConstraints.CENTER); frame.setSize(1000,800); frame.add(mainPanel); frame.setVisible(true); } /**configures the panels * @param p * @param c * @param x * @param y * @param width * @param height * @param align */ private void addItem(JPanel p,JComponent c, int x, int y , int width,int height, int align){ GridBagConstraints gc=new GridBagConstraints(); gc.gridx=x; gc.gridy=y; gc.gridwidth=width; gc.gridheight=height; gc.weightx=100; gc.weighty=100; gc.insets=new Insets(5,5,5,5); gc.fill=GridBagConstraints.NONE; p.add(c,gc); } private class ReadFileButtonListener implements ActionListener{ @Override public void actionPerformed(ActionEvent arg0) { generateAllFileList(); } } public void generateAllFileList(){ DefaultListModel&lt;String&gt; list=new DefaultListModel&lt;String&gt;(); list.addElement("Fred"); allFileList=new JList&lt;String&gt;(list); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload