Results 1 to 2 of 2
  1. #1
    rangvald is offline Member
    Join Date
    Nov 2009
    Posts
    1
    Rep Power
    0

    Default Cannot get my JList to update!

    Hello!

    I have a problem where my JList not updates after i have writen in new data in another GUI.

    This is the construction!

    WorkSiteFunc
    Handles data that should be displayed with the GUI all data that the GUI is viewing is get from here with the getAllData().

    WorkSiteGUI
    This shows a JList in a JScrollPane and some JButtons.
    One of the JButtons is "Create Newworks site"
    witch creates a WorkSite_MakeGUI.

    Haves a update function that should update the data in the JList.
    The data arrives and are there when i run the method but the JList dosent show the new WorkSite.

    WorkSite_MakeGUI
    This shows the window where you put in data in some JTextFields.
    When you press the ok JButton a function calls that adds the new
    data to the a list with all WorkSites in WorkSiteFunc class.
    And after that tells the WorkSiteGUI to update.

    Here is some code

    Java Code:
    package InTime;
    
    import javax.swing.*;
    import javax.swing.border.EmptyBorder;
    import javax.swing.event.ListSelectionEvent;
    import javax.swing.event.ListSelectionListener;
    
    import InTime.shared.WorkSite;
    
    import java.awt.*;
    import java.awt.TrayIcon.MessageType;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.*;
    public class WorkSiteGUI {
    	
    	private JLabel hourLabel,workSiteLabel;
    	private JTextField workSiteField,hourField;
    	private Vector<WorkSite> workSites = null;
    	private JButton okButton,submitButton,cancelButton, makeWorkSiteButton,deleteWorkSiteButton;
    	private String[] workSiteData;
    	private JScrollPane workSiteScrollPane;
    	private JList workSiteJList;
    	private JFrame win;
    	private JPanel contentPane, p1, p2, p3, p4, p5;
    
    	public WorkSiteGUI() {
    		workSites = InTime.getInstance().getWorkSiteFunc().getAllData();
    		makeWorkSiteData();
    		makeHUD();
    	}
    
    	private void makeHUD() {
    		
    		// Frame and ContentPane
    		contentPane = new JPanel(new BorderLayout());
    		
    		win = new JFrame("Skapa eller ändra Arbetsplats");
    		win.setLocation(0, 0);
    		win.setContentPane(contentPane);
    		win.setPreferredSize(new Dimension(500, 145));
    		win.setLocationRelativeTo(null);
    		win.setResizable(false);
    		win.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    
    		//BUTTONS
    		ActionListener buttonListener = new buttonListener();
    		okButton = new JButton("Ok");
    		okButton.setPreferredSize(new Dimension(93,25));
    		okButton.addActionListener(buttonListener);
    		
    		submitButton = new JButton("Verkställ");
    		submitButton.setPreferredSize(new Dimension(90,25));
    		submitButton.addActionListener(buttonListener);
    		
    		cancelButton = new JButton("Avbryt");
    		cancelButton.setPreferredSize(new Dimension(92,25));
    		cancelButton.addActionListener(buttonListener);
    		
    		makeWorkSiteButton = new JButton("Skapa Arbetsplats");
    		makeWorkSiteButton.setPreferredSize(new Dimension(140,25));
    		makeWorkSiteButton.addActionListener(buttonListener);
    		
    		deleteWorkSiteButton = new JButton("Tabort Arbetsplats");
    		deleteWorkSiteButton.setPreferredSize(new Dimension(140,25));
    		deleteWorkSiteButton.addActionListener(buttonListener);
    	
    		//Panel
    		p1 = new JPanel();
    		p1.setLayout(new BoxLayout(p1, BoxLayout.Y_AXIS));
    		p2 = new JPanel(new BorderLayout());
    		p3 = new JPanel(new FlowLayout());
    		p4 = new JPanel(new FlowLayout());
    		p5 = new JPanel(new FlowLayout());
    		
    		//Worksite List
    		workSiteJList = new JList(workSiteData);
    		workSiteJList.addListSelectionListener(new listListener());
    
    		workSiteScrollPane = new JScrollPane(workSiteJList);
    		workSiteScrollPane.setPreferredSize(new Dimension(75,100));
    		workSiteScrollPane.setMinimumSize(new Dimension(75,100));
    		workSiteScrollPane.setSize(new Dimension(75,200));
    		workSiteScrollPane.setAutoscrolls(true);
    		
    		// Label
    		workSiteLabel = new JLabel("Välj Arbetsplats");
    		workSiteLabel.setPreferredSize(new Dimension(140, 40));
    		hourLabel=new JLabel(" Timmar");
    		hourLabel.setPreferredSize(new Dimension(140, 40));
    		
    		// Text Field
    		workSiteField = new JTextField("t.ex  137 ");
    		workSiteField.setPreferredSize(new Dimension(70, 25));
    		hourField = new JTextField("t.ex  17 ");
    		hourField.setPreferredSize(new Dimension(70, 25));
    		
    		p1.add(workSiteLabel);
    		p1.add(workSiteScrollPane);
    		p1.setBorder(new EmptyBorder(0, 5, 0, 0));
    		
    		p3.add(hourLabel);
    		p3.add(hourField);
    		
    		p4.add(makeWorkSiteButton);
    		p4.add(deleteWorkSiteButton);
    		
    		p5.add(okButton);
    		p5.add(submitButton);
    		p5.add(cancelButton);
    		
    		p2.add(p3, BorderLayout.NORTH);
    		p2.add(p4, BorderLayout.CENTER);
    		p2.add(p5, BorderLayout.SOUTH);
    		
    		contentPane.add(p1, BorderLayout.WEST);
    		contentPane.add(p2, BorderLayout.CENTER);
    		
    		win.pack();
    		win.setVisible(true);
    	}
    	
    	/**
    	 * Creates the workSitesList data
    	 */
    	private void makeWorkSiteData()
    	{
    		int storlek = workSites.size();
    		workSiteData = new String[storlek];
    		for(int i = 0; i < storlek; i++)
    		{
    			int resnr = workSites.get(i).getResultatenhetsnr();
    			workSiteData[i] = Integer.toString(resnr);
    			System.out.println(resnr);
    		}
    	}
    	
    	/**
    	 * Updates the worksite and hour textfields
    	 * @param index the index of the selected worksite.
    	 */
    	private void updateFields(int index)
    	{
    		hourField.setText(Integer.toString(workSites.get(index).getHours()));
    		workSiteField.setText(Integer.toString(workSites.get(index).getResultatenhetsnr()));
    	}
    	
    	/**
    	 * A innerclass as that listens on the WorkSiteList.
    	 */
    	private class listListener implements ListSelectionListener
    	{
    		public void valueChanged(ListSelectionEvent e) 
    		{
    			JList temp = (JList) e.getSource();
    			ListSelectionModel lsm = temp.getSelectionModel();
    	        int selected;
    
    	        if (lsm.isSelectionEmpty()) {
    	        	 System.out.println(" <none>");
    	        } else {
    	            // Find out which indexes are selected.
    	            int minIndex = lsm.getMinSelectionIndex();
    	            int maxIndex = lsm.getMaxSelectionIndex();
    	            for (int i = minIndex; i <= maxIndex; i++) {
    	                if (lsm.isSelectedIndex(i)) {
    	                	selected = i;
    	                	 if(selected >= 0)
    	                	 {
    	                		 updateFields(selected);
    	                	 }
    	                }
    	            }
    	        }
    		}
    	}
    	
    	/**
    	 * Is used inside the inner class(es).
    	 * @return a reference to this WorkSite.
    	 */
    	private WorkSiteGUI getThis()
    	{
    		return this;
    	}
    	
    	public void update()
    	{
    		makeWorkSiteData();
    		Runnable doWorkRunnable = new Runnable() {
    		    public void run() 
    		    {
    		    	p1.revalidate();
    		    }
    		};
    		SwingUtilities.invokeLater(doWorkRunnable);
    	}
    	
    	/**
    	 * A innerclass that listen on the avbryt, ok, buttons
    	 */
    	private class buttonListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e) {
    			JButton button = (JButton)e.getSource();
    			if(button == okButton)
    			{
    				okButtonFunc();
    			}else if(button == submitButton)
    			{
    				submitButtonFunc();
    			} else if (button == cancelButton)
    			{
    				cancelButton();
    			} else if (button == makeWorkSiteButton)
    			{
    				makeWorkSite();
    			} else if (button == deleteWorkSiteButton)
    			{
    				deleteWorkSite();
    			}
    			
    			
    		}
    		
    		private void okButtonFunc()
    		{
    			
    		}
    		
    		private void submitButtonFunc()
    		{
    			
    		}
    		
    		private void cancelButton()
    		{
    			win.dispose();
    		}
    		
    		private void makeWorkSite()
    		{
    			new WorkSiteGUI_MakeWSGUI(getThis());
    		}
    		
    		private void deleteWorkSite()
    		{
    			
    		}
    	}
    
    }

  2. #2
    aaroncarpet's Avatar
    aaroncarpet is offline Senior Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    147
    Rep Power
    0

    Default

    do you mean a seperate instance of this gui? or a completely different gui

Similar Threads

  1. How to use a JList?
    By glhansen in forum New To Java
    Replies: 3
    Last Post: 03-24-2009, 11:27 AM
  2. JList
    By pinks_70986 in forum New To Java
    Replies: 1
    Last Post: 02-12-2009, 09:36 AM
  3. About JList
    By hungleon88 in forum Advanced Java
    Replies: 5
    Last Post: 08-30-2008, 10:24 PM
  4. searching within a JList
    By newtojava7 in forum New To Java
    Replies: 1
    Last Post: 03-10-2008, 01:12 AM
  5. Help with JList
    By Albert in forum NetBeans
    Replies: 1
    Last Post: 07-13-2007, 04:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •