Results 1 to 15 of 15
  1. #1
    nikolas is offline Member
    Join Date
    Jan 2015
    Posts
    11
    Rep Power
    0

    Question How to update 4 jlist in one jpanel

    Hello,

    I use eclipse luna with database HSQLDB. I'm beginner in java. I use design pattern MVC. I can not find an example on google. Recover data from a database and update multiple JList depending on the selection

    I have 4 table in HSQLDB with foreign key and primary key

    I have 4 jlist into one jpanel
    department: I have a class department and a class departmentCellRenderer departmentListMode
    Courses: I have a class Courses and a class CoursesCellRenderer CoursesListMode
    Student: I have a class Student and a class StudentCellRenderer StudentListMode
    Grade: I have a class Grade and a class GradeCellRenderer GradeListMode

    When I select an item in Jlist(or more item) department, I update Jlist Course...When I select an item in Courses, I update Jlist Student...
    : department -> Courses -> Student -> Grade sheet of courses

    My JFrame:
    Java Code:
    package IHM;
    
    import java.awt.*;
    import java.awt.event.*;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    
    import javax.swing.*;
    
    import Fabriques.*;
    
    /*
    import org.hsqldb.*;
     */
    public class GestionNotesEtudiantsJDBC {
    
    	static Connection conn;
    	/** procedure de lancement du programme 
    	 * @throws Exception **/	
    	public static void main(String [] args) throws Exception {
    	
    		Class.forName("org.hsqldb.jdbcDriver");
    		conn=DriverManager.getConnection("jdbc:hsqldb:file:BDD/bddNotes","sa","");
    
    		FabFiliere.getInstance().demarrerConnexion(conn);
    		
    		//creation de la fenetre principale
    		JFrame frame = new JFrame("Student Notes Management");
    		frame.getContentPane().add(new PanelGeneral(frame), BorderLayout.CENTER);
    		
    		// mise en place d'un observateur de cette fenetre - pour le exit 
    		frame.addWindowListener(new WindowAdapter() {
    			public void windowClosing(WindowEvent e) {
    				try {
    					conn.close();
    				} catch (SQLException e1) {
    				}
    				System.exit(0);
    			}
    		});
    		frame.pack();
    		frame.setVisible(true);
    	}//fin main
    
    }//fin GestionNotesEtudiantsJDBC
    My main Jpanel:
    Java Code:
    package IHM;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.sql.SQLException;
    
    import javax.swing.JFrame;
    import javax.swing.JList;
    import javax.swing.JPanel;
    import javax.swing.event.ListSelectionEvent;
    import javax.swing.event.ListSelectionListener;
    
    import Donnees.Department;
    import Donnees.DepartmentCellRenderer;
    import Donnees.DepartmentListModel;
    import Metier.Metier;
    
    
    public class PanelGeneral extends JPanel implements ActionListener, ListSelectionListener {
    
    	JFrame pere;
    	JList<Department> jList1= new JList<Department> ();
    	JList<Courses> jList2= new JList<Courses> ();
    	JList<Student> jList3= new JList<Student> ();
    	JList<Grade> jList4= new JList<Grade> ();
    	
    	private javax.swing.JScrollPane jScrollPane1;
    	private javax.swing.JScrollPane jScrollPane2;
    	private javax.swing.JScrollPane jScrollPane3;
    	private javax.swing.JScrollPane jScrollPane4;
    	Metier m;
    	
    	public PanelGeneral (JFrame pere) throws Exception{
    
            jScrollPane1 = new javax.swing.JScrollPane();
            jScrollPane2 = new javax.swing.JScrollPane();
            jList2 = new javax.swing.JList();
            jScrollPane3 = new javax.swing.JScrollPane();
            jList3 = new javax.swing.JList();
            jScrollPane4 = new javax.swing.JScrollPane();
            jList4 = new javax.swing.JList();
    
            jScrollPane1.setViewportView(jList1);
            
            
            jList1.setCellRenderer(new FiliereCellRenderer());
            jList1.setModel(new FiliereListModel());
            jList1.addListSelectionListener(this);
    
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
            this.setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(18, 18, 18)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
        }// </editor-fold>                        
      
    	@Override
    	public void valueChanged(ListSelectionEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    	
    	public void refresh() throws SQLException{
    		jList1.setModel(new FiliereListModel());
    	}
    }
    Thanks for your help,

    Nikolas

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: How to update 4 jlist in one jpanel

    You haven't said what the problem is. What does your program do or don't do. What do you want it to do different?
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    nikolas is offline Member
    Join Date
    Jan 2015
    Posts
    11
    Rep Power
    0

    Default Re: How to update 4 jlist in one jpanel

    Hello SurfMan,

    Thanks for your answer,

    This is an implementation problem. I do not know how to create the mechanism to update my items, including the fact that when I select an item, I update the next item, and so on. I am looking for a example on how to deal with a database.

    Thanks,

    Nikolas

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: How to update 4 jlist in one jpanel

    You need to split this challenge up in two parts:
    1) How do I "chain" the JList clicks to eachother
    2) How do I access the database.

    Challenge 1 can be split up in two sub-challenges:
    1.1) Listening to selection changes in a single JList
    1.2) Making the JLists respond to eachother

    For challenge 1.1, you could add a ListSelectionListener that responds to selection changes in your JList. Based on the selected item, you can then update the second/third/fourth JList with new information. A basic implementation of a ListSelectionListener looks like this:
    Java Code:
    myList.addListSelectionListener(new ListSelectionListener() {
                @Override
                public void valueChanged(ListSelectionEvent e) {
                    if (!e.getValueIsAdjusting()) {
                        //either use myList.getSelectedValue()
                        //or more generic, but more verbose:
                        int selectedIndex = e.getFirstIndex();
                        ((JList)e.getSource).getModel().getElementAt(selectedIndex);
                    }
                }
            });
    Tutorial/examples here.

    Challenge 1.2 requires you to implement 1.1 for each JList that updates the next one.

    For challenge 2, accessing a database, there would be more than enough examples around on the interwebs. For example, on the site of HQSLDB iteself.

    Try to make a little test program for each challenge. When you successfully made the programs and you fully understand how it works, go and combine them all.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  5. #5
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: How to update 4 jlist in one jpanel

    Update: I realise that you are using some graphical editor to create your UI. This will add a layer of complexity to your program, since both you and your editor needs to understand what's going on. I would advise you to start learning creating Swing UIs by hand. It might look tedious at first, but once you have a good grasp of how to work with various LayoutManagers, it's much more flexible to work with. Some of the interesting LayoutManagers are BorderLayout, GridLayout, and GridBagLayout. (Forget GroupLayout and SpringLayout). Others might have suggestions too, everybody has their favourite LayoutManager. I personally LOVE MigLayout. /shameless_plug
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  6. #6
    nikolas is offline Member
    Join Date
    Jan 2015
    Posts
    11
    Rep Power
    0

    Default Re: How to update 4 jlist in one jpanel

    thanks for your help and your advice.

    At the beginning I start my project without graphical editor but I have so struggled to put my objects that I went on my netbeans to create objects and put my code in Eclipse. I find Netbean is better than eclipse for the GUI part.

    Nikolas

  7. #7
    nikolas is offline Member
    Join Date
    Jan 2015
    Posts
    11
    Rep Power
    0

    Default Re: How to update 4 jlist in one jpanel

    Hello SurfMan

    I notice that you are good in Java. Let me ask you two additional questions. My questions concern the JPanel.

    I have a button in a JPanel and I would like that opens an already existing JPanel. This JPanel is in my project in a sub folder HMI. I am looking for the command to do so.

    Is it possible to transmit the select item of this JPanel to another JPanel ? How to do this ?

    Thanks,

    Nikolas

  8. #8
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: How to update 4 jlist in one jpanel

    Answer for question #1:
    If you want to open a popup style window with the existing JPanel, you can create a new JDialog object with the current JFrame as parent and modal parameter true in the constructor, add the JPanel to it and call setVisible(true).
    In your button's ActionListener:
    Java Code:
    YourHMIPanel panel = new YourHMIPanel();
    JDialog d = new JDialog(myJFrame, true );
    d.add(panel);
    d.setVisible(true);
    As you probably won't have a reference to the current JFrame to work with, you can use the ever awesome SwingUtilities class to find it:
    Java Code:
    JFrame myCurrentFrame = SwingUtilities.getRoot( componentOnThatFrame );
    Answer to question #2: "Transmitting" objects can be done by passing the reference via a constructor or method. Consider this example:
    Java Code:
    public class Example {
        public static void main(String[] args) {
            Baz baz = new Baz();
            Foobar fb = new Foobar(baz);
        }
    }
    
    class Foobar {
        private Baz bazzaz;
    
        public Foobar(Baz b) {
            this.bazzaz = b;
        }
    
        public void setBazzaz(Baz bazzaz) {
            this.bazzaz = bazzaz;
        }
    }
    
    class Baz {
        
    }
    As you can see in the example above, I create an instance of Baz and use that as parameter in the constructor of the object FooBar I create. You can also use a method to give FooBar an instance of Baz. In your case, I think using a method on the "receiving" JPanel is more appropiate, since the JPanel is most likely already created.

    A more extensive example is below. Here is what happens:
    • Create the first panel. It has a field for the second panel we can use later.
    • Create the scond panel. It has a method other objects can call.
    • Pass the instance of the second panel to the first panel so it has access to its methods.


    A bit more advanced, but better maintainable, is the use of a controller class that supervises all the passing of data, but that would make the example even bigger. I can cook up an example of that if you want.

    Java Code:
    import javax.swing.*;
    import javax.swing.event.ListSelectionEvent;
    import javax.swing.event.ListSelectionListener;
    import java.awt.*;
    
    public class TwoPanels {
        public static void main(String[] args) {
            JFrame f = new JFrame("Two list example");
            f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            f.setLayout(new GridLayout(1, 2));
    
    
            FirstPanel firstPanel = new FirstPanel();
            SecondPanel secondPanel = new SecondPanel();
            firstPanel.setNextPanel(secondPanel);
    
            f.add(firstPanel);
            f.add(secondPanel);
            f.pack();
            f.setVisible(true);
        }
    }
    
    class FirstPanel extends JPanel {
        private SecondPanel nextPanel;
    
        public FirstPanel() {
            setLayout(new BorderLayout());
    
            final JList<String> list = new JList<String>(new String[]{"Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6", "Item 7", "Item 8" });
            add(new JScrollPane(list), BorderLayout.CENTER);
    
            list.addListSelectionListener(new ListSelectionListener() {
                @Override
                public void valueChanged(ListSelectionEvent e) {
                    if (!e.getValueIsAdjusting()) {
                        nextPanel.addToList(list.getSelectedValue());
                    }
                }
            });
        }
    
        public void setNextPanel(SecondPanel nextPanel) {
            this.nextPanel = nextPanel;
        }
    }
    
    class SecondPanel extends JPanel {
    
        private DefaultListModel<String> listModel;
    
        public SecondPanel() {
            setLayout(new BorderLayout());
    
            listModel = new DefaultListModel<String>();
            JList<String> list = new JList<String>(listModel);
    
            add(new JScrollPane(list), BorderLayout.CENTER);
        }
    
        public void addToList(String s) {
            listModel.addElement(s);
        }
    
    }
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  9. #9
    nikolas is offline Member
    Join Date
    Jan 2015
    Posts
    11
    Rep Power
    0

    Default Re: How to update 4 jlist in one jpanel

    Thanks for your help. Nikolas

  10. #10
    nikolas is offline Member
    Join Date
    Jan 2015
    Posts
    11
    Rep Power
    0

    Default Re: How to update 4 jlist in one jpanel

    Hello SurfMan

    I got through the opening portion of a JPanel but I block against the passing of value between the two panel. Can you show me how?

    Panel 1, Gestion
    Java Code:
    package IHM;
    
    import java.awt.BorderLayout;
    
    public class Gestion extends JFrame {
    
    	public String test;
    	private JPanel contentPane;
    	
    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					Gestion frame = new Gestion();
    					frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
    
    	/**
    	 * Create the frame.
    	 * @throws Exception 
    	 */
    	public Gestion() throws Exception {
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 450, 300);
    		contentPane = new JPanel();
    		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		contentPane.setLayout(new BorderLayout(0, 0));
    		setContentPane(contentPane);
    		
    		final PanelUE panel2 = new PanelUE();
    		
    		JPanel panel = new JPanel();
    		contentPane.add(panel, BorderLayout.NORTH);
    		
    		String choix[] = {" Pierre", " Paul", " Jacques", " Lou", " Marie"};
    		final JList list = new JList(choix);
    		panel.add(list);
    		
    		list.addListSelectionListener(new ListSelectionListener() {
    			public void valueChanged(ListSelectionEvent arg0) {
    				test = (String) list.getSelectedValue();
    				System.out.println(test);
    			}
    		});
    		
    			
    		JPanel panel_1 = new JPanel();
    		contentPane.add(panel_1, BorderLayout.SOUTH);
    		
    		JButton btnNewButton = new JButton("New button");
    		btnNewButton.addMouseListener(new MouseAdapter() {
    			@Override
    			public void mouseClicked(MouseEvent arg0) {
    				JDialog d = new JDialog();
    				d.getContentPane().add(panel2);
    				d.setVisible(true);
    				d.setBounds(500, 400, 500, 400);				
    			}
    		});
    		panel_1.add(btnNewButton);
    		
    		
    		
    	}
    
    }
    Panel 2, PanelUE
    Java Code:
    package IHM;
    
    import javax.swing.JPanel;
    
    public class PanelUE extends JPanel {
    	System.out.println(test);
    	/**
    	 * Create the panel.
    	 */
    	public PanelUE() {
    		FlowLayout flowLayout = (FlowLayout) getLayout();
    		flowLayout.setAlignOnBaseline(true);
    		setForeground(Color.WHITE);
    		setBorder(new LineBorder(new Color(0, 0, 0), 2));
    		
    		
    		JPanel panel = new JPanel();
    		add(panel);
    		
    		JList list = new JList();
    		panel.add(list);
    
    	}
    
    }
    Thanks,

    Nikolas
    Last edited by nikolas; 01-12-2015 at 02:13 PM.

  11. #11
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: How to update 4 jlist in one jpanel

    If PanelUE is the one that should receive the value, then you should create a method that accepts this value:

    Java Code:
    import javax.swing.*;
    import javax.swing.border.LineBorder;
    import java.awt.*;
    
    public class PanelUE extends JPanel {
        System.out.println(test); //<-------------------- how does this compile???????
        /**
         * Create the panel.
         */
        public PanelUE() {
            FlowLayout flowLayout = (FlowLayout) getLayout();
            flowLayout.setAlignOnBaseline(true);
            setForeground(Color.WHITE);
            setBorder(new LineBorder(new Color(0, 0, 0), 2));
    
    
            JPanel panel = new JPanel();
            add(panel);
    
            JList list = new JList();
            panel.add(list);
            
        }
    
        public void setValue(String someValue) {
            //What does PanelUE need to do with this value?
        }
    
    }
    Then make sure the first list calls this method:
    Java Code:
    list.addListSelectionListener(new ListSelectionListener() {
           public void valueChanged(ListSelectionEvent arg0) {
                test = (String) list.getSelectedValue();
                System.out.println(test);
                panel2.setValue(test);
            }
         });
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  12. #12
    nikolas is offline Member
    Join Date
    Jan 2015
    Posts
    11
    Rep Power
    0

    Default Re: How to update 4 jlist in one jpanel

    No, this doesn't run. ;-)

  13. #13
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: How to update 4 jlist in one jpanel

    Have you seen the addition I made in class PanelUE?
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  14. #14
    nikolas is offline Member
    Join Date
    Jan 2015
    Posts
    11
    Rep Power
    0

    Default Re: How to update 4 jlist in one jpanel

    Hello,

    Sorry I just saw your message.
    I tested two methods.

    public class PanelUE extends JPanel {
    System.out.println(test);
    Java Code:
    Message:
    Multiple markers at this line
    	- Syntax error on token "test", VariableDeclaratorId expected after 
    	 this token
    	- Syntax error on token(s), misplaced construct(s)

    public PanelUE() {
    System.out.println(test);
    Message:
    [CODE]Create local variable[CODE]

    Nikolas

  15. #15
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: How to update 4 jlist in one jpanel

    Delete that line. That is not MY code, this is your own code! In post #10 you have pasted this code:

    Java Code:
    package IHM;
     
    import javax.swing.JPanel;
     
    public class PanelUE extends JPanel {
        System.out.println(test);
        /**
         * Create the panel.
         */
    <snip>
    That line #6 is wrong. That's not supposed to be there, but you pasted it. Hence my comment that this doesn't compile.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. Trying to update a jList
    By kmccmk9 in forum AWT / Swing
    Replies: 3
    Last Post: 11-07-2013, 06:26 AM
  2. Update JList problem
    By ostkanten in forum New To Java
    Replies: 5
    Last Post: 09-25-2012, 04:53 AM
  3. Update Jlist in JFrame
    By dragon_baby in forum AWT / Swing
    Replies: 4
    Last Post: 05-26-2012, 02:07 PM
  4. How update Jlist in java Chat
    By pappyj77 in forum Threads and Synchronization
    Replies: 59
    Last Post: 09-13-2011, 01:26 AM
  5. Cannot get my JList to update!
    By rangvald in forum AWT / Swing
    Replies: 1
    Last Post: 11-17-2009, 01:57 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
  •