0

I have a JFrame named MainGUI. Inside of MainGUI I have passed three LinkedList ll1, ll2, ll3.

These LinkedList are full of data and I'm trying to just print one of them on the screen into my JPanel. I'm use to just doing a for loop and using System.out.println to print things out onto the screen.

So right now I have MainGUI which hosts three buttons.

New Tasks In Progress Tasks Completed Tasks

Each button has a different LinkedList ll1, ll2, ll3 etc.
I want to be able to click the button and have the data elements listed below in the JPanelI created which rests under the buttons.

Any help is deeply appreciated.

user3622460
  • 1,231
  • 5
  • 23
  • 42
  • You should provide a sample of the code you're working on. Take a look at [this question](http://stackoverflow.com/questions/284899/how-do-you-add-an-actionlistener-onto-a-jbutton-in-java) and also [this tutorial](http://www.javaprogrammingforums.com/java-swing-tutorials/278-how-add-actionlistener-jbutton-swing.html). – goncalotomas May 21 '15 at 20:54
  • 1
    Your question seems incomplete, incomplete in that there's no code and limited description of your program, and what is wrong with your current code. So far it just seems like a problem of passing information from one object to another. So where are you stuck? – Hovercraft Full Of Eels May 21 '15 at 21:09
  • Have a look at [How to Use Lists](http://docs.oracle.com/javase/tutorial/uiswing/components/list.html) – MadProgrammer May 21 '15 at 21:46

2 Answers2

2

Since you provided no code, I assume you are having trouble understanding how LinkedList can interact in programs that have a GUI.

First off, when using buttons you always need to instruct them to do something when they are clicked by adding an ActionListener, as explained in this answer.
Secondly, if you want to add the list data to the JPanel, there are a few ways you can do it. A JList, or if you'd like the user to be able to copy and paste the data (I find it to be very handy), a JTextArea, ... Just make sure to call setEditable(false) in order to stop the user from fiddling with the data you provide. Considering a JTextArea, here's what that would look like, if ll1 contained Strings:

Adding somewhere that our JPanel contains a JTextArea:

JTextArea txtArea = new JTextArea();
txtArea.setEditable(false);
panel.add(txtArea);

Now, we order the button to do something when clicked:

btn.addActionListener(new ActionListener() { 
  public void actionPerformed(ActionEvent e) { 
    txtArea.setText(null); //clear out old text
    for(String str: ll1) {
      txtArea.append(str+"\n");
    }
    panel.revalidate(); //repaint JPanel
  } 
});

This way, you can click the button as many times as you want. Note that if you add more content to ll1 after it is displayed it won't get visually updated by itself, you will always need to click the button again or look further into Listeners.

Community
  • 1
  • 1
goncalotomas
  • 1,000
  • 1
  • 12
  • 29
2

You can try adding a JTextArea or whichever JComponent that suits what you want to display to the JPanel that you want to display the data from. Write the data from your linked list to that JComponent using its method e.g. append() if you're using JTextArea.

Lightsout
  • 3,454
  • 2
  • 36
  • 65