Results 1 to 18 of 18
  1. #1
    helpmeplease is offline Member
    Join Date
    Dec 2009
    Posts
    15
    Rep Power
    0

    Default Need help with array list jpanel question!!! Plz help!!!

    I wrote a program that takes a users input and posts the variables back to them. I got everything working except getting the data to post the array back on the screen. what am i doing wrong?

    Java Code:
    /**
     * This java program will take either allow a user to input some questions wth solutions attached.
     * The program can also allow a user to query the database and see the questions in the database along with the answers attached.
     * 
     * @Mr. Willie Rainwater, Andre' R. Seals, & Elvino Taylor
     * @December 4th, 2009
     */
    
    
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.Desktop;
    import java.io.IOException;
    import java.net.URI;
    import java.net.URISyntaxException;
    import java.io.File;
    import javax.swing.border.*;
    import java.util.ArrayList;
    import java.util.Arrays;
    
    public class AnswerBot extends JFrame
    {
       private JFrame botFrame;
       JButton exitButton, okButton, queryButton;
       ArrayList<String> questionArray = new ArrayList<String>();
       ArrayList<String> solutionArray = new ArrayList<String>();
      JTextField Q = new JTextField (60);
       JTextField S = new JTextField (60);
       ArrayList<String> pairArray = new ArrayList<String>();
       JPanel viewPane=new JPanel();
    
        /**
         * Constructor for objects of class AnswerBot
         */
        public AnswerBot()
        {
          botFrame = new JFrame("IT Answerbot");
          makeFrame();
          
          //instantiating listener object
                ButtonListener bListener = new ButtonListener();
                exitButton.addActionListener(bListener);
                okButton.addActionListener(bListener);
                
        }
    
       
        private void makeFrame()
        {
            // main container
            Container mainContainer = botFrame.getContentPane( );
            mainContainer.setLayout(new BorderLayout());
            
                
            
                
            
            // center containter to hold Capture and Display Panel
            JPanel centerContainer = new JPanel ();
            centerContainer.setLayout(new BorderLayout());
            
            // put the exit button in the south
            //mainContainer.add(new JButton("Exit"), BorderLayout.SOUTH);
            
            // Capture Panel with BorderLayout
            JPanel capturePane= new JPanel();
            capturePane.setLayout(new BorderLayout());
            
             //set the border with a title
           capturePane.setBorder(new TitledBorder(new EtchedBorder(), "Bot Pane"));
            
            // make a top panel to hold name, surname, and occupation with GridLayout
            JPanel topPane = new JPanel();
            topPane.setLayout(new GridLayout(3,1));
            
            
            // make three panels with FlowLayout
            JPanel questionPane = new JPanel();
            questionPane.setLayout(new FlowLayout(FlowLayout.LEFT));        
            JPanel solutionPane = new JPanel();
            solutionPane.setLayout(new FlowLayout(FlowLayout.LEFT));        
            JPanel jobPane = new JPanel();
            jobPane.setLayout(new FlowLayout(FlowLayout.LEFT));
            
            //put the label "Question:" and textField into the name panel
            questionPane.add(new JLabel("Question:"));
            //questionPane.add(new JTextField("", 60));
            
            questionPane.add(Q);
            
             //put the label "Solution:" and textField into the surname panel
            solutionPane.add(new JLabel("Solution:"));
            //checkPane.add(new JTextField("", 60));
            solutionPane.add(S);
             //put the three panels into top panel
            topPane.add(questionPane);
            topPane.add(solutionPane);
            
            //put the top panel into the Capture panel
            capturePane.add(topPane, BorderLayout.NORTH);
            
            
             // make a button panel for the Ok and Cancel buttons
            JPanel buttonPane = new JPanel();
            buttonPane.setLayout(new FlowLayout());
            okButton = new JButton("Ok");
            exitButton = new JButton("Exit");
            buttonPane.add(okButton);
            buttonPane.add(exitButton);
            
           
            
           //put the action button panel into Capture panel 
            capturePane.add(buttonPane, BorderLayout.SOUTH); 
            
            //put the Capture panel into center panel
            centerContainer.add(capturePane, BorderLayout.CENTER);
            
            // Display Panel with GridLayout
            JPanel displayPane= new JPanel();
            displayPane.setLayout(new FlowLayout());      
            
            //set the border with a title
            displayPane.setBorder(new TitledBorder(new EtchedBorder(), "Query saved Question/Solutions Pane"));
            
            
            capturePane.add(viewPane, BorderLayout.CENTER);
            
            viewPane.setBorder(new TitledBorder(new EtchedBorder(), "View Query Question/Solution Pane"));
            
            
            
            JPanel labelPane = new JPanel();
            displayPane.setLayout(new FlowLayout());
            JButton queryButton = new JButton("Query");
            
            displayPane.add(queryButton);
            // put 2 labels in the label panels
             labelPane.add(new JLabel("Question: ")); 
            labelPane.add(new JLabel("Solution: "));
    
            
            //put the Display panel into center panel
            centerContainer.add(displayPane, BorderLayout.SOUTH);
            
            ////put the center panel into main container to display
            mainContainer.add(centerContainer, BorderLayout.CENTER);
             
            botFrame.setSize(800,600);
            botFrame.setVisible(true);
    
        }
        class ButtonListener implements ActionListener
        {
            public void actionPerformed(ActionEvent buttonEvent)
            {
       
                if (buttonEvent.getSource() == okButton)
                {       
                   questionArray.add(Q.toString());
                    Q.setText(" "); 
                    
                    solutionArray.add(S.toString());
                    S.setText(" ");
                }
            
                //action to be performed when a helpTopics event
                if (buttonEvent.getSource() == exitButton)
    
                {
                    System.exit(0);
                }
                if (buttonEvent.getSource() == queryButton)
               {
                 int i;
                 
    System.out.print("Your question is: ");
    
           System.out.print(questionArray.get(Q));
                }
            }
            
        }
    }
    Last edited by helpmeplease; 12-11-2009 at 05:11 AM. Reason: Code tags added to aid readability of code

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Hello, and welcome to the forum!

    A few suggestions:
    1) I've added code tags to your code to help make your post readable. Please read my signature to see how you can do this yourself next go-around.
    2) Please describe in greater detail just what you're trying to achieve here. This here:
    I wrote a program that takes a users input and posts the variables back to them. I got everything working except getting the data to post the array back on the screen. what am i doing wrong?
    assumes that we know a lot more about your current problem than we do. Please help us to understand it better, and maybe we can help you out.

    Much luck.

  3. #3
    helpmeplease is offline Member
    Join Date
    Dec 2009
    Posts
    15
    Rep Power
    0

    Default

    basically its like a mini address book.

    it uses an array like

    question: _____________ <--- user inputs question
    answer: _______________ <------------------ user inputs answer

    __________________________________________________ _______


    ok <-----user presses this button and it saves to the array


    query <------------ user presses this button and the data posts back up in the middle field.

    if u run the program youll see exactly what im talking about

  4. #4
    helpmeplease is offline Member
    Join Date
    Dec 2009
    Posts
    15
    Rep Power
    0

    Default

    basically i got most of the code working, its the last part i need help with. getting the query button to display the results the user input.


    this part really:

    if (buttonEvent.getSource() == queryButton)
    {
    int i;

    System.out.print("Your question is: ");

    System.out.print(questionArray.get(Q));
    }

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    query <------------ user presses this button and the data posts back up in the middle field.
    And this is what I'm having trouble understanding. How is the data supposed to be displayed? I'm afraid that details that are probably quite logically arranged in your head are not in ours, and these details are important.

    Other issue: My guess is that you want a question associated with a solution in a one-to-one correspondence. If so, then don't separate them into parallel arrays, but instead join them together into a single object, say a QuestionSolution object that holds two Strings (and any other logic that this class may need). Then create one ArrayList<QuestionSolution>.

    Possible display solutions: a JTable with two columns, or a JList with a custom renderer, or a display panel actually be a JScrollPane that holds a column of JPanels, that each displays one question and its corresponding solution.

  6. #6
    helpmeplease is offline Member
    Join Date
    Dec 2009
    Posts
    15
    Rep Power
    0

    Default

    you guessed right. its a question and answer at the same time.

    Need help with array list jpanel question!!! Plz help!!!-xccx.jpg

    maybe this will help. you see the middle field? thats where i want the answer to post
    Last edited by helpmeplease; 12-11-2009 at 05:31 AM.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    As mentioned above, create a new class that holds two Strings, a question String and a solution String.

  8. #8
    helpmeplease is offline Member
    Join Date
    Dec 2009
    Posts
    15
    Rep Power
    0

    Default

    peep my last response. i fixed it

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by helpmeplease View Post
    peep my last response. i fixed it
    So you're all set then? If not, suggestions in response #5.

  10. #10
    helpmeplease is offline Member
    Join Date
    Dec 2009
    Posts
    15
    Rep Power
    0

    Default

    I wish. it runs with no errors but still doesnt post back on. regardless of if its divided or in the same array it should run. thanks for trying though

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by helpmeplease View Post
    I wish. it runs with no errors but still doesnt post back on. regardless of if its divided or in the same array it should run. thanks for trying though
    I think that you're missing what I wrote. Again:
    Possible display solutions: a JTable with two columns, or a JList with a custom renderer, or a display panel actually be a JScrollPane that holds a column of JPanels, that each displays one question and its corresponding solution.

  12. #12
    helpmeplease is offline Member
    Join Date
    Dec 2009
    Posts
    15
    Rep Power
    0

    Default

    I understand what you wrote. I dont think you get the problem. I could do either easily if I knew how to print it to the screen.

    my issue deals more with

    Java Code:
    System.out.println( ?????????????????? )

    other than that i could have it running

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You're far better off not trying to use println here but to use one of the techniques I suggested. println will only work if you redirect the out stream to a component.

  14. #14
    helpmeplease is offline Member
    Join Date
    Dec 2009
    Posts
    15
    Rep Power
    0

    Default

    but wouldnt I have to println at some point anyway? how can I print to the screen without it?

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    A simple example using a JList:
    Java Code:
    import java.awt.BorderLayout;
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.swing.*;
    
    public class AnsBot2 {
       List<Fubar2> fooList = new ArrayList<Fubar2>();
    
       private JPanel mainPanel = new JPanel();
    
       public AnsBot2() {
          fooList.add(new Fubar2("Hello", "Goodbye"));
          fooList.add(new Fubar2("Open", "Close"));
          fooList.add(new Fubar2("Inside", "Outside"));
          fooList.add(new Fubar2("Monday", "Tuesday"));
          fooList.add(new Fubar2("January", "February"));
          fooList.add(new Fubar2("Summer", "Winter"));
          fooList.add(new Fubar2("Hello", "Goodbye"));
          fooList.add(new Fubar2("Open", "Close"));
          fooList.add(new Fubar2("Inside", "Outside"));
          fooList.add(new Fubar2("Monday", "Tuesday"));
          fooList.add(new Fubar2("January", "February"));
          fooList.add(new Fubar2("Summer", "Winter"));
          fooList.add(new Fubar2("Hello", "Goodbye"));
          fooList.add(new Fubar2("Open", "Close"));
          fooList.add(new Fubar2("Inside", "Outside"));
          fooList.add(new Fubar2("Monday", "Tuesday"));
          fooList.add(new Fubar2("January", "February"));
          fooList.add(new Fubar2("Summer", "Winter"));
          
          
          mainPanel.setLayout(new BorderLayout());
          DefaultListModel model = new DefaultListModel();
          for (Fubar2 foo2 : fooList) {
             model.addElement(foo2);
          }
          JList list = new JList(model);
          
          mainPanel.add(new JScrollPane(list));
       }
    
       public JComponent getComponent() {
          return mainPanel;
       }
    
       private static void createAndShowUI() {
          JFrame frame = new JFrame("AnsBot2");
          frame.getContentPane().add(new AnsBot2().getComponent());
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                createAndShowUI();
             }
          });
       }
       
    }
    
    class Fubar2 {
       private String spam;
       private String shrubbery;
       
       Fubar2(String spam, String shrubbery) {
          this.spam = spam;
          this.shrubbery = shrubbery;
       }
       
       public String getSpam() {
          return spam;
       }
    
       public String getShrubbery() {
          return shrubbery;
       }
       
       @Override
       public String toString() {
          return "<HTML><br>Spam: " + spam + "<br>" + "Shrubbery: " + shrubbery + "<br></HTML>";
       }
    }
    Last edited by Fubarable; 12-11-2009 at 06:22 AM.

  16. #16
    helpmeplease is offline Member
    Join Date
    Dec 2009
    Posts
    15
    Rep Power
    0

    Default

    Yeah that code is easy. thats assuming the variables are already in place. Im speaking on when a user inputs the variables.......

  17. #17
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    That's what a DefaultListModel is all about.

  18. #18
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by helpmeplease View Post
    Yeah that code is easy. thats assuming the variables are already in place. Im speaking on when a user inputs the variables.......
    and I think you are misunderstanding your problem here and my recommendations. You are actually creating your list on demand, when the user presses a button. My "simple" code (or something like it), can do the same thing -- create a model and then use the GUI to display it on demand. And actually the ArrayList in my code is unnecessary, as you can just add objects to a JList's model (the DefaultListModel object).

Similar Threads

  1. How to display image from byte array in JPANEL
    By waqasdaar in forum AWT / Swing
    Replies: 0
    Last Post: 03-22-2009, 01:11 AM
  2. n00b: linked list question
    By ankitmcgill in forum New To Java
    Replies: 2
    Last Post: 03-13-2009, 05:15 AM
  3. A simple List question
    By right2001 in forum New To Java
    Replies: 2
    Last Post: 02-16-2009, 04:37 AM
  4. Linked List Question
    By CirKuT in forum New To Java
    Replies: 4
    Last Post: 12-10-2008, 07:56 PM
  5. [SOLVED] 2 D Array and JComboBox on a JPanel
    By crazydeo in forum New To Java
    Replies: 7
    Last Post: 05-23-2008, 10:17 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
  •