-1

I found an answer to a question asked here however I am very confused on how to implement it in my program. I won't post my entire program because it is very long and I'm not sure how to isolate this example. Really I just need a little clarification on how I can make it work.

Basically what I have is two different GUI's. The first GUI is a custom double list that will allow the user to move items between list one and list two. The second GUI will be contained in a while loop. For each of the items moved to list two a separate JOptionsPane will appear allowing the user to choose a reason code for each item.

The problem I was having was that when I attempted to run the first custom GUI followed by the second GUI, they would appear at the same time. I understand why, the custom GUI is not like a JOptionsPane and does not just magically stop the rest of the code from going. I tried adding a conditional variable to the second GUI that would prevent it from running unless the user clicked confirm in the double list but this did not work as well. Below is the code I found here I am confused about

new Thread(new Runnable(){
                        @Override
                        public void run(){
                        ListGUI gui = new ListGUI();
                        gui.createAndShowGUI(); 
                        unlockWaiter();
                        }
                    }).start();

                    System.out.println("test");

                    waitForThread();

                    System.out.println("test2");



                }

and

public static void waitForThread(){
        monitorState=true;
        while(monitorState){
            synchronized(monitor){
                try{
                    monitor.wait();
                }catch (Exception e) {}
            }
        }
    }

and

public static void unlockWaiter(){
        synchronized(monitor){
            monitorState=false;
            monitor.notifyAll();
        }
    }

As you can see from the top example I tried my best to implement this code first by testing it. The "gui.createAndShowGUI" is simply my double list method.

What I am confused about is what I should put in the place where the "test" and "test2" is printed. If my second GUI is a method to call a JOptionPane (I need it to be contained in a loop) where do I put this method call in the below code?

How do I ensure that the code will stop when the double list appears and only continue to the second GUI when the user clicks the continue button?

Sorry I don't have more code to show but I'm so confused I could get any farther than this.

SOLUTION:

The solution for me was not to use a custom GUI but to instead use a JOptionPane with an added panel. Since JOptionPane already has the functionality to hold everything else until the process is complete I found this worked best. I'll post my code below for anyone curious how I did it:

package jpaneltest;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;

public class JOptionPaneExample

{

    public static JList outputDetails, inputDetails;
    public static JButton buttonin;
    public static JButton buttonout;
    public static JButton buttontest;
    public static ArrayList<String> list = new ArrayList<String>();
    public static DefaultListModel input;
    public static DefaultListModel output;

    public static void displayGUI(){

        JOptionPane.showConfirmDialog(null, getPanel(),"JOptionPane Example : ", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);

    }


    private static JPanel getPanel(){

        JPanel panel = new JPanel();



        list.add("1");
        list.add("2");
        list.add("3");

        input = new DefaultListModel();
        output = new DefaultListModel();

        String[] shoppingItems = new String[BufferedWriterTester.entryDetails.size()];
        shoppingItems = BufferedWriterTester.entryDetails.toArray(shoppingItems);

        for(int i = 0; i < shoppingItems.length; i++){
            input.addElement(shoppingItems[i]);
        }

        outputDetails = new JList(input);
        outputDetails.setVisibleRowCount(10);
        outputDetails.setFixedCellHeight(20);
        outputDetails.setFixedCellWidth(140);
        outputDetails.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

        JScrollPane list1 = new JScrollPane(outputDetails);

        inputDetails = new JList(output);
        inputDetails.setVisibleRowCount(10);
        inputDetails.setFixedCellHeight(20);
        inputDetails.setFixedCellWidth(140);
        inputDetails.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

        JScrollPane list2 = new JScrollPane(inputDetails);

        JPanel buttonPanel = new JPanel();

        buttonin = new JButton(">>");
        buttonin.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e){
                int[] fromindex = outputDetails.getSelectedIndices();
                Object[] from = outputDetails.getSelectedValues();

                for(int i=0; i< from.length; i++){
                    output.addElement(from[i]);
                }

                for(int i = (fromindex.length-1); i>=0; i--){
                    input.remove(fromindex[i]);
                }
            }
        });
        buttonPanel.add(buttonin);

        buttonout = new JButton("<<");
        buttonout.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e){
                Object[] to = inputDetails.getSelectedValues();
                int[] toindex = inputDetails.getSelectedIndices();

                for(int i = 0; i < to.length; i++){
                    input.addElement(to[i]);
                }

                for(int i = (toindex.length-1); i >=0; i--){
                    output.remove(toindex[i]);
                }
            }
        });
        buttonPanel.add(buttonout);

        buttontest=new JButton("Test");
        JPanel bottomPanel = new JPanel();
        bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.LINE_AXIS));

        bottomPanel.add(Box.createRigidArea(new Dimension(10,0)));
        bottomPanel.add(list1);
        bottomPanel.add(Box.createRigidArea(new Dimension(5,0)));
        bottomPanel.add(buttonPanel);
        bottomPanel.add(Box.createRigidArea(new Dimension(5,0)));
        bottomPanel.add(list2);
        bottomPanel.add(Box.createRigidArea(new Dimension(10,0)));


        return panel;
    }


}
jesric1029
  • 698
  • 3
  • 10
  • 33
  • 1
    I'm not sure blocking would work like that. Swing is event based: http://stackoverflow.com/a/13520062 and even 2 windows will share the same thread. I don't know where you got that code from but it's not safe because you can't guarantee that unlock runs after lock. Maybe https://docs.oracle.com/javase/tutorial/uiswing/concurrency/ can explain how it works. A gui is not a thread, so maybe you have to change how you do it fundamentally. – zapl Nov 19 '15 at 15:54
  • Any advice on how I might get a custom GUI to run and then follow it with a JOptionsPane that only runs once the user has closed out the custom GUI? – jesric1029 Nov 19 '15 at 16:11
  • Not really sure how you mean that. Add some code that demonstrates the principle you envision. – zapl Nov 19 '15 at 18:48

1 Answers1

1

After taking zapls feedback I took a second look into my options. I discovered that for what I intended the best solution was to simply create a JOptionsPane with an extra panel added in that could handle the above requirements. This way the process is simplified and automatic.

jesric1029
  • 698
  • 3
  • 10
  • 33