Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Creating window within a window

Greeting Experts,

I have created a Jframe window with 3 buttons in it.Now when i click on one of the buttons, it will prompt me another window with 2 other buttons.

I have created a class called AllClient. and this is how i have initialise and declared this:

public class AllClient extends JPanel
implements ActionListener{

//all the codes
}

Do i have to create another class to show another Jframe window which will pop out once i click the buttons from the Allclient JPanel?

IF thats true, however , i am unable to create another class and implement the fucntion..

What do i do and how do i create another window?

these are my codes:

import java.io.*;
import java.net.*;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.ImageIcon;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
public class AllClient extends JPanel
implements ActionListener{
private static final int NUM = 21;
protected JButton b1, b2, b3;
protected JButton b4, b5;
public AllClient() {
// ImageIcon leftButtonIcon = createImageIcon("images/right.gif");
// ImageIcon middleButtonIcon = createImageIcon("images/middle.gif");
// ImageIcon rightButtonIcon = createImageIcon("images/left.gif");

b1 = new JButton("Viewer");
b1.setVerticalTextPosition(AbstractButton.CENTER);

b2 = new JButton("Alert Log Viewer");
b2.setVerticalTextPosition(AbstractButton.BOTTOM);

b3 = new JButton("Exit");
b3.setVerticalTextPosition(AbstractButton.BOTTOM);



//Listen for actions on buttons 1 ,2 and 3.

b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);



b1.setToolTipText("Click here to View the Viewer");
b2.setToolTipText("Click here to View the Alert Log.");
b3.setToolTipText("Click here to Exit");

//Add Components to this container, using the default FlowLayout.
add(b1);
add(b2);
add(b3);

}

public Viewer() {



b4 = new JButton("OK");
b4.setVerticalTextPosition(AbstractButton.CENTER);

b5 = new JButton("Exit");
b5.setVerticalTextPosition(AbstractButton.BOTTOM);

b4.addActionListener(this);
b5.addActionListener(this);

b4.setToolTipText("Click here to proceed");
b5.setToolTipText("Click here to Exit");

add(b4);
add(b5);

}

public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("Viewer")){
creatViewerGUI();
}else if (e.getActionCommand().equals("Alert Log Viewer")){

}else if (e.getActionCommand().equals("Exit")){
System.exit(0);
}

}



//Add Actions to the Buttons to perform the tasks

private static void createAndShowGUI() {

//Create and set up the window.
JFrame frame = new JFrame("Main Menu");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Create and set up the content pane.
AllClient newContentPane = new AllClient();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);

//Display the window.
frame.pack();
frame.setVisible(true);
}

private static void creatViewerGUI(){
//Create and set up the window.
JFrame frame2 = new JFrame("Viewer");
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Create and set up the content pane.
Viewer newContentPane = new Viewer();
newContentPane.setOpaque(true); //content panes must be opaque
frame2.setContentPane(newContentPane);

//Display the window.
frame2.pack();
frame2.setVisible(true);
}
public static void main(String[] args) {
String str ="";
String str2 = "";
String str3="";
String str4="";

//DataOutputStream out1 = out2;
DataInputStream in[] = new DataInputStream[NUM];
BufferedReader br[] = new BufferedReader[NUM];
Socket t[] = new Socket[NUM];
try {

for(int i=0;i<NUM;i++)
{
t[i] = new Socket("127.0.0.1", 2000 + i);
in[i] = new DataInputStream(t[i].getInputStream());
//out1 = new DataOutputStream(t1.getOutputStream());
br[i] = new BufferedReader(new InputStreamReader(System.in));
//boolean more1 = true;
//System.out.println(in[i].readUTF());

System.out.println("Connection "+i);
}

while (true) {
for(int i=0;i<NUM;i++)
{
str = in[i].readUTF();
System.out.print(str);
}
System.out.print("\n");
// str2 = br1.readLine();
//out1.writeUTF(str2);
// out1.flush();
//str1 = in1.readUTF();
//if (str1 == null)
//more1 = false;
//else
// System.out.println(str1);
}

} catch(Exception e){
System.out.println("Error: " + e);

}


//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});


}
/** Returns an ImageIcon, or null if the path was invalid. */
protected static ImageIcon createImageIcon(String path) {
java.net.URL imgURL = AllClient.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
}





Sorry for the trouble but i really need help on this>Thanks experts
[6956 byte] By [ashali19] at [2007-11-11 9:58:44]
# 1 Re: Creating window within a window
Sorry , Some of the codes which i have added for a port connection is redundant and ps ignore that..very sorry about that..
ashali19 at 2007-11-11 22:32:13 >