0

I have one JFrame open another JFrame, but when I try and click on the second JFrame when it pops up, nothing clicks. The thing is, if I select the old JFrame (click on it) and then select the new one, the new one will then work. The problem is I need it to work when it pops up because obviously I don't want users to have to click a hundred places to get the program to work.

Here is the code that calls/creates the new JFrame:

public void displayData(ArrayList<Judge> novice, ArrayList<Judge> advanced) {
    DisplayWindow w = new DisplayWindow(novice, advanced);
    w.setLocation(this.getLocation().x+20, this.getLocation().y+20);
    w.setVisible(true);
    w.setState(JFrame.NORMAL);
}

Here is the code for "DisplayWindow" (I suspect that the methods at the top have nothing to do with it because they don't manage the JFrame directly at all, but I still posted them):

public class DisplayWindow extends JFrame {
private JPanel contentPane;
private ArrayList<Judge> novice;
private ArrayList<Judge> advanced;

/**
 * Create the frame.
 */
public static String getSingleText(ArrayList<Judge> d) {
    StringBuilder product = new StringBuilder("");
    for (Judge j : d) {
        product.append(String.format("Judge: %s ", j.toString() + (j.getDebates().get(0).isAdvanced() ? 'A' : 'N')));
        for (Debate de : j.getDebates()) {
            product.append(String.format("Round: %s ", de != null ? de.toString() : "null"));
        }
        product.append("\n");
    }
    return product.toString();
}
public String getText(ArrayList<Judge> novice, ArrayList<Judge> advanced) {
    StringBuilder product = new StringBuilder("");
    product.append("Advanced:\n");
    product.append(getSingleText(advanced));
    product.append("\nNovice:\n");
    product.append(getSingleText(novice));
    return product.toString();
}
public DisplayWindow(ArrayList<Judge> novice, ArrayList<Judge> advanced) {
    this.novice = novice;
    this.advanced = advanced;

    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    contentPane.setLayout(new BorderLayout(0, 0));
    setContentPane(contentPane);


    JTextArea textArea = new JTextArea(getText(novice, advanced));
    textArea.setEditable(false);
    JScrollPane scroll = new JScrollPane(textArea);
    contentPane.add(scroll, BorderLayout.CENTER);

    JLabel lblNewLabel = new JLabel("Debate Calculation:");
    contentPane.add(lblNewLabel, BorderLayout.NORTH);

    JPanel panel = new JPanel();
    contentPane.add(panel, BorderLayout.SOUTH);

    JButton btnSave = new JButton("Save");
    panel.add(btnSave);
}

}

And, just in case, here is the code for how the old JFrame was created:

public DataWindow() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    contentPane.setLayout(new BorderLayout(0, 0));
    setContentPane(contentPane);
    contentPane.setLayout(new BorderLayout(0, 0));
    setLocationRelativeTo(null);
    String[] columnNames = {"School",
            "Advanced #",
            "Novice #",
    };
    Object[][] data = {

            };  

    DefaultTableModel model = new DefaultTableModel(columnNames, 0); 
    table = new JTable(model){
        public Class getColumnClass(int column) {
            switch (column) {
                case 0:
                    return String.class;
                case 1:
                    return Integer.class;
                case 2:
                    return Integer.class;
                case 3:
                    return Integer.class;
                default:
                    return Integer.class;
            }
        }
    };
    JScrollPane scrollPane = new JScrollPane(table);
    table.setFillsViewportHeight(true);
    contentPane.add(scrollPane, BorderLayout.CENTER);


    JPanel panel = new JPanel();
    contentPane.add(panel, BorderLayout.NORTH);
    panel.setLayout(new WrapLayout(FlowLayout.CENTER, 5, 5));
    chckbxCantJudgeOwn.setSelected(true);

    panel.add(chckbxCantJudgeOwn);
    chckbxCantDebateOwn.setSelected(true);

    panel.add(chckbxCantDebateOwn);
    chckbxCantJudgeSchool.setSelected(true);

    panel.add(chckbxCantJudgeSchool);
    chckbxCantDebateSchool.setSelected(true);

    panel.add(chckbxCantDebateSchool);
    chckbxCantDebateSchool.setSelected(true);

    JPanel panel_1 = new JPanel();
    contentPane.add(panel_1, BorderLayout.SOUTH);
    panel_1.setLayout(new GridLayout(0, 2, 0, 0));

    JButton btnNewButton = new JButton("+");
    btnNewButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            DefaultTableModel model = (DefaultTableModel) table.getModel();
            model.addRow(new Object[]{"?????", 0, 0});
        }
    });
    panel_1.add(btnNewButton);

    JButton button = new JButton("-");
    button.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            int[] selectedRows = table.getSelectedRows();
            if (selectedRows.length > 0) {
                for (int i = selectedRows.length - 1; i >= 0; i--) {
                    DefaultTableModel model = (DefaultTableModel) table.getModel();
                    model.removeRow(selectedRows[i]);
                }
            }
        }
    });
    panel_1.add(button);

    btnCancel.setEnabled(false);
    btnCancel.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            processer.stop();
            toggleButtons();
        }
    });
    btnProcess.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            processer = new Thread(new Runnable() {
                public void run() {
                    startProcess(getData(table));                       
                }
            });
            processer.start();
            toggleButtons();

        }
    });
    panel_1.add(btnProcess);
    panel_1.add(btnCancel);
}
sinθ
  • 11,093
  • 25
  • 85
  • 121
  • 1
    ...or you could just use plain JOptionPane message dialog and pass content pane within, instead of using multiple JFrames. As it should be...because: http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-bad-practice Simplest solution! – Branislav Lazic Apr 30 '13 at 22:37
  • @brano88 How would I set the content pain of a JOptionPane? (Is that what you mean?) – sinθ Apr 30 '13 at 22:44
  • 1
    Extend you DisplayWindow class with JPanel. Or even better, make method which returns JPanel. Create instance of that class (like: `DisplayWindow dw = new DisplayWindow();`) and pass it like this: `JOptionPane.showMessageDialog(null,dw,"Title",JOptionPane.PLAIN_MESSAGE);`. JOptionPane is modal and it will block access to your main frame while being displayed. – Branislav Lazic Apr 30 '13 at 22:49
  • @brano88, but now I can only have one open at a time. The JOptionPane stops you from using the first JFrame. – sinθ Apr 30 '13 at 23:15
  • 1
    Of course it does! Thats the purpose of modal dialog. Wait....Don't tell me you are using two JFrames in parallel? Ever heard for `JTabbedPane` or `CardLayout`? Btw, tell me for one popular application which uses multiple JFrames? (Except of Gimp! Which is God awful!) – Branislav Lazic Apr 30 '13 at 23:20
  • @brano88 I don't know which language these are programmed in but: Photoshop, Chrome, Safari, Xcode, Word... they all have multiple windows. Maybe I'm misunderstanding something. I'm new to using swing. – sinθ Apr 30 '13 at 23:30
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/29215/discussion-between-mike-g-and-brano88) – sinθ Apr 30 '13 at 23:31

0 Answers0