Autor Tema: Ayuda con JDialog  (Leído 2170 veces)

0 Usuarios y 1 Visitante están viendo este tema.

Desconectado moyo18

  • Trade Count: (0)
  • The Communiter-
  • *
  • Thank You
  • -Given: 6
  • -Receive: 41
  • Mensajes: 1719
Ayuda con JDialog
« : mayo 18, 2010, 12:44:44 pm »
hey pues tengo un problemin aki no se como resolverlo tengo un Form de mi textpad y tengo un menu dond al darle clikc a dond dice replace tiene q mostrarme un dialog q seria un form para reemplazar palabras.

el codigo q tengo hasta el momento es el siguiente.

main form supuestamente esto tiene q llamar al dialog para q aparesca y no lo hace

Código: [Seleccionar]

replaceItem = new JMenuItem();
searchMenu.add(getReplaceItem());
replaceItem.setText("Replace");
replaceItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {

replaceText();



}
 
});

//replace method
public void replaceText(){
replaceDialog myReplace = new replaceDialog(this, "Replace", editor, rootPaneCheckingEnabled);
editor.setText(myReplace.replace());
}


ahora en el dialog q es otra clase tengo esto

Código: [Seleccionar]
package src;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//import textPadForm;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComponent;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.UIManager;



/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class replaceDialog extends javax.swing.JDialog {

/**
*
*/
private static final long serialVersionUID = 1L;
private JLabel findLabel;
private JLabel replaceLabel;
private JButton closeButton;
private JButton replaceButton;
private JTextField replaceText;
private JTextField findText;




private String textToReplace1 = "";

public replaceDialog(JFrame parent, String title, JTextArea textToReplace, boolean modal) {
super(parent, title, modal);
initGUI();

textToReplace1 = textToReplace.getText();


}

private void initGUI() {
try {
GroupLayout thisLayout = new GroupLayout((JComponent)getContentPane());
getContentPane().setLayout(thisLayout);
{
findLabel = new JLabel();
findLabel.setText("Find");
}
{
replaceLabel = new JLabel();
replaceLabel.setText("Replace With");
}
{
findText = new JTextField();
}
{
replaceText = new JTextField();
}
{
replaceButton = new JButton();
replaceButton.setText("Replace All");
replaceButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
replace();
}
});
}
{
closeButton = new JButton();
closeButton.setText("Close");
}
thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
.addContainerGap()
.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
    .addComponent(getFindText(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
    .addComponent(findLabel, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
    .addComponent(replaceButton, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
    .addComponent(getReplaceText(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
    .addComponent(replaceLabel, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
    .addComponent(closeButton, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE))
.addContainerGap(85, 85));
thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
.addContainerGap()
.addGroup(thisLayout.createParallelGroup()
    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
        .addComponent(findLabel, GroupLayout.PREFERRED_SIZE, 44, GroupLayout.PREFERRED_SIZE)
        .addGap(50))
    .addComponent(replaceLabel, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 94, GroupLayout.PREFERRED_SIZE))
.addGroup(thisLayout.createParallelGroup()
    .addComponent(getFindText(), GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 168, GroupLayout.PREFERRED_SIZE)
    .addComponent(getReplaceText(), GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 168, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(thisLayout.createParallelGroup()
    .addGroup(thisLayout.createSequentialGroup()
        .addComponent(replaceButton, GroupLayout.PREFERRED_SIZE, 89, GroupLayout.PREFERRED_SIZE)
        .addGap(0, 0, Short.MAX_VALUE))
    .addComponent(closeButton, GroupLayout.Alignment.LEADING, 0, 89, Short.MAX_VALUE))
.addContainerGap());
pack();
} catch (Exception e) {
e.printStackTrace();
}
}

public JTextField getFindText() {
return findText;
}

public JTextField getReplaceText() {
return replaceText;
}

   
public String replace(){
    //////////////////////////////////////////////////////////////////

String newText = "";
String textArea = textToReplace1;
String find = findText.getText();
String replace = replaceText.getText();
if (find != ""){
if (replace != ""){
int location = textArea.indexOf(find);
if(location != -1){
newText = textArea.replace(find, replace);

}else{
UIManager.put("OptionPane.okButtonText", "Ok");
JOptionPane.showMessageDialog(this, find +" was not found!!!",
"Not Found", JOptionPane.WARNING_MESSAGE);
}
}
else{
UIManager.put("OptionPane.okButtonText", "Ok");
JOptionPane.showMessageDialog(this, "You did not enter a word to replace",
"Error", JOptionPane.WARNING_MESSAGE);
}
}else{
UIManager.put("OptionPane.okButtonText", "Ok");
JOptionPane.showMessageDialog(this, "You did not enter a word to find",
"Error", JOptionPane.WARNING_MESSAGE);
}
//////////////////////////////////////////////////////////////////////////////////////

return newText;

    }

}


la onda q no se como aparesca el dialog en el otro form, alguna idea ?

rey

  • Visitante
  • Trade Count: (0)
Re:Ayuda con JDialog
« Respuesta #1 : julio 16, 2010, 04:05:54 am »
Veo que la discusión es de mayo pero no importa.
la solución es simple, no te aparece el JDialog porque no la es cambiado la propiedad Visible que por defecto es false, para
cambiar esa propiedad usas el método setVisible que recibe como parametro un boolean

public void replaceText(){
      replaceDialog myReplace = new replaceDialog(this, "Replace", editor, rootPaneCheckingEnabled);
      myReplace.setVisible(true);
      editor.setText(myReplace.replace());
   }