2 Replies - 350 Views - Last Post: 04 April 2013 - 03:03 PM Rate Topic: -----

#1 Mackinaw  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 18-November 12

Java Gui help

Posted 04 April 2013 - 08:13 AM

Hello,
Right now I have a program that reads in file, does some stuff, and outputs a write file. The code arranges students,grad students, faculty, and staff.

For my GUI, I have a JList that outputs a certain name when the student/grad/faculty/staff button is pressed. What I want to do is when I click on the actual name in the JList, I would like it to pop up a new JFrame/Panel (or any other method) with their specific information.

I use the netbeans GUI builder as well if anyone was wondering. I added a actionevent mouse click to JList but not sure is that is correct of need to do something else.

here is my GUI code.
Thanks in advance.

 package department;

/**
 *
 * @author ADMIN
 */
public class NewJFrame extends javax.swing.JFrame {
        Department d = new Department();
        
        
    /**
     * Creates new form NewJFrame
     */
    public NewJFrame(Department d) {
        initComponents();
        this.d = d;
      
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        allStudents_btn = new javax.swing.JButton();
        allEmployees_btn = new javax.swing.JButton();
        close_btn = new javax.swing.JButton();
        undergrad_btn = new javax.swing.JButton();
        graduate_btn = new javax.swing.JButton();
        faculty_btn = new javax.swing.JButton();
        staff_btn = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jList1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jList1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jList1);

        allStudents_btn.setText("Show All Students");
        allStudents_btn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                allStudents_btnActionPerformed(evt);
            }
        });

        allEmployees_btn.setText("Show All Employees");
        allEmployees_btn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                allEmployees_btnActionPerformed(evt);
            }
        });

        close_btn.setText("Close");
        close_btn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                close_btnActionPerformed(evt);
            }
        });

        undergrad_btn.setText("Undergrad");
        undergrad_btn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                undergrad_btnActionPerformed(evt);
            }
        });

        graduate_btn.setText("Graduate");
        graduate_btn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                graduate_btnActionPerformed(evt);
            }
        });

        faculty_btn.setText("Faculty");
        faculty_btn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                faculty_btnActionPerformed(evt);
            }
        });

        staff_btn.setText("Staff");
        staff_btn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                staff_btnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(close_btn, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 632, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(undergrad_btn)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(graduate_btn))
                            .addComponent(allStudents_btn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(101, 101, 101)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(faculty_btn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(staff_btn, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(allEmployees_btn, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(151, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(allStudents_btn, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(allEmployees_btn, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(undergrad_btn)
                    .addComponent(graduate_btn)
                    .addComponent(faculty_btn)
                    .addComponent(staff_btn))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 26, Short.MAX_VALUE)
                .addComponent(close_btn, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>                        

    
    /**
     * shows all students in JList when pressed
     * @param evt 
     */
    private void allStudents_btnActionPerformed(java.awt.event.ActionEvent evt) {                                                
        
        
         String [] array = new String[d.getNumStudents()];
        for(int i = 0; i<d.getNumStudents(); i++){
            array[i] = d.getStudent(i).getFirstName();
        }
        jList1.setListData(array);
    }                                               

    
    /**
     * Shows all employees in JList when pressed
     * @param evt 
     */
    private void allEmployees_btnActionPerformed(java.awt.event.ActionEvent evt) {                                                 
        
        
        String [] array = new String[d.getNumStudents()];
        for(int i = 0; i<d.getNumEmployees(); i++){
            array[i] = d.getEmployee(i).getFirstName();
        }
        jList1.setListData(array);
        
        
    }                                                

    
    /**
     * exits program
     * @param evt 
     */
    private void close_btnActionPerformed(java.awt.event.ActionEvent evt) {                                          
        System.exit(0);
         
    }                                         

    private void undergrad_btnActionPerformed(java.awt.event.ActionEvent evt) {                                              
        // TODO add your handling code here:
        
        String [] array = new String[d.getNumStudents()];
        for(int i = 0; i<d.getNumStudents(); i++){
           if(d.getStudent(i) instanceof GraduateStudent){
                 
             }else{
               array[i] = d.getStudent(i).getFirstName() + " " +
                          d.getStudent(i).getLastName();
              }
        }
        jList1.setListData(array);
        
    }                                             

    private void graduate_btnActionPerformed(java.awt.event.ActionEvent evt) {                                             
String [] array = new String[d.getNumStudents()];
        for(int i = 0; i<d.getNumStudents(); i++){
           if(d.getStudent(i) instanceof GraduateStudent) {
                array[i] = d.getStudent(i).getFirstName() + " " +
                           d.getStudent(i).getLastName();
            }
        }
        jList1.setListData(array);        
    }                                            

    private void jList1MouseClicked(java.awt.event.MouseEvent evt) {                                    
      
    }                                   

    private void faculty_btnActionPerformed(java.awt.event.ActionEvent evt) {                                            
String [] array = new String[d.getNumEmployees()];
        for(int i = 0; i<d.getNumEmployees(); i++){
           if(d.getEmployee(i) instanceof Faculty){
                array[i] = d.getEmployee(i).getFirstName() + " " + 
                           d.getEmployee(i).getLastName();
                }
                
        }
        jList1.setListData(array);        // TODO add your handling code here:
    }                                           

    private void staff_btnActionPerformed(java.awt.event.ActionEvent evt) {                                          
String [] array = new String[d.getNumEmployees()];
        for(int i = 0; i<d.getNumEmployees(); i++){
           if(d.getEmployee(i) instanceof Staff){
                  
               array[i] = d.getEmployee(i).getFirstName()+ " " + 
                          d.getEmployee(i).getLastName(); 
               
                            
                }
                
        } 
        jList1.setListData(array);
    }                                         

    /**
     * @param args the command line arguments
     */
   
    // Variables declaration - do not modify                     
    private javax.swing.JButton allEmployees_btn;
    private javax.swing.JButton allStudents_btn;
    private javax.swing.JButton close_btn;
    private javax.swing.JButton faculty_btn;
    private javax.swing.JButton graduate_btn;
    private javax.swing.JList jList1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton staff_btn;
    private javax.swing.JButton undergrad_btn;
    // End of variables declaration                   
}



Is This A Good Question/Topic? 0
  • +

Replies To: Java Gui help

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2816
  • View blog
  • Posts: 11,924
  • Joined: 20-September 08

Re: Java Gui help

Posted 04 April 2013 - 08:37 AM

An ActionListener should be OK. Make sure you have the visualization you want in Student.toString. You can then do the following in actionPerformed

Quote

JOptionPane.showMessageDialog(this, jList1.getSelectedValue().toString());

Was This Post Helpful? 0
  • +
  • -

#3 Mackinaw  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 18-November 12

Re: Java Gui help

Posted 04 April 2013 - 03:03 PM

is there a way to change the contents of the Jlist when clicked?

Right now my JList contains first name and last name of employee/student/etc. Upon clicking, I would like to show more specific information.

JOptionPane.showMessageDialog(this, jList1.getSelectedValue().toString());


shows only what is in the JList, which is only the names.

I tried creating another JList with different toString method, so when I click the JList1 I can bring the JList2 contents in the pop up

I tried
 JOptionPane.showMessageDialog(this, jList2.getSelectedValue().toString());

but this doesnt work since there is no selected value from my JList2;

So, how would I see first name last name of my employee, but upon clicking, see more specific information?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1