gocan76
gocan76

Reputation: 39

Closing a JFrame

I am working on a jtable. For every row of this table (when I select it) I need to open a new jframe. After I open the application, I do a simple search using a button and I select a row for the first time, it works ok; the problem starts when I close this new jframe just opened I execute another search and I select
another row; in practice more than one jframe with the same content is opened. It is like more than one request is sent.

This code is in the main app:

private static String QUERY_BASED_ON_SITE2="from WordsToFind a where a.wordToFindName like '";

private void runQueryBasedOnName2() {
     executeHQLQuery2(QUERY_BASED_ON_SITE2 + jTextField14.getText() + "%'");
}

private void executeHQLQuery2(String hql2) {
    try {
        Configuration configuration = new Configuration().configure();
        StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
        SessionFactory factory = configuration.buildSessionFactory(builder.build());
        Session session = factory.openSession();
        Query q2 = session.createQuery(hql2);
        List resultsWords2 = (List) q2.list();
        System.out.println("resultsWords2 ----> " + resultsWords2);
        displayWords2(resultsWords2);
        } catch (ClassNotFoundException ex) {
        Logger.getLogger(ClientEditor.class.getName()).log(Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        Logger.getLogger(ClientEditor.class.getName()).log(Level.SEVERE, null, ex);
    }
}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    if(!jTextField14.getText().trim().equals("")) {
        runQueryBasedOnName2();
    }        

}

private void displayWords2(List resultsWords2) throws ClassNotFoundException, InstantiationException {                       
        Vector<String> tableHeaders2 = new Vector<String>();
        final Vector tableWords2 = new Vector();   
        tableHeaders2.add("Word Id");
        for(Object ow : resultsWords2) {
              WordsToFind words;
              words = (WordsToFind)ow;
              Vector<Object> oneRowWords = new Vector<Object>();
              oneRowWords.add(words.getWordToFindId());
              tableWords2.add(oneRowWords);
                        }     
    jTable4.setModel(new DefaultTableModel(tableWords2, tableHeaders2));         
    jTable4.setAutoCreateRowSorter(true);
    jTable4.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    jTable4.addMouseListener(new MouseAdapter() {
              public void mouseClicked(MouseEvent e) {      
                int selectedRow = jTable4.getSelectedRow();                                
                selectedRow = jTable4.convertRowIndexToModel(selectedRow);
                SimpleSwingBrowser browser = new SimpleSwingBrowser();
                browser.setVisible(true);                                                              
                browser.loadURL("http://www.google.com");
                browser.highlihtWord();                                                          
                }
              });
}

I tried adding the follwing line of code into the SimpleSwingBrowser class

and

dispose();

also

this.dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));

I obtained the same results. Thanks Alb

Upvotes: 0

Views: 244

Answers (4)

Ram Mandal
Ram Mandal

Reputation: 1959

a bit of code would be very helpful to debug your problem. I think you should name the instance to null when you are using method

dispose()

please let me know if this help you. or if you get solution please share with us. thanks bro

Upvotes: 0

Huang Chen
Huang Chen

Reputation: 1205

Setting the default operation to exit on close would work. Make sure to set that for the jframes you intend to close

JFrame frame= new JFrame()
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Using dipose() for the jframe you open on click could also work

http://chortle.ccsu.edu/java5/notes/chap56/ch56_9.html

Upvotes: 0

ooozguuur
ooozguuur

Reputation: 3476

The JFrame gets the event to close and upon closing, exits.

    JFrame frame= new JFrame()
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // frame code here ..
    frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));

Upvotes: 1

Rahul
Rahul

Reputation: 299

When you create new JFrames from your current JFrame , make sure it has this piece of code

setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

Note : Don't make new JFrames of same class , create a seperate exteneded JFrame for that.

Upvotes: 0

Related Questions