Results 1 to 2 of 2
  1. #1
    GavinCash is offline Member
    Join Date
    Oct 2010
    Posts
    10
    Rep Power
    0

    Default Using Multiple Panels to Produce an Output

    Hello Folks!

    I've been working on a multi-class code to simulate, somewhat, the enrollment process of college students. I've gotten my Course and Person classes down easily, and an Information class to bring in names and the like. Now, while working on my main method, I've found that I've not a clue how to link two combo boxes to a button and create an action, or cause an output. I'd like to have a list of Students, a List of Courses, and three buttons. I've got two Panels, but have yet to find the way to add them to the main frame.

    Please, send some help my way!


    !! Edit !!
    I've gotten many of the bugs and issues out of the way and now I simply need to link the studentList and courseList and the Add button together. Is there a way to get the placement of certain objects in two lists that are selected into the 'addButton' action receiver bit?

    Java Code:
    package assignment6;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public final class Main  extends JFrame
    {
        private Person[] Students;
        private Course[] Classes;
        private JPanel studentPanel;
        private JPanel coursePanel;
        private JPanel buttonPanel;
        private JPanel mainPanel = new JPanel();
        private String addBut = "ADD", closeBut = "CLOSE", reportBut = "REPORT";
        private String[] List1 = new String[30], List2  = new String[5];
        private int stEl, coEl;
        public Main()
        {
            Information Info = new Information();
            Students = new Person[30];
            Classes = new Course[5];
            for(int x = 0; x < Students.length; x++)
            {
                Students[x] = new Person(Info.nextFN(),Info.nextLN(),Info.nextAd(),Info.nextSt());
            }
            for(int y = 0; y < Classes.length; y++)
            {
                Classes[y] = new Course(Info.nextCNa(),Info.nextCNu(),Info.nextCS(),Info.nextCM());
            }
            setTitle("Enrollment Window");
    	setLayout(new BorderLayout());
            JFrame frame = new JFrame();
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	frame.setLocationRelativeTo(null);
    	studentPanel();
            coursePanel();
            buttonPanel();
            getContentPane().add(mainPanel);
            frame.add(mainPanel);
            frame.setSize(450,200);
            frame.setVisible(true);
        }
        public void studentPanel()
        {
            List1 = new String[30];
            for(int a = 0; a < 30; a++)
                List1[a] = Students[a].getName();
            StudentListener StBt = new StudentListener();
            JComboBox studentList = new JComboBox(List1);
            studentList.setEditable(false);
            mainPanel.add(studentList);
            
        }
        public class StudentListener implements ActionListener
        {
            int Choice = 0;
            public void actionPerformed(ActionEvent e)
            {
                JComboBox CB = (JComboBox)e.getSource();
                String Student = (String)CB.getSelectedItem();
            }
        }
        public void coursePanel()
        {
            List2 = new String[5];
            for(int a = 0; a < 5; a++)
            {
                List2[a] = Classes[a].getName();
            }
            CourseListener CoBt = new CourseListener();
            JComboBox courseList = new JComboBox(List2);
            courseList.setEditable(false);
            mainPanel.add(courseList);
        }
        public class CourseListener implements ActionListener
        {
            int Choice1 = 0;
            public void actionPerformed(ActionEvent e)
            {
                JComboBox CB = (JComboBox)e.getSource();
                String Course = (String)CB.getSelectedItem();
            }
        }
        public void buttonPanel()
        {
            ButtonListener BL = new ButtonListener();
            JButton addButton = new JButton(addBut);
            addButton.setActionCommand(addBut);
            addButton.addActionListener(BL);
            JButton closeButton = new JButton(closeBut);
            closeButton.setActionCommand(closeBut);
            closeButton.addActionListener(BL);
            JButton reportButton = new JButton(reportBut);
            reportButton.setActionCommand(reportBut);
            reportButton.addActionListener(BL);
            mainPanel.add(addButton);
            mainPanel.add(closeButton,BorderLayout.CENTER);
            mainPanel.add(reportButton,BorderLayout.EAST);
        }
        public class ButtonListener implements ActionListener
        {
            int Choice2 = 0;
            public void actionPerformed(ActionEvent e)
            {
                String Response;
                if(e.getActionCommand().equals(addBut))
                {
                    
                }
                else if(e.getActionCommand().equals(closeBut))
                {
                    JOptionPane.showMessageDialog(null, "Closing Program","Closing Program", JOptionPane.INFORMATION_MESSAGE);
                    System.exit(1);
                }
                else if(e.getActionCommand().equals(reportBut))
                {
                    String[] Options = {"Student","Course"};
                    Response = (String)JOptionPane.showInputDialog(null, "Choose Report","Report",JOptionPane.INFORMATION_MESSAGE, null, Options, Options[0]);
                    if(Response.equals("Student"))
                    {
                        String Name = (String)JOptionPane.showInputDialog(null, "Choose Student","Student Report",JOptionPane.INFORMATION_MESSAGE, null, List1, List1[0]);
                        for(int x = 0; x < Students.length; x++)
                        {
                            if(Name.equals(Students[x].getName()))
                            {
                                JOptionPane.showMessageDialog(null, Students[x].toString(), "Student Report", JOptionPane.INFORMATION_MESSAGE);
                            }
                        }
                    }
                    else if(Response.equals("Course"))
                    {
                        String Course = (String)JOptionPane.showInputDialog(null, "Choose Course","Course Report",JOptionPane.INFORMATION_MESSAGE, null, List2, List2[0]);
                        for(int y = 0; y < Classes.length; y++)
                        {
                            if(Course.equals(Classes[y].getName()))
                            {
                                JOptionPane.showMessageDialog(null, Classes[y].getRoster(),"Course Report",JOptionPane.INFORMATION_MESSAGE);
                            }
                        }
                    }
                }
            }
        }
        public static void main(String[] args)
        {
            Main frame = new Main();
        }
    }
    Last edited by GavinCash; 12-07-2010 at 02:00 AM.

  2. #2
    demonick is offline Member
    Join Date
    Dec 2010
    Posts
    27
    Rep Power
    0

    Default

    For ComboBox the currently selected item is retrieved with
    .getSelectedIndex() - returns a String.
    For JList -
    sourceListForJList[jList.getLeadSelectionIndex()]

Similar Threads

  1. Problem while using the multiple panels in swings
    By anand206 in forum AWT / Swing
    Replies: 3
    Last Post: 03-23-2010, 04:33 PM
  2. Multiple Panels in JFrame or JPanel
    By DavidG24 in forum AWT / Swing
    Replies: 5
    Last Post: 05-16-2009, 12:47 PM
  3. Replies: 4
    Last Post: 10-02-2008, 10:05 PM
  4. Replies: 6
    Last Post: 07-28-2008, 06:04 PM
  5. Replies: 7
    Last Post: 07-09-2008, 12:06 AM

Posting Permissions

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