frame are exiting at the same time
i need some quick help here please..
i have a class names PopUpFrame class
Code :
public class PopUpFrame extends JFrame {
private JPanel popUpPanel;
private JLabel errorMessage;
private Image icon;
private ImageIcon background;
private JButton okButton;
private String errorNotificationFrameTitle;
private String errorNotificationLabel;
private int popUpWindowWidth;
private int buttonXPosition;
public PopUpFrame() {
icon = new ImageIcon("c:/pics/mercuryLogo.jpg").getImage().getScaledInstance(300, 300, 0);
background = new ImageIcon("c:/pics/mainbck2.jpg");
okButton = new JButton("Ok");
errorMessage = new JLabel();
errorNotificationFrameTitle = "";
errorNotificationLabel = "";
popUpWindowWidth = 300;
buttonXPosition = 110;
}
public void setComponents(Container cont) {
popUpPanel = new JPanel() {
@Override
public void paintComponent(Graphics g) {
// Scale image to size of component
Dimension d = getSize();
g.drawImage(background.getImage(), 0, 0, d.width, d.height, null);
super.paintComponent(g);
}
};
popUpPanel.setOpaque(false);
// set this panel's layout to null for absolute positioning of
// components
popUpPanel.setLayout(null);
// set the properties and actions of this button
Action closeAction = new AbstractAction() {
public void actionPerformed(ActionEvent ae) {
int x = JOptionPane.CLOSED_OPTION;
if (x == 1) {
System.exit(0);
}
}
};
okButton.addActionListener(closeAction);
okButton.setBounds(buttonXPosition, 35, 75, 20);
okButton.setFont(new Font("Monospaced", Font.BOLD, 11));
okButton.setBackground(Color.LIGHT_GRAY);
// set the properties of this label
errorMessage.setText(errorNotificationLabel);
errorMessage.setBounds(20, 5, 500, 30);
errorMessage.setFont(new Font("SansSerif", Font.BOLD, 11));
popUpPanel.add(errorMessage);
popUpPanel.add(okButton);
// add the panel to the container
cont.add(popUpPanel);
}
/**
* Sets the error title for this pop-up frame.
*
* @param frameTitle title for the pop-up frame
*/
public void setErrorNotificationFrameTitle(String frameTitle) {
errorNotificationFrameTitle = frameTitle;
}
/**
* Sets the error label message for this pop-up frame.
*
* If the message is too long, extend the width of the pop-up frame, and
* make the button in the center by calculating its x coordinate according
* to the width of the frame.
*
* @param labelMessage error message for this pop-up frame
*/
public void setErrorNotificationLabel(String labelMessage) {
if (labelMessage.length() > 50) {
popUpWindowWidth = popUpWindowWidth + 300;
buttonXPosition = buttonXPosition + 150;
}
else if (labelMessage.length() > 40) {
popUpWindowWidth = popUpWindowWidth + 250;
buttonXPosition = buttonXPosition + 120;
}
else if (labelMessage.length() > 30) {
popUpWindowWidth = (popUpWindowWidth * 2) - 200;
buttonXPosition = (buttonXPosition * 2) - 60;
}
errorNotificationLabel = labelMessage;
}
public void createPopUpFrame() {
this.setDefaultCloseOperation(PopUpFrame.EXIT_ON_CLOSE);
this.setComponents(this.getContentPane());
this.setTitle(errorNotificationFrameTitle);
this.setIconImage(icon);
this.setSize(popUpWindowWidth, 100);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setVisible(true);
}
}
and i have a simple main class that extends the JFrame class
Code :
public class NewMain {
public static void main(String[] args) {
JFrame f = new JFrame();
PopUpFrame p = new PopUpFrame();
f.setSize(400, 500);
f.setVisible(true);
f.setLocationRelativeTo(null);
p.createPopUpFrame();
}
}
my problem here is.. when i run the main class, the popUpFrame object will pop up..
but the problem is it DOESNT ACT like THE JOptionPane...
when i press the exit button.. all the frames are exiting.... how can i resolve this one...
i need this customize frame for my program...
Re: frame are exiting at the same time
I believe calling System.exit terminates everything.
Re: frame are exiting at the same time
tnx dude but i got the error... just a little stupidity i didnt notice this part
Code :
this.setDefaultCloseOperation(PopUpFrame.EXIT_ON_CLOSE);
damn.... im getting frustrated .. because i need to finish this ASAP.. thats why im not able to notice some of
my codings... darn... i dont even know how to use debugger until now..... :confused::P
Re: frame are exiting at the same time
now i want to ask how to.. make this.. please help...
i want to make an action for this button... that if i press ok it will just act like a JOption button
that will JUST EXIT the DIALOG not the whole program...
this is only the comman i know how to terminate
but if i use this.. all the windows that are running will terminate..
Code :
Action closeAction = new AbstractAction() {
public void actionPerformed(ActionEvent ae) {
// an action that will only command this button to exit ONLY itself not the whole windows that are running
}
};
Re: frame are exiting at the same time
Where 'frame' is a reference to your JFrame. You can also set the default close operation to DISPOSE_ON_CLOSE, or if you wish to re-use the window just set its visibility to false (setVisible(false)).
Re: frame are exiting at the same time
\m/:x:">damn! i love this forum!! THANKS SIR COPEG!!!! hahahaha
Re: frame are exiting at the same time
last problem.. i tried to deal with this one.. but i cant figure out the flow ..
the PopUp Class
Code :
public class PopUpFrame {
private JFrame f;
private JPanel popUpPanel;
private JLabel errorMessage;
private Image icon;
private ImageIcon background;
private JButton okButton;
private String errorNotificationFrameTitle;
private String errorNotificationLabel;
private int popUpWindowWidth;
private int buttonXPosition;
public PopUpFrame() {
f = new JFrame();
icon = new ImageIcon("c:/pics/mercuryLogo.jpg").getImage().getScaledInstance(300, 300, 0);
background = new ImageIcon("c:/pics/mainbck2.jpg");
okButton = new JButton("Ok");
errorMessage = new JLabel();
errorNotificationFrameTitle = "";
errorNotificationLabel = "";
popUpWindowWidth = 300;
buttonXPosition = 110;
}
public void setComponents(Container cont) {
popUpPanel = new JPanel() {
@Override
public void paintComponent(Graphics g) {
// Scale image to size of component
Dimension d = getSize();
g.drawImage(background.getImage(), 0, 0, d.width, d.height, null);
super.paintComponent(g);
}
};
popUpPanel.setOpaque(false);
// set this panel's layout to null for absolute positioning of
// components
popUpPanel.setLayout(null);
// set the properties and actions of this button
Action closeAction = new AbstractAction() {
public void actionPerformed(ActionEvent ae) {
f.dispose();
}
};
okButton.addActionListener(closeAction);
okButton.setBounds(buttonXPosition, 35, 75, 20);
okButton.setFont(new Font("Monospaced", Font.BOLD, 11));
okButton.setBackground(Color.LIGHT_GRAY);
okButton.setMnemonic(KeyEvent.VK_ENTER); // UNSUPPORTED Hot Key
// set the properties of this label
errorMessage.setText(errorNotificationLabel);
errorMessage.setBounds(20, 5, 500, 30);
errorMessage.setFont(new Font("SansSerif", Font.BOLD, 11));
popUpPanel.add(errorMessage);
popUpPanel.add(okButton);
// add the panel to the container
cont.add(popUpPanel);
}
/**
* Sets the error title for this pop-up frame.
*
* @param frameTitle title for the pop-up frame
*/
public void setErrorNotificationFrameTitle(String frameTitle) {
errorNotificationFrameTitle = frameTitle;
}
/**
* Sets the error label message for this pop-up frame.
*
* If the message is too long, extend the width of the pop-up frame, and
* make the button in the center by calculating its x coordinate according
* to the width of the frame.
*
* @param labelMessage error message for this pop-up frame
*/
public void setErrorNotificationLabel(String labelMessage) {
if (labelMessage.length() > 50) {
popUpWindowWidth = popUpWindowWidth + 300;
buttonXPosition = buttonXPosition + 150;
}
else if (labelMessage.length() > 40) {
popUpWindowWidth = popUpWindowWidth + 250;
buttonXPosition = buttonXPosition + 120;
}
else if (labelMessage.length() > 30) {
popUpWindowWidth = (popUpWindowWidth * 2) - 200;
buttonXPosition = (buttonXPosition * 2) - 60;
}
errorNotificationLabel = labelMessage;
}
public void createPopUpFrame() {
setComponents(f.getContentPane());
f.setTitle(errorNotificationFrameTitle);
f.setIconImage(icon);
f.setSize(popUpWindowWidth, 100);
f.setFocusableWindowState(false);
f.setLocationRelativeTo(null);
f.setResizable(false);
f.setVisible(true);
}
public static void popTheMessage() {
SwingUtilities.invokeLater(new Runnable() {
PopUpFrame pU = new PopUpFrame();
public void run() {
pU.createPopUpFrame();
}
});
}
}
main
Code :
public class NewMain {
public static void main(String[] args) {
PopUpFrame pU = new PopUpFrame();
pU.setErrorNotificationFrameTitle("Error Title");
pU.setErrorNotificationLabel("Label");
PopUpFrame.popTheMessage();
}
}
i want this class to be as flexible as possible.. but i cant figure out why is that i cant generate the title and the label?
i tried to put a string label and a string title in the PopUp class.. and it runs perfectly.. but when i use the methods .setErrorNotificationFrameTitle(String frameTitle); and setErrorNotificationLabel(String labelMessage); it doesnt generate the desired output... :confused:
Re: frame are exiting at the same time
A quick look-over and it looks like you're just setting a string inside the class, not the actual title of the frame. Use setTitle() to make sure the title gets set.
Code :
frame.setTitle("My Frame");
Re: frame are exiting at the same time
im still getting the same error...