0

In swing based project i create a master detail class using wizard and populate J Table and create a button Record in another class and put object of master detail class in action listener of Record button.But when i click Record button master detail class run butt not show GUI window while when i run it as main class or run direct it run successfully and show GUI window and also show record from database.I am confused because other classes run on click Record button if i replace object of master client class with any other class but only master detail class not show GUI window.Here is code of two class one is master detail with name of BrRecord and other class which handle it with name of Balance.Thanks.

 package my.soft;

import java.awt.EventQueue;
import java.beans.Beans;
import java.util.ArrayList;

import java.util.List;
import javax.persistence.RollbackException;
import javax.swing.JFrame;
import javax.swing.JPanel;

 public class BrRecord extends javax.swing.JPanel {

    public BrRecord() {
        initComponents();
        if (!Beans.isDesignTime()) {
            entityManager.getTransaction().begin();
        }
    } 

 private void refreshButtonActionPerformed(java.awt.event.ActionEvent evt) {                                              
        entityManager.getTransaction().rollback();
        entityManager.getTransaction().begin();
        java.util.Collection data = query.getResultList();
        for (Object entity : data) {
            entityManager.refresh(entity);
        }
        list.clear();
        list.addAll(data);
    } 

  private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
        int[] selected = masterTable.getSelectedRows();
        List<my.soft.Expance1> toRemove = new ArrayList<my.soft.Expance1>(selected.length);
        for (int idx = 0; idx < selected.length; idx++) {
            my.soft.Expance1 e = list.get(masterTable.convertRowIndexToModel(selected[idx]));
            toRemove.add(e);
            entityManager.remove(e);
        }
        list.removeAll(toRemove);
    }   

 private void newButtonActionPerformed(java.awt.event.ActionEvent evt) {                                          
        my.soft.Expance1 e = new my.soft.Expance1();
        entityManager.persist(e);
        list.add(e);
        int row = list.size() - 1;
        masterTable.setRowSelectionInterval(row, row);
        masterTable.scrollRectToVisible(masterTable.getCellRect(row, 0, true));
    }  

 private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
        try {
            entityManager.getTransaction().commit();
            entityManager.getTransaction().begin();
        } catch (RollbackException rex) {
            rex.printStackTrace();
            entityManager.getTransaction().begin();
            List<my.soft.Expance1> merged = new ArrayList<my.soft.Expance1>(list.size());
            for (my.soft.Expance1 e : list) {
                merged.add(entityManager.merge(e));
            }
            list.clear();
            list.addAll(merged);
        }
    } 

   private javax.swing.JTextField breakfastField;
    private javax.swing.JLabel breakfastLabel;
    private javax.swing.JTextField dateField;
    private javax.swing.JLabel dateLabel;
    private javax.swing.JButton deleteButton;
    private javax.persistence.EntityManager entityManager;
    private javax.swing.JTextField expance1Field;
    private javax.swing.JLabel expance1Label;
    private java.util.List<my.soft.Expance1> list;
    private javax.swing.JScrollPane masterScrollPane;
    private javax.swing.JTable masterTable;
    private javax.swing.JButton newButton;
    private javax.persistence.Query query;
    private javax.swing.JButton refreshButton;
    private javax.swing.JButton saveButton;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;

 public static void main(String[] args) {   EventQueue.invokeLater(new Runnable() {
            public void run() {
                new BrRecord().setVisible(true);

              JFrame frame = new JFrame();
               frame.setContentPane(new BrRecord());
               frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
               frame.pack();
              frame.setVisible(true);

            }
        });
    }

}

Balance source

 package my.soft;



//import static com.sun.org.apache.xalan.internal.lib.ExsltDatetime.date;
import static com.sun.corba.se.spi.presentation.rmi.StubAdapter.request;
import java.awt.Toolkit;
import java.awt.event.WindowEvent;
import static java.lang.System.out;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import static java.text.DateFormat.Field.DAY_OF_MONTH;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import static java.util.Calendar.MONTH;
import static java.util.Calendar.YEAR;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

 public class Balance extends javax.swing.JFrame {    public Balance() {
        initComponents();
        CurrentDate();
         conn = MySql.connectToDB();
    } 

 private void bRecordActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:

         BrRecord Br = new BrRecord();

        Br.setVisible(true);
               // DISPOSE_ON_CLOSE();
        Close();



    }  

 public static void main(String args[]) {

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Balance().setVisible(true);
            }
        });
    }                 
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Suraj Ali
  • 21
  • 7
  • 1
    1) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) 2) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example). Hard code some data to replace the DB calls. 3) A single blank line of white space in source code is all that is *ever* needed. Blank lines after `{` or before `}` are also typically redundant. – Andrew Thompson Sep 27 '14 at 07:14
  • 1
    What do you expect these two lines to do? `BrRecord Br = new BrRecord(); Br.setVisible(true);`. `BrRecord` is a panel. Without a visible parent container, it's useless – Paul Samsotha Sep 27 '14 at 07:16
  • If BrRecord Br = new BrRecord(); Br.setVisible(true); is useless then why it run and show GUI window if i run it direct as main class and what is alternative to invoke BrRecord on clicking a button? – Suraj Ali Sep 27 '14 at 07:21
  • Because you are adding it here `frame.setContentPane(new BrRecord());`. Not sure what you're trying to do, but maybe you want to have a reference to the `BrRecord` you are adding to the frame, and just change some state in _that_ panel, instead of trying to add a whole new one. Also, this `new BrRecord().setVisible(true);` does nothing either. But then again, I have no idea what you're trying to accomplish here – Paul Samsotha Sep 27 '14 at 07:24
  • i create BrRecord class using wizard and its all code generated by netbean i only want to invoke from other class if you mad some change then please do it i am new in java thanks – Suraj Ali Sep 27 '14 at 07:29
  • 1
    Your best bet is to create a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) that we can cop-paste-compile-run, that demonstrates the problem. For one, this code has too many references to methods/classses that are not here, that would cause it to not compile. And secondly, you have db access code, that would cause exceptions if we tried to run it. See if you can create the MCVE. Make sure to test it to make sure it runs, and demonstrates the problem. Take out the DB access code, because it doesn't seem like that's part of the problem – Paul Samsotha Sep 27 '14 at 07:34

0 Answers0