Opening New Window in Jframe

user6680

When you click the button "Search" it should open up the class MISControlPanel.java, but instead it opens up a window like you see in the picture. It shouldn't be blank like that. Any ideas why the class is coming up blank? I think it has something to do with initializing "newWindow" to null(otherwise it threw this error: "The local variable newWindow may not have been initialized") so I'm not sure. enter image description here

relevant snippet from code below

 MISControlPanel newWindow = null;
                    try {
                        newWindow = new MISControlPanel();
                    } catch (IOException e3) {
                        // TODO Auto-generated catch block
                        e3.printStackTrace();
                    }
                        newWindow.setVisible(true);

Full class code


      import java.awt.EventQueue;

import javax.naming.InvalidNameException;
import javax.naming.ldap.LdapName;
import javax.swing.JFrame;
import javax.swing.JButton;

import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.GridLayout;

import javax.naming.ldap.LdapName;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import java.awt.Color;
import java.awt.Font;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Inet4Address;
import java.net.UnknownHostException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.JTextArea;
import javax.swing.JTable;

public class MISSearch {

    JFrame frame;
    static JTextField textField;
    private JTextField textField_1;
    JLabel selectedComputerFromAD = new JLabel("testing");
    String sCurrentLine = null;
    String CN = null;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MISSearch window = new MISSearch();
                window.frame.setVisible(true);


                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     * 
     * @throws IOException
     * 
     * @wbp.parser.entryPoint
     */
    public MISSearch() throws IOException {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     * 
     * @throws IOException
     */

    private void initialize() throws IOException {
        frame = new JFrame();
        frame.getContentPane().setBackground(Color.LIGHT_GRAY);
        frame.getContentPane().setForeground(Color.RED);
        frame.setBounds(100, 100, 394, 111);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
        frame.setTitle("MIS Control Panel");
        frame.setResizable(false);
        FileWriter fw = new FileWriter("C:\\Users\\anoc5f\\Desktop\\Output.txt");
        File tempFile = new File("myTempFile.txt");
        JButton searchComputerButton = new JButton("Search");
        searchComputerButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                 MISControlPanel newWindow = null;
                try {
                    newWindow = new MISControlPanel();
                } catch (IOException e3) {
                    // TODO Auto-generated catch block
                    e3.printStackTrace();
                }
                    newWindow.setVisible(true);

                    try {
                        new test();
                    } catch (IOException e2) {
                        // TODO Auto-generated catch block
                        e2.printStackTrace();
                    }

                String line;
                BufferedWriter bw = null;
                BufferedWriter writer = null;
                try {
                    writer = new BufferedWriter(new FileWriter(tempFile));
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }


                String s = null;

                Process p = null;
                /*
                 * try { // p = Runtime.getRuntime().exec(
                 * "cmd /c start c:\\computerQuery.bat computerName"); } catch
                 * (IOException e1) { // TODO Auto-generated catch block
                 * e1.printStackTrace(); }
                 */
                try {

                    p = Runtime.getRuntime().exec("c:\\computerQuery.bat");

                } catch (IOException e1) {

                    // TODO Auto-generated catch block

                    e1.printStackTrace();

                }
                StringBuffer sbuffer = new StringBuffer();
                BufferedReader in = new BufferedReader(new InputStreamReader(p
                        .getInputStream()));

                try {

                    while ((line = in.readLine()) != null) {

                        System.out.println(line);

                        // textArea.append(line);

                        String dn = "CN=FDCD111304,OU=Workstations,OU=SIM,OU=Accounts,DC=FL,DC=NET";
                        LdapName ldapName = new LdapName(dn);
                        String commonName = (String) ldapName.getRdn(
                                ldapName.size() - 1).getValue();

                    }
                    ComputerQuery.sendParam();

                } catch (IOException e1) {

                    // TODO Auto-generated catch block

                    e1.printStackTrace();

                } catch (InvalidNameException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } finally

                {
                    try {
                        fw.close();

                    }

                    catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                }

                try {

                    in.close();

                } catch (IOException e1) {

                    // TODO Auto-generated catch block

                    e1.printStackTrace();

                }

                ComputerQuery.sendParam();
        /*
                   SwingUtilities.invokeLater(new Runnable() {
                        public void run() {
                          testLabel.setText(CN);
                        }
                      });
            */
            }
        });


        try (BufferedReader br = new BufferedReader(new FileReader(
                "resultofbatch.txt"))) {

            final Pattern PATTERN = Pattern.compile("CN=([^,]+).*");
            try {
                while ((sCurrentLine = br.readLine()) != null) {

                    String[] tokens = PATTERN.split(","); // This will return
                                                            // you a array,
                                                            // containing the
                                                            // string array
                                                            // splitted by what
                                                            // you write inside
                                                            // it.
                    // should be in your case the split, since they are
                    // seperated by ","
                    // System.out.println(sCurrentLine);
                    CN = sCurrentLine.split("CN=", -1)[1].split(",", -1)[0];

                    System.out.println(CN);

                }

            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        } catch (IOException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }
        // SwingUtilities.invokeLater(updateCN());
        searchComputerButton.setBounds(269, 17, 89, 20);
        frame.getContentPane().add(searchComputerButton);

        textField = new JTextField();
        textField.setBounds(164, 17, 95, 20);
        frame.getContentPane().add(textField);
        textField.setColumns(10);

        JLabel lblComputerName = new JLabel("Computer Name:");
        lblComputerName.setForeground(Color.BLACK);
        lblComputerName.setFont(new Font("Tahoma", Font.BOLD, 14));
        lblComputerName.setBounds(41, 11, 124, 29);
        frame.getContentPane().add(lblComputerName);
        java.net.InetAddress localMachine = null;
        try {
            localMachine = java.net.InetAddress.getLocalHost();
        } catch (UnknownHostException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }

        JLabel label = null;
        JLabel lblOpid = new JLabel("Computer by OPID:");
        lblOpid.setFont(new Font("Tahoma", Font.BOLD, 14));
        lblOpid.setBounds(23, 46, 137, 20);
        frame.getContentPane().add(lblOpid);

        textField_1 = new JTextField();
        textField_1.setBounds(164, 48, 95, 20);
        frame.getContentPane().add(textField_1);
        textField_1.setColumns(10);

        JButton btnNewButton_2 = new JButton("Search");
        btnNewButton_2.setBounds(269, 47, 89, 23);
        frame.getContentPane().add(btnNewButton_2);

    }
    public void setVisible(boolean visible){
        frame.setVisible(true);
    }
    private void threadStart() {
          SwingUtilities.invokeLater(new Runnable() {
            public void run() {

                        }
          });
        }

}
m.cekiera

