Results 1 to 4 of 4
  1. #1
    arimaliz is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Unhappy help with variables please

    I am doing a dossier for my IB computer Science Class (Standard level). My program helps the user to decide what type of dossier they should do if they are staying for the second year of the IB. It is very simple. Consists of only 3 combo boxes and 6 window frames. It is a GUI program. I need help making the selections on my combo boxes variables and then send them to another listener to make a multi-dimensional array and then print that onto my results frame. Here is my code, please help.


    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.lang.Math;

    public class Dosier {
    JFrame frame1, frame2, frame3, frame4, frame5, frame6;
    JPanel contentPane1,contentPane2, contentPane3, contentPane4, contentPane5, contentPane6;
    JScrollPane scroll;
    JTextArea area, res;
    JComboBox subjects, hobbies, preference;
    JLabel instruc, test2, prompt1, prompt2, que1, que2, par, d, result;
    JTextField name, level;
    JButton topic, test, finish, done, results, pic, next;

    public Dosier() {
    .............
    ...........
    frame4 = new JFrame("Test");/*creates a new window for the test*/
    frame4.setDefaultCloseOperation(JFrame.EXIT_ON_CLO SE);

    contentPane4 = new JPanel();
    contentPane4.setLayout(new GridLayout(0, 2, 10, 5));
    contentPane4.setBorder(BorderFactory.createEmptyBo rder(10, 10, 10, 10));

    test2 = new JLabel("subjects");
    test2.setAlignmentX(JLabel.CENTER_ALIGNMENT);
    test2.setBorder(BorderFactory.createEmptyBorder(20 , 50, 20, 50));
    contentPane4.add(test2);

    String [] subs = {"mathematics", "sciences", "languages", "arts"};/*creates arrays for the combo box*/
    subjects = new JComboBox(subs);/*creates a combo box*/
    subjects.setAlignmentX(JComboBox.LEFT_ALIGNMENT);
    subjects.setSelectedIndex(0);
    subjects.addActionListener(new subjectsListener());/*creates a listener for combo box*/
    contentPane4.add(subjects);

    que1 = new JLabel("hobbies");
    que1.setAlignmentX(JLabel.CENTER_ALIGNMENT);
    que1.setBorder(BorderFactory.createEmptyBorder(20, 50, 20, 50));
    contentPane4.add(que1);

    String [] hobs = {"sports", "music", "drawing", "other"};
    hobbies = new JComboBox(hobs);
    hobbies.setAlignmentX(JComboBox.LEFT_ALIGNMENT);
    hobbies.setSelectedIndex(0);
    hobbies.addActionListener(new hobbiesListener());
    contentPane4.add(hobbies);

    que2 = new JLabel("dossier general ideas");
    que2.setAlignmentX(JLabel.CENTER_ALIGNMENT);
    que2.setBorder(BorderFactory.createEmptyBorder(20, 50, 20, 50));
    contentPane4.add(que2);

    String [] dos = {"data based", "educational", "games"};
    preference = new JComboBox(dos);
    preference.setAlignmentX(JComboBox.LEFT_ALIGNMENT) ;
    preference.setSelectedIndex(0);
    preference.addActionListener(new preferenceListener());
    contentPane4.add(preference);

    results = new JButton("My results");/*creates a button to direct the user to the4 results window*/
    results.setAlignmentX(JButton.CENTER_ALIGNMENT);
    results.addActionListener(new resultsListener());
    contentPane4.add(results);

    frame4.setContentPane(contentPane4);
    frame4.pack();
    frame4.setVisible(false);
    .........
    ........
    class subjectsListener implements ActionListener{

    public void actionPerformed(ActionEvent event) {
    JComboBox cBox = (JComboBox)event.getSource();
    String subjects = (String)cBox.getSelectedItem();
    if(subjects.equals("mathematics")){
    final String variable1 = subjects;
    }else if(subjects.equals("sciences")){
    final String variable1 = subjects;
    }else if(subjects.equals("languages")){
    final String variable1 = subjects;
    }else if(subjects.equals("arts")){
    final String variable1 = subjects;
    }

    }
    }
    class hobbiesListener implements ActionListener{

    public void actionPerformed(ActionEvent event) {
    JComboBox hBox = (JComboBox)event.getSource();
    String hobbies = (String)hBox.getSelectedItem();
    if(hobbies.equals("sports")){
    final String variable2 = hobbies;
    }else if(hobbies.equals("music")){
    final String variable2 = hobbies;
    }else if(hobbies.equals("drawing")){
    final String variable2 = hobbies;
    }else if(hobbies.equals("other")){
    final String variable2 = hobbies;
    }

    }
    }

    class preferenceListener implements ActionListener{

    public void actionPerformed(ActionEvent event) {
    JComboBox pBox = (JComboBox)event.getSource();
    String preference = (String)pBox.getSelectedItem();
    if(preference.equals("data based")){
    final String variable3 = preference;
    }else if(preference.equals("educational")){
    final String variable3 = preference;
    }else if(preference.equals("games")){
    final String variable3 = preference;
    }

    }

    }
    /*allows user to end the program*/
    class doneListener implements ActionListener{

    public void actionPerformed(ActionEvent event) {
    String done = event.getActionCommand();
    if(done.equals("Done")){
    frame3.setVisible(false);
    frame4.setVisible(true);
    }

    }

    }

    class resultsListener implements ActionListener{

    public void actionPerformed(ActionEvent event) {
    String results = event.getActionCommand();
    //final String[][]dossiers = new String[12][4];
    if(results.equals("My results")){
    setLabel(result);
    /*for (int i=0 ; i<12; i++){
    if((variable1.equals(dossier[i][1]) || vairable2.equals(dossier[i][1])) && variable3.equals(dossier[i][1])){
    results[next]= dossiers[i][0];
    next ++;
    }

    //do search

    //add results to results frame

    */
    frame4.setVisible(false);
    frame2.setVisible(true);
    frame5.setVisible(true);
    //}return results;

    }
    }
    }

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Have you tried it already? Whats giving you are a hard time? Any errors? We will help but Id love to get some solid questions(provide details), and what you have tried so far.

    also, please use code tags, large amounts of gui code is hard to read when unformatted.

    To use code tags surround your code in [ code] and [/code]

    [co de]
    Your
    code
    here
    [/code]

  3. #3
    arimaliz is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default

    oh sorry i am new to this ^^ well so far with that code i have no errors but i want need my program to do is give a result when something is selected from the combo boxes. E.g. its a window with 3 combo boxes. If the user picks from the first combo box mathematics, second box sports, and on the third box databased, then the result would be they need to do a, for example, a databased calendar. My teacher told me i can set the options of the combo boxes as a variable and then in my results listener i can do a multi-dimensional array where it evaluates the choices. I just cant seem to get the variables set to my next action listener to carry out the multi-array. I will repost the code
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.lang.Math;
    
    public class Dosier {
    JFrame frame1, frame2, frame3, frame4, frame5, frame6;
    JPanel contentPane1,contentPane2, contentPane3, contentPane4, contentPane5, contentPane6;
    JScrollPane scroll;
    JTextArea area, res;
    JComboBox subjects, hobbies, preference;
    JLabel instruc, test2, prompt1, prompt2, que1, que2, par, d, result;
    JTextField name, level;
    JButton topic, test, finish, done, results, pic, next;
    
    public Dosier() {
    .............
    ...........
    frame4 = new JFrame("Test");/*creates a new window for the test*/
    frame4.setDefaultCloseOperation(JFrame.EXIT_ON_CLO SE);
    
    contentPane4 = new JPanel();
    contentPane4.setLayout(new GridLayout(0, 2, 10, 5));
    contentPane4.setBorder(BorderFactory.createEmptyBo rder(10, 10, 10, 10));
    
    test2 = new JLabel("subjects");
    test2.setAlignmentX(JLabel.CENTER_ALIGNMENT);
    test2.setBorder(BorderFactory.createEmptyBorder(20 , 50, 20, 50));
    contentPane4.add(test2);
    
    String [] subs = {"mathematics", "sciences", "languages", "arts"};/*creates arrays for the combo box*/
    subjects = new JComboBox(subs);/*creates a combo box*/
    subjects.setAlignmentX(JComboBox.LEFT_ALIGNMENT);
    subjects.setSelectedIndex(0);
    subjects.addActionListener(new subjectsListener());/*creates a listener for combo box*/
    contentPane4.add(subjects);
    
    que1 = new JLabel("hobbies");
    que1.setAlignmentX(JLabel.CENTER_ALIGNMENT);
    que1.setBorder(BorderFactory.createEmptyBorder(20, 50, 20, 50));
    contentPane4.add(que1);
    
    String [] hobs = {"sports", "music", "drawing", "other"};
    hobbies = new JComboBox(hobs);
    hobbies.setAlignmentX(JComboBox.LEFT_ALIGNMENT);
    hobbies.setSelectedIndex(0);
    hobbies.addActionListener(new hobbiesListener());
    contentPane4.add(hobbies);
    
    que2 = new JLabel("dossier general ideas");
    que2.setAlignmentX(JLabel.CENTER_ALIGNMENT);
    que2.setBorder(BorderFactory.createEmptyBorder(20, 50, 20, 50));
    contentPane4.add(que2);
    
    String [] dos = {"data based", "educational", "games"};
    preference = new JComboBox(dos);
    preference.setAlignmentX(JComboBox.LEFT_ALIGNMENT) ;
    preference.setSelectedIndex(0);
    preference.addActionListener(new preferenceListener());
    contentPane4.add(preference);
    
    results = new JButton("My results");/*creates a button to direct the user to the4 results window*/
    results.setAlignmentX(JButton.CENTER_ALIGNMENT);
    results.addActionListener(new resultsListener());
    contentPane4.add(results);
    
    frame4.setContentPane(contentPane4);
    frame4.pack();
    frame4.setVisible(false);
    .........
    ........
    class subjectsListener implements ActionListener{
    
    public void actionPerformed(ActionEvent event) {
    JComboBox cBox = (JComboBox)event.getSource();
    String subjects = (String)cBox.getSelectedItem();
    if(subjects.equals("mathematics")){
    final String variable1 = subjects;
    }else if(subjects.equals("sciences")){
    final String variable1 = subjects;
    }else if(subjects.equals("languages")){
    final String variable1 = subjects;
    }else if(subjects.equals("arts")){
    final String variable1 = subjects;
    }
    
    }
    }
    class hobbiesListener implements ActionListener{
    
    public void actionPerformed(ActionEvent event) {
    JComboBox hBox = (JComboBox)event.getSource();
    String hobbies = (String)hBox.getSelectedItem();
    if(hobbies.equals("sports")){
    final String variable2 = hobbies;
    }else if(hobbies.equals("music")){
    final String variable2 = hobbies;
    }else if(hobbies.equals("drawing")){
    final String variable2 = hobbies;
    }else if(hobbies.equals("other")){
    final String variable2 = hobbies;
    }
    
    }
    }
    
    class preferenceListener implements ActionListener{
    
    public void actionPerformed(ActionEvent event) {
    JComboBox pBox = (JComboBox)event.getSource();
    String preference = (String)pBox.getSelectedItem();
    if(preference.equals("data based")){
    final String variable3 = preference;
    }else if(preference.equals("educational")){
    final String variable3 = preference;
    }else if(preference.equals("games")){
    final String variable3 = preference;
    }
    
    }
    
    }
    /*allows user to end the program*/
    class doneListener implements ActionListener{
    
    public void actionPerformed(ActionEvent event) {
    String done = event.getActionCommand();
    if(done.equals("Done")){
    frame3.setVisible(false);
    frame4.setVisible(true);
    }
    
    }
    
    }
    
    class resultsListener implements ActionListener{
    
    public void actionPerformed(ActionEvent event) {
    String results = event.getActionCommand();
    //final String[][]dossiers = new String[12][4];
    if(results.equals("My results")){
    setLabel(result);
    /*for (int i=0 ; i<12; i++){
    if((variable1.equals(dossier[i][1]) || vairable2.equals(dossier[i][1])) && variable3.equals(dossier[i][1])){
    results[next]= dossiers[i][0];
    next ++;
    }
    
    //do search
    
    //add results to results frame
    
    */
    frame4.setVisible(false);
    frame2.setVisible(true);
    frame5.setVisible(true);
    //}return results;
    
    }
    }
    }

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,002
    Rep Power
    20

Similar Threads

  1. Variables in a Servlet
    By krishkill in forum Java Servlet
    Replies: 3
    Last Post: 06-11-2010, 02:20 AM
  2. What are Instance variables and static variables?
    By sandeshforu in forum New To Java
    Replies: 3
    Last Post: 09-09-2009, 05:48 PM
  3. Collecton of Variables
    By c26354 in forum New To Java
    Replies: 12
    Last Post: 06-30-2008, 03:09 PM
  4. Variables
    By mew in forum New To Java
    Replies: 3
    Last Post: 12-11-2007, 12:44 PM

Tags for this Thread

Posting Permissions

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