Create a list of posts and make each post clickable.
I am trying to create a UI that has two panels.
The left pane will display a list of files and the right pane will display the content.
Now I want the list of files in the left pane to look like a regular list. But when I click on an entry in this list, the content of the specific file should be displayed in the right pane.
How can I achieve this with Swing?
Here I made a short example, with help JList
from left and JTextArea
right. I used ListSelectionListener
to change the list of items. Use LayoutManager
as you like.
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class JListTest {
private JList jList1;
private JPanel jPanel1;
private JTextArea jTextArea1;
public JListTest() {
initComponents();
}
private void initComponents() {
JFrame f = new JFrame();
jPanel1 = new JPanel();
jList1 = new JList();
jTextArea1 = new JTextArea();
jList1.setModel(new AbstractListModel() {
String[] strings = {"Item 1", "Item 2"};
@Override
public int getSize() {
return strings.length;
}
@Override
public Object getElementAt(int i) {
return strings[i];
}
});
jList1.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent evt) {
jList1ValueChanged(evt);
}
});
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jPanel1.add(jList1);
jPanel1.add(jTextArea1);
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.add(jPanel1);
f.pack();
f.setVisible(true);
}
private void jList1ValueChanged(javax.swing.event.ListSelectionEvent evt) {
//set text on right here
String s = (String) jList1.getSelectedValue();
if (s.equals("Item 1")) {
jTextArea1.setText("You clicked on list 1");
}
if (s.equals("Item 2")) {
jTextArea1.setText("You clicked on list 2");
}
}
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new JListTest();
}
});
}
}
Check out this tutorial . It explains how to use lists in Swing, including the event handlers that are required to register click events.
You might want to look at this JTree example .
Some examples for your reference:
-
FileBrowser
usesJTree
on the left and nested part panels on the right. -
ImageDisplay
inserts a custom oneJFileChooser
on the left and displays a scrolling image on the right. -
CheckTable
shows aJTable
on the left and aDisplayPanel
on the right.
Use JList.addListSelectionListener(ListSelectionListener)
.
See How to Write a List List Lister for more examples .
you haven't tried it at first, right? Swing does almost all of the input related to listeneres. Check your mouse listener or adjust the awning below
fooobar.com/questions/92460 / ...
For completeness, I am quoting it here:
String[] items = {"A", "B", "C", "D"};
JList list = new JList(items);
list.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
JList list = (JList)evt.getSource();
if (evt.getClickCount() == 2) {
int index = list.locationToIndex(evt.getPoint());
} else if (evt.getClickCount() == 3) { // Triple-click
int index = list.locationToIndex(evt.getPoint());
}
}
});
I'm sure you can also do it with one click ... if you don't write a comment