0

I set up a JFrame with a JDialog which opens when I click a button. Now when i minimize my Frame with the Dialog opened and maximize it again, only the JDialog is shown up. Also setModal to true doesnt bring a solution with it. How can I avoid this? Thanks

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JDialog;

public class Dialog extends JDialog implements KeyListener{

private static final long serialVersionUID = -4420611194056842395L;
private JButton[] buttons = new JButton[9];
private byte val = -1;

/**
 * Builds the JDialog relative to a component 
 * @param c Mother-component
 * @param size Size of the mother-component to scale
 */
public Dialog(Component c, int size){
    this.setSize(size/2, size/2);
    this.setModal(true);
    this.setLocationRelativeTo(c);
    this.setLayout(new GridLayout(3,3));
    this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
    init();     
    this.addKeyListener(this);
    this.setUndecorated(true);  // hide and show problem
}

/**
 * Initializes the buttons for the menu.
 * Adds ActionListener to each button and adds them to the dialog
 */
private void init(){
    for(int i = 0; i<buttons.length;i++){
        buttons[i] = new JButton(Integer.toString(i+1));
        buttons[i].setBackground(Color.gray);
        buttons[i].setFont(new Font("Arial", Font.BOLD, 20));
        buttons[i].setFocusable(false);
        buttons[i].addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                val = (Byte.parseByte(((AbstractButton) e.getSource()).getText()));
                setVisible(false);  //Closes the dialog after a click
            }

        });
        this.add(buttons[i]);

    }
}


public void resetButtons(){
    for(int i = 0;i<buttons.length;i++){
        buttons[i].setEnabled(true);
    }

}

/**
 * 
 * @return Returns the value of the clicked button
 */
public byte returnValue(){
    return val;
}

@Override
public void keyPressed(KeyEvent e) {
    if(e.getKeyCode()==KeyEvent.VK_ESCAPE) this.dispose();
}

@Override
public void keyReleased(KeyEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void keyTyped(KeyEvent e) {
    // TODO Auto-generated method stub

}
}

import javax.swing.JFrame;

public class Frames extends JFrame{

private Dialog d = new Dialog(this, 200);

public Frames() {
    // TODO Auto-generated constructor stub
    super("test");
    this.setSize(800, 500);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setLocationRelativeTo(null);
    this.setLayout(null);
    this.setResizable(false);
    this.setVisible(true);
    d.setVisible(true);
}
public static void main(String[] args) {
    Frames f = new Frames();
}
}
Frakcool
  • 10,915
  • 9
  • 50
  • 89
Tron
  • 1
  • 3
  • 1
    can you put your code please? – Youcef LAIDANI Jan 25 '17 at 19:26
  • 2
    For better help sooner please provide a valid [mcve] or [Short, Self Contained, Correct Example](http://sscce.org/) that demonstrates your issue – Frakcool Jan 25 '17 at 19:34
  • 1
    Tip, you can add @YCF_L (the `@` is important) to notify someone you've replied to a comment – Frakcool Jan 25 '17 at 22:02
  • 1
    There are some problems I can see in your code right now, however I can't even open a `JDialog`. 1) You're using a `null` layout and I can't see anything but 3 dots in the GUI for each button. Please read [null layout is evil](http://www.leepoint.net/GUI/layouts/nulllayout.html) and [Why is it frowned upon to use a null layout in Swing?](http://stackoverflow.com/questions/6592468/why-is-it-frowned-upon-to-use-a-null-layout-in-swing) – Frakcool Jan 25 '17 at 22:13
  • 2) You're using `setSize()` method on your `JDialog`, please see [Should I avoid the use of setPreferred|Minimum|Maximum|Size in Java Swing?](http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi), the general consensus says **YES**. 3) You didn't posted a valid [mcve], you could instead make a `JFrame` with a single button that opens a `JDialog` which still can reproduce your problem. – Frakcool Jan 25 '17 at 22:17
  • From my comment, I see that you've set your Dialog to be undecorated, and it's the grid that appears, however I'm not able to reproduce your problem on Ubuntu 15.10, I minimized the `JDialog` and restore it and both the `JFrame` and the `JDialog` came back – Frakcool Jan 25 '17 at 22:23
  • I think this is a OS issue, which is only allowing the dialog to be managed as it's modal – MadProgrammer Jan 25 '17 at 22:43

0 Answers0