[quote=rcipriani]Para abrir as janelas (JInternalFrame) no JDesktopPane, eu utilizo da seguinte forma:
Tenho uma função que fica na TelaPrincipal.java que adiciona as telas no Desktop:
//ADICIONA TELAS (JInternalFrame) AO JDesktopPane
// usado assim: this.addTelaDesk(new TelaFuncionarios(null));
public void addTelaDesk(JInternalFrame tela){
for (JInternalFrame jInternalFrame : this.getTelaDesktop().getAllFrames()) {
// se uma janela semelhante estiver aberta
if(jInternalFrame.getClass().toString().equalsIgnoreCase(tela.getClass().toString())){
jInternalFrame.moveToFront(); // traz janela para frente para facilitar a escolha
Object[] opções = {"Utilizar Aberta", "Abrir Nova"};
int qst = JOptionPane.showOptionDialog(null, "Abrir uma nova janela ou utilizar a que ja esta aberta?",
"Janela duplicada",
JOptionPane.DEFAULT_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
opções,
opções[0]
);
// se utilizar a aberta retorna e não abre outra,
// caso contrário sai do for e abre outra igual
if(qst == 0){
return;
}else if(qst == 1){
break;
}
}
}
this.telaDesktop.add(tela); //adiciona na tela
tela.setVisible(true); // seta visivel
this.cascade(); //coloca em cascata para deixar "pratico"
}
A função para colocar em cascata:
private void cascade() {
JDesktopPane desk = this.getTelaDesktop(); // JDesktopPane
Rectangle dBounds = desk.getBounds(); // Bordas do JDesktopPane
int separation = 25; // distancia entre as janelas
// Pega todos os frames e organiza, o ultimo fica mais em baido e mais pra cima
int i = desk.getAllFrames().length; // quantidade de frames
for (JInternalFrame f : desk.getAllFrames()) {
f.setLocation(i*separation, i*separation);
i--; //mutiplicador
}
}
Exemplos de como chamar depois:
this.addTelaDesk(new TelaFuncionarios(null));
String matricula = JOptionPane.showInputDialog("Digite a matrícula do funcionário:");
this.addTelaDesk(new TelaFuncionarios(matricula));
Pra mim ta funcionando bem como quero, mas qualquer sugestão é bem vinda!
Abraços
[/quote]
Excelente exemplo amigo, corrigi alguns problemas relacionado a ordem de exibição das janelas, segue o código melhorado…
No método addTelaDesk…
public void addTelaDesk(JInternalFrame tela) {
for (JInternalFrame antiga : getDesktop().getAllFrames()) {
if (antiga.getClass().toString()
.equalsIgnoreCase(tela.getClass().toString())) {
antiga.moveToFront();
// fix caso usuário escolha a janela que já está aberta,
// ou feche a dialog o focus ficará na janela duplicada que está
// em focus atrás da dialog
try {
antiga.setSelected(true);
} catch (PropertyVetoException e) {
e.printStackTrace();
}
Object[] opcoes = { "Utilizar aberta", "Abrir nova" };
int qst = JOptionPane
.showOptionDialog(
janela,
"Abrir uma nova janela ou utilizar a que já está aberta?",
"Ops! Janela duplicada",
JOptionPane.DEFAULT_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
opcoes,
opcoes[0]);
if (qst <= 0) {
//fix caso o usuário feche o dialog sem escolher uma opção
return;
} else if (qst == 1) {
break;
}
}
}
getDesktop().add(tela);
tela.setVisible(true);
this.cascade();
}
No método cascade…
private void cascade() {
int separation = 25;
int i = getDesktop().getAllFrames().length;
for (JInternalFrame f : getDesktop().getAllFrames()) {
f.setLocation(i*separation, i*separation);
i--;
if (i == 0) {
//dá o focus sempre na última janela que ficará em cascata
try {
f.setSelected(true);
} catch (PropertyVetoException e) {
e.printStackTrace();
}
}
}
}