Question

The following code displays a list on the left hand side and the content on the right hand side. So when an item in the list is selected it should display something on the right hand side (this uses split panes). The problems is that it does not display when an item is selected instead it displays it when you click the divider. I was wondering what is wrong with the following code that it does not automatically show the content when and item from the list is selected. Thanks in advance.

Below shows the partial code on where I am having troubles with:

public class FrequentQuestions implements ActionListener{

@Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    frame = new MyInternalFrame("Frequently Asked Questions");

    String [] options = {"How to open/save images", "formats", "info"};
    JList list = new JList(options);
    panelQuestions.add(list);
    list.addListSelectionListener(new ListSelectionListener(){

        @Override
        public void valueChanged(ListSelectionEvent e) {
            // TODO Auto-generated method stub
            if(e.getValueIsAdjusting())
                return;

            //String [] options2 = {"hello","bye"};
            //JList list2 = new JList(options2);
            JList list = (JList) e.getSource();


            if (list.isSelectionEmpty()) {
                System.out.println("list selection is empty!");
            }
            else{
                int index = ((JList)e.getSource()).getSelectedIndex();
                if(index == 0){
                    readFile();
                }
                else if(index == 1){
                    System.out.println("2nd item selected");

                }
                else{
                    System.out.println("3rd item selected");
                }
            }
        }

    });

    scroll = new JScrollPane(panelAnswers,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, panelQuestions, scroll);
    pane.setAutoscrolls(true);
    pane.setOpaque(true);

    panelQuestions.setMinimumSize(new Dimension(200,50));
    panelQuestions.setBackground(new Color(0,0,0,0));
    panelAnswers.setMinimumSize(new Dimension(100,30));
    pane.setOneTouchExpandable(true);
    pane.setDividerLocation(250);

    frame.add(pane);
    frame.setVisible(true);
    desk.add(frame);

     try {
            frame.setSelected(true);
    } catch (java.beans.PropertyVetoException e1) {

    }

}
 }

Here is the full code (it might not compile because I have the text files, I don't know if this will help):

 public class FrequentQuestions implements ActionListener{

private int openFrameCount;
private JDesktopPane desk;
private JTextArea Tarea;
private JScrollPane scroll;
private BufferedReader in ;
JPanel panelQuestions = new JPanel();
JPanel panelAnswers = new JPanel();
JTextArea text = new JTextArea();
//private FrequentQuestions quest;
JSplitPane pane ;
MyInternalFrame frame;

public FrequentQuestions(JDesktopPane desktop) {
    // TODO Auto-generated constructor stub
    desk = desktop;
}

@Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    frame = new MyInternalFrame("Frequently Asked Questions");

    String [] options = {"How to open/save images", "formats", "Information"};
    JList list = new JList(options);
    //panelQuestions.add(list);
    list.addListSelectionListener(new ListSelectionListener(){

        @Override
        public void valueChanged(ListSelectionEvent e) {
            // TODO Auto-generated method stub
            if(e.getValueIsAdjusting() == false)
                return;

            String [] options2 = {"hello","bye"};
            JList list2 = new JList(options2);
            JList list = (JList) e.getSource();


            if (list.isSelectionEmpty()) {
                System.out.println("list selection is empty!");
            }
            else{
                int index = ((JList)e.getSource()).getSelectedIndex();
                if(index == 0){
                    readFile();
                    System.out.println("I am outputting!");
                }
                else if(index == 1){
                    readFormatFile();
                    System.out.println("2nd item selected");

                }
                else{
                    System.out.println("3rd item selected");
                }
            }
        }

    });

    JScrollPane scroll1 = new JScrollPane(list,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    scroll = new JScrollPane(panelAnswers,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, scroll1, scroll);
    pane.setAutoscrolls(true);
    pane.setOpaque(true);

    panelQuestions.setMinimumSize(new Dimension(259,50));
    panelQuestions.setBackground(new Color(0,0,0,0));
    panelAnswers.setMinimumSize(new Dimension(340,30));
    pane.setOneTouchExpandable(true);
    pane.setDividerLocation(290);


    frame.add(pane);
    frame.setVisible(true);
    desk.add(frame);

     try {
            frame.setSelected(true);
    } catch (java.beans.PropertyVetoException e1) {

    }

}

private class MyInternalFrame extends JInternalFrame {

    static final int xPosition = 30, yPosition = 30;
    public MyInternalFrame(String title) {
        super(title, true,true, true, true);
        setSize(600,500);

        // Set the window's location.
        setLocation(xPosition * openFrameCount, yPosition * openFrameCount);
    }
}

public void readFile(){
    panelAnswers.removeAll();
    try {
        in = new BufferedReader(new FileReader("openSave.txt"));
    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    String line;
    String file = ""; 
    try {
        while((line = in.readLine()) != null)
        {
            System.out.println(line);
            file += line;
            file +="\n";

        }
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    try {
        in.close();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    JTextArea Tarea = new JTextArea();
    //System.out.println(file);

    Tarea.setText(file);
    Font f = new Font("TimesNewRoman", Font.ROMAN_BASELINE, 12);
    Tarea.setFont(f);
    Tarea.setBackground(new Color(0,0,0,0));
    Tarea.setEditable(false);
    panelAnswers.add(Tarea);

}

  public void readFormatFile(){
    panelAnswers.removeAll();
    try {
        in = new BufferedReader(new FileReader("format.txt"));
    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    String line;
    String file = ""; 
    try {
        while((line = in.readLine()) != null)
        {
            System.out.println(line);
            file += line;
            file +="\n";

        }
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    try {
        in.close();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    JTextArea Tarea = new JTextArea();
    //System.out.println(file);

    Tarea.setText(file);
    Font f = new Font("TimesNewRoman", Font.ROMAN_BASELINE, 12);
    Tarea.setFont(f);
    Tarea.setBackground(new Color(0,0,0,0));
    Tarea.setEditable(false);
    panelAnswers.add(Tarea);

}
 }
Was it helpful?

Solution

Here is a working ListSelectionListener:

public class FrequentQuestions extends JFrame {

    final JLabel label = new JLabel("TEXT");

    public FrequentQuestions() {

        JList<String> list = new JList<String>(new String[]{"A", "B"});
        list.addListSelectionListener(new ListSelectionListener() {

            public void valueChanged(ListSelectionEvent e) {

                if (e.getValueIsAdjusting())
                    return;
                JList<String> list = (JList<String>) e.getSource();
                label.setText(list.getSelectedValue());
            }
        });

        JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, list, label);
        add(split);

        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }

    public static void main(String[] args) {

        new FrequentQuestions();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top