1

I want avoid the rest of the code getting executed till the user responds to the jtable ( wait till he cancel it to display the JOptionPane

import java.awt.*;

import javax.swing.*;

public class test {

public static void main(String args[]) {

String[][] rows = { { "Sameendra", "MCA","sam", "Old" }, { "vinod", "M.ED","sam", "Old" }, { "Suman", "BIT","sam", "Old" }, { "Amit", "Msc","sam", "Old" }, {"Deepak", "BIT","sam", "Old"}, { "Ravi", "MCA","sam", "Old" },// { "Sameendra", "MCA","sam", "Old" }, { "vinod", "M.ED","sam", "Old" }, };
};

    String headers[] = { "Upper", "Lower", "New", "Old" };
    JTable table = new JTable(rows, headers);
    table.setEnabled(true);



    JScrollPane scrollPane = new JScrollPane(table);
    JFrame frame = new JFrame("New Table");


    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setLocationRelativeTo(frame);
    frame.setLocation(500,325);
    frame.setSize(300, 168);
    frame.setVisible(true);
    frame.setFocusable(true);
    JOptionPane.showMessageDialog(null, "Goback?", "return",   

    JOptionPane.INFORMATION_MESSAGE);

2 Answers2

1

Remember, most GUI's are event driven environments, this means that it's possible to have multiple windows and controls on the screen at one time and the user can pick and choose what they want to do.

If you want to stop the code execution until the user closes a window, then you need to use a modal dialog of some kind. This will stop the code executing at the point the dialog is made visible and will continue once the dialog is closed.

Instead of using JFrame, you could use JDialog or more easily, use a JOptionPane, passing the JTable as message object.

Take a look at How to use dialogs for more details and How do i make the output come in different columns? for example...

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Thank you, will try the JDioalog later, JOption seems fine for the moment :) though it doesn't look good Thanks anyway – user3068640 Dec 05 '13 at 06:06
  • JOptionPane uses a JDialog to display its content and very flexible in allow you to provide your own custom options (buttons) – MadProgrammer Dec 05 '13 at 06:52
0

to request focus, u can use scrollpane.requestFocusInWindow();

Gagan93
  • 1,826
  • 2
  • 25
  • 38