I think it is because you use newWindow.setVisible(true); but your MISControlPanel don't extend Window class to inherit setVisible() method, but has field JFrame, and you need to set it visible separatly. Try to add this method to MISControlPanel:

public void setVisible(boolean visible){
    frame.setVisible(visible);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

opening a prism module on new window on a registered region

From Dev

Opening a new terminal window in C

From Dev

Opening a new window with reduced size in grails

From Dev

Window not opening in a new tab

From Dev

Opening a new window by clicking <p:commandButton> in JSF

From Dev

JButton opening new JFrame?

From Dev

Opening JFrame from another package in the main window

From Dev

Opening new window from ViewModel

From Dev

link opening new window and refreshing current

From Dev

Opening a new powershell window with a CMD command

From Dev

Opening a new JFrame using a JButton

From Dev

HTML "mailto:" not opening in new window

From Dev

opening a link in a new window

From Dev

Java File Copy and Opening New jFrame

From Dev

opening a new window based on the current url of the page

From Dev

Opening a New Window when Chrome Extension is Uninstalled

From Dev

Tweet button opening new window and new tab

From Dev

Opening a JFrame within a JFrame

From Dev

Opening new window with button in OpenERP

From Dev

Click on link not opening in new window

From Dev

opening new window in PHP not working

From Dev

Opening new window in wicket

From Dev

Opening JFrame from another package in the main window

From Dev

Opening a new JFrame using a JButton

From Dev

Opening a new JFrame by clicking a button

From Dev

GUI, JComboBox and opening a new window

From Dev

Opening a new JFrame when JButton is clicked

From Dev

Completely loading JFrame before opening a new

From Dev

Opening an SWF file in a new window