3

I am trying to create an UI which has two panes.

In the left pane I display the list of files and right pane displays the contents.

Now, I want list of files in the left pane to look as a normal list. But when I click an entry in this list, the contents of the particular file should be displayed in the right pane.

How can I achieve this using Swing?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Hashken
  • 4,396
  • 7
  • 35
  • 51
  • The files are created by another part of the program. So, I am sure that the files are readable ASCII files. – Hashken Jan 31 '13 at 12:23

6 Answers6

5

Here I have done a short example, with the help of JList on the left and JTextArea on right. I have used ListSelectionListener to get the item list change. Use a LayoutManager as per your convenience.

enter image description here

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();
            }
        });
    }
}
joey rohan
  • 3,505
  • 5
  • 33
  • 70
  • +1 for [sscce](http://sscce.org/). Note `pack()` _before_ `setVisible()` and more sparing use of white space. Also consider `EXIT_ON_CLOSE`. – trashgod Jan 31 '13 at 13:01
  • @trashgod thanks for the edit. `JList.addListSelectionListener` is invoked 2 times as soon as you click,but, you cannot make out just by looking at the interface because i am just setting the text on `JTextArea`, any reasons?,`EXIT_ON_CLOSE` edited :) – joey rohan Jan 31 '13 at 13:07
  • 1
    Print `evt.getValueIsAdjusting()` and watch as you click and drag over the list. – trashgod Jan 31 '13 at 15:38
  • @trashgod oh god! thanks! was not even knowing that function exist! :) – joey rohan Jan 31 '13 at 15:41
3

Check out this tutorial. It explains how to use lists in Swing, including event handlers that are necessary to register for click events.

RoflcoptrException
  • 51,941
  • 35
  • 152
  • 200
3

You might want to look at this JTree example.

Jayamohan
  • 12,734
  • 2
  • 27
  • 41
3

first off, you have not even tried yet, right? Swing does almost everything input related with listeneres. Check out the mouse listener, or adjust the awnser giving below

https://stackoverflow.com/a/4344762/258418

For completness I quote 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 am sure you can make it take single clicks as well,... if not write a comment

Community
  • 1
  • 1
ted
  • 4,791
  • 5
  • 38
  • 84
  • 2
    The `JList` API suggests a `MouseListener` / `MouseAdapter` for multiple clicks, but wouldn't a `ListSelectionListener` be better for single clicks? – trashgod Jan 31 '13 at 12:16
  • I guess you are right... (unless I construct something absurd where doubleclicking is needed to change the selection, and singleclicking displays info) – ted Jan 31 '13 at 18:23
  • A `ListSelectionListener` also sees keyboard navigation, and there can be multiple listeners. – trashgod Jan 31 '13 at 20:51
3

Some examples for reference:

  • FileBrowser uses a JTree on the left and nested detail panels on the right.

  • ImageDisplay embeds a custom JFileChooser on the left and displays a scrollable image on the right.

  • CheckTable shows a JTable on the left and a DisplayPanel on the right.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
3

Use JList.addListSelectionListener(ListSelectionListener).

See How to Write a List Selection Listener for more examples.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Sergiy Medvynskyy
  • 11,160
  • 1
  • 32
  • 48