hi i have this problem when other user sent message over it pop up a JOptionPane
and i can’t see the content why this happens?
it happens when i paint more then 1
g.drawImage
hi i have this problem when other user sent message over it pop up a JOptionPane
and i can’t see the content why this happens?
it happens when i paint more then 1
g.drawImage
If you’re trying to draw an image on top of the JOptionPane, it won’t work because dialogs are in a layer above the rest of Swing. This means that the dialogs get drawn on top of everything else.
You need to write a class that extends JOptionPane (or perhaps JDialog) and modify the paintComponent method. Alternatively, you could make a JDialog and just add Swing components to it to make it look like a JOptionPane with images. You can display an image as the icon for a JLabel (using the ImageIcon class to create the Icon).
ok thanks alot i will try it out =) 
i have a
public class ValidatorDialog extends JDialog
it pops up only when i need to log in but it’s also having the problem
if you need any codes? i can paste it here not sure which codes do you all need to see
thanks for the expert advice
in the below picture
it has 2 label, 2 text field, 1 button
for the button and textfield need to click on it for it to appear
or drag the box larger the contents will appear
It sounds like you’re having a problem with the layout now. Try calling ValidatorDialog.pack after adding all the components to the ValidatorDialog.
If you’re using a null layout, you have to set the position and the size of the components. It doesn’t sound like you’re using a null layout because resizing the dialog causes things to appear.
Have you tried just making it start out bigger?
By the way, you can use ValidatorDialog.setLocation to make the ValidatorDialog appear in the middle of the frame.
If none of this helps, post the code where you’re creating the ValidatorDialog, where you’re creating components and adding them to the ValidatorDialog, and where you’re displaying the ValidatorDialog.
ok i will try it out thanks
this is where i create my VD
public ValidatorDialog(Frame parent, Callback[] cbs) {
super(parent, "Validation Information Required", true);
callbacks = cbs;
Container c = getContentPane();
JPanel validationPanel = new JPanel();
validationPanel.setLocation(200,200);
validationPanel.setLayout(new GridLayout(2, 0));
c.add(validationPanel, BorderLayout.NORTH);
JButton validateButton = new JButton("CONTINUE");
c.add(validateButton, BorderLayout.SOUTH);
// when pressed, set the data from the UI components to the
// matching CallBacks.
validateButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
transcribeToCallbacks();
ValidatorDialog.this.setVisible(false);
ValidatorDialog.this.getParent().remove(ValidatorDialog.this);
}
});
// Iterate through the javax.security.auth.callback.CallBacks
// and render the appropriate UI accordingly.
// For each CallBack, the matching UI Component is stored in
// the dataFields array. The order is important, as they will
// be retrieved along side their matching CallBack.
for (Callback cb : cbs) {
if (cb instanceof ChoiceCallback) {
ChoiceCallback ccb = (ChoiceCallback) cb;
validationPanel.add(new JLabel(ccb.getPrompt()));
JComboBox combo = new JComboBox(ccb.getChoices());
combo.setSelectedItem(ccb.getDefaultChoice());
validationPanel.add(combo);
dataFields.add(combo);
} else if (cb instanceof ConfirmationCallback) {
ConfirmationCallback ccb = (ConfirmationCallback) cb;
validationPanel.add(new JLabel(ccb.getPrompt()));
JComboBox combo = new JComboBox(ccb.getOptions());
combo.setSelectedItem(ccb.getDefaultOption());
validationPanel.add(combo);
dataFields.add(combo);
} else if (cb instanceof NameCallback) {
NameCallback ncb = (NameCallback) cb;
validationPanel.add(new JLabel(ncb.getPrompt()));
JTextField nameField = new JTextField(ncb.getDefaultName());
validationPanel.add(nameField);
dataFields.add(nameField);
} else if (cb instanceof PasswordCallback) {
PasswordCallback ncb = (PasswordCallback) cb;
validationPanel.add(new JLabel(ncb.getPrompt()));
JPasswordField passwordField = new JPasswordField();
validationPanel.add(passwordField);
dataFields.add(passwordField);
} else if (cb instanceof TextInputCallback) {
TextInputCallback tcb = (TextInputCallback) cb;
validationPanel.add(new JLabel(tcb.getPrompt()));
JTextField textField = new JTextField(tcb.getDefaultText());
validationPanel.add(textField);
dataFields.add(textField);
} else if (cb instanceof TextOutputCallback) {
TextOutputCallback tcb = (TextOutputCallback) cb;
validationPanel.add(new JLabel(tcb.getMessage()));
}
}
pack();
setVisible(true);
}
and i call my VD in the class Login.java
public void validationRequest(Callback[] callbacks) {
// TODO Auto-generated method stub
status.setText("Status: Validating...");
ValidatorDialog v = new ValidatorDialog(this, callbacks);
v.setLocation(200, 200);
game.setMsg(v.getUserName());
//uname = v.getUserName();
mgr.sendValidationResponse(callbacks);
v.pack();
}
My advice to you is to avoid BorderLayout because it’s terrible.
Change the layout of the content pane to use a vertical BoxLayout. To add a label and a text field next to eachother, add them to a horizontal box, and add the horizontal Box to the content pane.
Finally, add the button to the content pane. If there are more than 1 button, add them all in a horizontal Box.
You might be able to still use the GridLayout for the labels and text fields even after converting the content pane to a vertical BoxLayout. I usually work with Boxes because they seem to work a little bit better.
Box contains some special methods (such as createGlue) that you may want to use as well, though I don’t think they’ll be necessary in this instance.
mmm still no difference
i have solve it , it’s having problems with repaint() ;
thanks for your help i have also convert to BoxLayout =) no harm trying but useful =) ;D
fletchergames, please read the first-post very carefully, he’s having a whole other problem than you seem to think.
The problem is that the contents of a Dialog isn’t shown, when it should. He’s not trying to paint images in his dialogs. LayoutManagers have even less to do with it.
I don’t really know the problem, nor it’s solution, but you can try this:
Ensure you do all your UI-code on the EventDispatchThread (EDT)
SwingUtilities.invokeLater(new Runnable(){
public void run()
{
Dialog d = new Dialog(SET_THE_PARENT___NOT_NULL, modal=true);
...
d.pack();
d.setVisible();
}
});
It should never have problems with repaint. don’t call repaint manually to ‘fix’ it, because there’s something wrong, that causes the repaint not to occur automaticly. Better solve that! (try what i said in my previous post)
well thanks for your guide.
as for repaint() in my case i called an extra repaint
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2 = (Graphics2D)g;
for(across = 0; across < width ; across++) {
for(vert = 0; vert < height ; vert++) {
if (A[vert][across] == 1)
{
g2.drawImage(gras,across*32,vert*32,this);
}
else if(A[vert][across]==2)
{
g2.drawImage(tree,across*32,vert*32,this);
}
else if(A[vert][across]==3)
{
g2.drawImage(mount,across*32,vert*32,this);
}
else if(A[vert][across]==4)
{
g2.drawImage(seamount,across*32,vert*32,this);
}
else if(A[vert][across]==5)
{
g2.drawImage(sea,across*32,vert*32,this);
}
else if(A[vert][across]==6)
{
g2.drawImage(sbr,across*32,vert*32,this);
}
else if(A[vert][across]==7)
{
g2.drawImage(sbl,across*32,vert*32,this);
}
else if(A[vert][across]==8)
{
g2.drawImage(str,across*32,vert*32,this);
}
else if(A[vert][across]==9)
{
g2.drawImage(stl,across*32,vert*32,this);
}
else if(A[vert][across]==10)
{
g2.drawImage(shoretop,across*32,vert*32,this);
}
else if(A[vert][across]==11)
{
g2.drawImage(shoreleft,across*32,vert*32,this);
}
else if(A[vert][across]==12)
{
g2.drawImage(shorebottom,across*32,vert*32,this);
}
else
{
g2.drawImage(shoreright,across*32,vert*32,this);
}
}
}
g2.setColor(Color.cyan);
g2.drawString(uname,x,y-10);
g2.drawImage(pship, x, y,this);
for(int i = 0;i< Login.q;i++)
{
//String nu = Login.nUser[i];
g2.drawString(Login.nUser[i],xX[i],yY[i]-10);
g2.drawImage(pship, xX[i], yY[i], this);
}
//as u can see, when i remove this repaint , i do not have that problem
not sure why but i seems that it’s repainting a blank stuff.
repaint();
}
You should never call repaint from within the paint or paintComponent methods because repaint just calls the paint method again.
Also, why don’t you just put all the images in an array (or ArrayList or enum or something) instead of having all those if statements? You’re just doing extra typing and making your code harder to modify.
oh ok thanks alot! i’ll try the array way =) thanks