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.