0

i updated the code because many people doesn't understand so write a simple representation for that. here is the problem whenever i clicked the button it open a new frame but i dont want this it does't open a new frame it remain open the same frame.

code for main frame :

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class JavaProject2_27 {

    private JFrame frame;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    JavaProject2_27 window = new JavaProject2_27();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public JavaProject2_27() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JButton btnClicked = new JButton("Clicked");
        btnClicked.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                JavaProject2_28 obj=new JavaProject2_28();
                obj.getJavaProject2_28();
            }
        });
        btnClicked.setBounds(150, 99, 89, 23);
        frame.getContentPane().add(btnClicked);
    }
}

code for the second frame :

import java.awt.EventQueue;

import javax.swing.JFrame;

public class JavaProject2_28 {

    private JFrame frame;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    JavaProject2_28 window = new JavaProject2_28();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public JavaProject2_28() {
        initialize();
    }

    public void getJavaProject2_28()
    {
        frame.setVisible(true);
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}

snapshot

  • So your question is "erase the details of Supplier ID and Supplier Name fields after press suplier_info enter button" is it? – Blasanka Jun 06 '17 at 05:34
  • After press the Enter button of Product Info panel it open the list of product frame which contain the list of product in jtable. After inserting the perticular detail from jtable to Person Code and Person Name textfield. My previous information in Supplier Info Panel was erased. Why this information is erased. I want this information. – Mohammad Azaz Jun 06 '17 at 06:57
  • Yes, it is but in want this information in supplier panel. – Mohammad Azaz Jun 06 '17 at 06:59
  • If you want to get an answer, you should create a runnable example ([SSCCE](http://sscce.org)) which demonstrates your problem – Sergiy Medvynskyy Jun 06 '17 at 07:58
  • I don't get it. – Mohammad Azaz Jun 06 '17 at 08:17
  • He said that you have to give code snippet that we can run and check because we cannot say what happen without seeing what did you done. also your code that shared here is incomplete and where is the implementation of `getDataJavaProject2_25()` – Blasanka Jun 06 '17 at 08:50
  • 1) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) 2) Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is an `UPPER_CASE_CONSTANT`) and use it consistently. 3) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 4) Also, it's not entirely clear from the uncompilable code snippets if explicitly setting the bounds of components is counterproductive or merely pointless, but.. – Andrew Thompson Jun 06 '17 at 08:57
  • .. Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Jun 06 '17 at 08:59

2 Answers2

0

I was checking your earlier program, still cannot find the implementation of getDataJavaProject2_23(); method in those three classes that you provided.

First of all answer to your question few things to mention:

  1. Use proper naming convention, as a example: JavaProject2_28 this class name didnot make any sense to us or even for you if you look at this after a week or two.
  2. You can divide your program for more classes, as a example: database data handling, GUI and etc.
  3. And another important thing is multiple JFrames for one application is not good because of even if you look at your task bar when running your program you can see there is multiple icons, it is difficult to deal with your application. There is more disadvatages about this. read this to get clear idea. Instead of multiple JFrames you can use one JFrame and multiple JPanel with appropriate layouts.

Answer for your updated question:

whenever i clicked the button it open a new frame but i dont want this it does't open a new frame it remain open the same frame.

I checked your program it is work fine(after press the button open up the other frame).

You can do some changes that are unnecessary:

Remove this function

public void getJavaProject2_28()
{
    frame.setVisible(true);
}

And add frame.setVisible(true); to the initialize() method.

Solution:

Add frame.setVisible(true) to button action. Like below:

        }
    });
    btnClicked.setBounds(150, 99, 89, 23);
    frame.getContentPane().add(btnClicked);
    frame.setVisible(true);
}

And in the main frame(which is button button added) change like below:

JButton btnClicked = new JButton("Clicked");
btnClicked.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        JavaProject2_28 obj=new JavaProject2_28();
    }
});

Also you do not need the main() method in both if you are only open second JFrame in main frame button.

Blasanka
  • 21,001
  • 12
  • 102
  • 104
  • `public JavaProject2_23() { initialize(); con=JavaProject2_24.Database(); Supplier_List(); } public void getDataJavaProject2_23() { frame.setVisible(true); } public void Supplier_List() { try { String query="select * from Supplier_Table"; PStat=con.prepareStatement(query); res=PStat.executeQuery(); table.setModel(DbUtils.resultSetToTableModel(res)); } catch(Exception e) { JOptionPane.showMessageDialog(null, e); } }` – Mohammad Azaz Jun 06 '17 at 16:16
  • it is not working afterall new frame is open whenever we click on button. – Mohammad Azaz Jun 06 '17 at 16:27
  • whenever i click on button it remain on the same frame it dowsnot open any new frame i want this. – Mohammad Azaz Jun 06 '17 at 16:28
  • the problem is remain the same after all new frame open multiple time when i hit the button. i want that it only open once when i press button multiple times. when i press the button 3 times three individual frame is open that not happining.i want that when i press button 3 or 4 times only single frame will open. – Mohammad Azaz Jun 06 '17 at 16:38
  • NO multiple click only one frame – Mohammad Azaz Jun 06 '17 at 16:40
  • I think you are messing up with the program it is working fine to me. Go to this link and get your whole code and try. https://gist.github.com/Blasanka/f6b9a5c2eb43d9a19dfa4f0e75f757ad – Blasanka Jun 06 '17 at 16:48
0

I solved It Please A look : Parent Class:

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class ParentClass {

    private JFrame frame;
    private int Count;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ParentClass window = new ParentClass();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public ParentClass() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JButton btnClicked = new JButton("Clicked");
        btnClicked.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                Count++;
                System.out.println("Count = "+Count);
                if(Count==1)
                {
                    ChildClass obj=new ChildClass();
                    obj.ChildClassVisibility();
                }
            }
        });
        btnClicked.setBounds(150, 99, 89, 23);
        frame.getContentPane().add(btnClicked);
    }
}

ChildClass:

import java.awt.EventQueue;

import javax.swing.JFrame;

public class ChildClass {

    private JFrame frame;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ChildClass window = new ChildClass();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public ChildClass() {
        initialize();
    }

    public void ChildClassVisibility()
    {
        frame.setVisible(true);
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}
  • `if(Count==1)` this is not a good way, because your program will be slow when your program getting larger. Multiple `JFrame`s appear because your are calling `setVisible(true)` two times. One in the main method and another one when button press. – Blasanka Jun 06 '17 at 17:16
  • and one more question how to make a getter and setter methods for JFrame object – Mohammad Azaz Jun 06 '17 at 17:17
  • because i want to use that frame visiblity into another frame – Mohammad Azaz Jun 06 '17 at 17:18
  • If you want disappear main frame after click the button add `frame.dispose()` in `parentClass` – Blasanka Jun 06 '17 at 17:25