-2

I'm new to Java GUI. I'm not good to thread programming. I just wondering if there is any easy way to create and display a JFrame from another JFrame method. I give you my code. The SettingsForm calls the Test which is a JFrame via the close() method. All imports have been included.

class SettingsForm extends JFrame{

    JButton submit,defaultOption;//koumpia kataxwrhshs-akurwshs
    JLabel host, port, dbName, user, pass;//koina JLabel.Etiketes host, port, dbName, user kai password
    JTextField hostT, portT, dbNameT, userT, passT;//koina JTextField.perioxh egrafhs twn host, port, dbName, user kai password
    ImageIcon img;

    public SettingsForm()//arxikh dhmiourgia tou frame
    {
        super("Data Base Information");
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.img = new ImageIcon("logo.png");
    }//constractor

    public void createForm(int rows,int columns,int vgap,int hgap)//orizei sto panel tou frame topothethsh sumfwna me to GridLayout
    {
        JPanel panel=new JPanel();
        GridLayout g=new GridLayout(rows,columns,vgap,hgap);
        panel.setLayout(g);
        this.setContentPane(panel);

        this.addElements();
        this.setIconImage(this.img.getImage());
        this.pack();

        submit.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                if (hostT.getText().length()!=0 && portT.getText().length()!=0 && dbNameT.getText().length()!=0 && userT.getText().length()!=0 && passT.getText().length()!=0)
                {
                    try {
                    CreateSettings s = new CreateSettings("Endocrino\\settings.txt");
                    s.write(hostT.getText(), portT.getText(), dbNameT.getText(), userT.getText(), passT.getText());
                    close();
                    } catch (FileNotFoundException e1) {
                        e1.printStackTrace();
                    } catch (Exception e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                }
                else{
                    JOptionPane.showMessageDialog(null,"All items must be filled","Warning",JOptionPane.ERROR_MESSAGE);
                }
            }
        });

        defaultOption.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {   hostT.setText("localhost");
                portT.setText("3306");
                dbNameT.setText("endocrino");
                userT.setText("endo");
                passT.setText("1234");
            }
        });
    }//createForm


    public void addButtons()//prosthetei to koumpi kataxwrhshs sto panel
    {
        Container c=this.getContentPane();
        this.submit=new JButton("OK");
        this.defaultOption=new JButton("Set Default");

        c.add(submit);
        c.add(defaultOption);
    }//addButtons

    public void addHost()//prosthetei to label kai text field gia egrafh tou host apo ton xrhsth
    {
        Container c=this.getContentPane();
        this.host=new JLabel("Host");
        this.hostT=new JTextField();
        c.add(host);
        c.add(hostT);
    }//addHost

    public void addPort()//prosthetei to label kai text field gia egrafh tou port apo ton xrhsth
    {
        Container c=this.getContentPane();
        this.port=new JLabel("Port");
        this.portT=new JTextField();
        c.add(port);
        c.add(portT);
    }//addPort

    public void addDBName()//prosthetei to label kai text field gia egrafh tou Data Base Name apo ton xrhsth
    {
        Container c=this.getContentPane();
        this.dbName=new JLabel("Data Base Name");
        this.dbNameT=new JTextField();
        c.add(dbName);
        c.add(dbNameT);
    }//addDBName

    public void addUser()//prosthetei to label kai text field gia egrafh tou user apo ton xrhsth
    {
        Container c=this.getContentPane();
        this.user=new JLabel("User");
        this.userT=new JTextField();
        c.add(user);
        c.add(userT);
    }//addUser

    public void addPassword()//prosthetei to label kai text field gia egrafh tou password apo ton xrhsth
    {
        Container c=this.getContentPane();
        this.pass=new JLabel("Password");
        this.passT=new JTextField();
        c.add(pass);
        c.add(passT);
    }//addPassword

    public void showForm()//emfanizei to frame
    {
        this.setVisible(true);
        this.setLocationRelativeTo(null);
    }//showFrame

    public void addElements(){
        this.addHost();
        this.addPort();
        this.addDBName();
        this.addUser();
        this.addPassword();
        this.addButtons();
    }//addElements

    public void close(){
        this.dispose();
        Test t=new Test();
        t.createForm(6,1,3,3);
        t.showForm();
        t.dosCommands();
    }//close
}//SettingsForm

And the Test class

class Test extends JFrame{

    JLabel firstMsg, progressMsg, enFolderMsg, dbMsg, shortcutMsg, finishedMsg;//koina JLabel.Etiketes host, port, dbName, user kai password
    ImageIcon img;

    public Test()//arxikh dhmiourgia tou frame
    {
        super("Endocrino Installation");
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.img = new ImageIcon("logo.png");
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        this.setLocation(dim.width/2-this.getSize().width/2, dim.height/2-this.getSize().height/2);
    }//constractor

    public void createForm(int rows,int columns,int vgap,int hgap)//orizei sto panel tou frame topothethsh sumfwna me to GridLayout
    {
        JPanel panel=new JPanel();
        GridLayout g=new GridLayout(rows,columns,vgap,hgap);
        panel.setLayout(g);
        this.setContentPane(panel);

        this.addElements();
        this.setIconImage(this.img.getImage());  
        this.pack();
    }//createForm

    public void addProgressMsg()//prosthetei to minima "Plese wait.."
    {
        Container c=this.getContentPane();
        this.progressMsg=new JLabel("Progress");
        c.add(this.progressMsg);
    }//addFirstMsg


    public void revalidateItems(){
        this.firstMsg.revalidate();
        this.progressMsg.revalidate();
        this.enFolderMsg.revalidate();
        this.dbMsg.revalidate();
        this.shortcutMsg.revalidate();
        this.finishedMsg.revalidate();
        revalidate();
    }
    public void repaintItems(){
        this.firstMsg.repaint();
        this.progressMsg.repaint();
        this.enFolderMsg.repaint();
        this.dbMsg.repaint();
        this.shortcutMsg.repaint();
        this.finishedMsg.repaint();
        repaint();
    }

    public void addFirstMsg()//prosthetei to minima "Plese wait.."
    {
        Container c=this.getContentPane();
        this.firstMsg=new JLabel("Please wait...");
        c.add(this.firstMsg);
    }//addFirstMsg

    public void addEnFolderMsg()//prosthetei to label gia tin dimiourgia tou fakelou C:/Endocrino
    {
        Container c=this.getContentPane();
        this.enFolderMsg=new JLabel();
        c.add(this.enFolderMsg);
    }//addEnFolderMsg

    public void addDBMsg()//prosthetei to label gia tin dimiourgia tis basis
    {
        Container c=this.getContentPane();
        this.dbMsg=new JLabel();
        c.add(this.dbMsg);
    }//addDBMsg

    public void addShortcutMsg()//prosthetei to label gia tin dimiourgia tou shortcut sto Desktop
    {
        Container c=this.getContentPane();
        this.shortcutMsg=new JLabel();
        c.add(this.shortcutMsg);
    }//addShortcutMsg

    public void addFinishedMsg()//prosthetei to label gia tin dimiourgia tou minimatos "finished"
    {
        Container c=this.getContentPane();
        this.finishedMsg=new JLabel();
        c.add(this.finishedMsg);
    }//addFinishedMsg

    public void showForm()//emfanizei to frame
    {
        this.setVisible(true);  
    }//showFrame

    public void addElements(){
        this.addProgressMsg();
        this.addFirstMsg();
        this.addEnFolderMsg();
        this.addDBMsg();
        this.addShortcutMsg();
        this.addFinishedMsg();
    }//addElements

    public void close(){
        this.dispose();
        System.exit(0);
    }

    public void dosCommands() throws InterruptedException{
        revalidateItems();repaintItems();
        this.enFolderMsg.setText("Creating Endocrino Folder...");
        revalidateItems();repaintItems();
        doSth();
        this.dbMsg.setText("Creating Data Base...");
        revalidateItems();repaintItems();
        doSth();
        this.shortcutMsg.setText("Creating shortcut...");
        revalidateItems();repaintItems();
        doSth();
        this.finishedMsg.setText("Finished");
        JOptionPane.showMessageDialog(null,"Now you can delete the Endocrino folder from your Desktop");
        close();
    }//dosCommands
}//Test

My problem is that the Test frame window is opened but it's JLabels not displaying...

mKorbel
  • 108,320
  • 17
  • 126
  • 296
xarlap
  • 157
  • 1
  • 2
  • 14

2 Answers2

0

Try adding this line at the end of your Close() method:

t.setVisible(true);    
Mel
  • 453
  • 1
  • 3
  • 13
0

I fix it. Here is the solution:

class SettingsForm extends JFrame{

    JButton submit,defaultOption;//koumpia kataxwrhshs-akurwshs
    JLabel host, port, dbName, user, pass;//koina JLabel.Etiketes host, port, dbName, user kai password
    JTextField hostT, portT, dbNameT, userT, passT;//koina JTextField.perioxh egrafhs twn host, port, dbName, user kai password
    ImageIcon img;

    public SettingsForm()//arxikh dhmiourgia tou frame
    {
        super("Data Base Information");
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.img = new ImageIcon("logo.png");
        this.setIconImage(this.img.getImage());
        this.pack();
        this.setVisible(true);
        this.setLocationRelativeTo(null);
    }//constractor

    public void createForm(int rows,int columns,int vgap,int hgap)//orizei sto panel tou frame topothethsh sumfwna me to GridLayout
    {
        JPanel panel=new JPanel();
        GridLayout g=new GridLayout(rows,columns,vgap,hgap);
        panel.setLayout(g);
        this.setContentPane(panel);

        this.addElements();

        submit.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                if (hostT.getText().length()!=0 && portT.getText().length()!=0 && dbNameT.getText().length()!=0 && userT.getText().length()!=0 && passT.getText().length()!=0)
                {
                    try {
                    CreateSettings s = new CreateSettings("Endocrino\\settings.txt");
                    s.write(hostT.getText(), portT.getText(), dbNameT.getText(), userT.getText(), passT.getText());
                    close();
                    } catch (FileNotFoundException e1) {
                        e1.printStackTrace();
                    } catch (Exception e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                }
                else{
                    JOptionPane.showMessageDialog(null,"All items must be filled","Warning",JOptionPane.ERROR_MESSAGE);
                }
            }
        });

        defaultOption.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {   hostT.setText("localhost");
                portT.setText("3306");
                dbNameT.setText("endocrino");
                userT.setText("endo");
                passT.setText("1234");
            }
        });
    }//createForm


    public void addButtons()//prosthetei to koumpi kataxwrhshs sto panel
    {
        Container c=this.getContentPane();
        this.submit=new JButton("OK");
        this.defaultOption=new JButton("Set Default");

        c.add(submit);
        c.add(defaultOption);
    }//addButtons

    public void addHost()//prosthetei to label kai text field gia egrafh tou host apo ton xrhsth
    {
        Container c=this.getContentPane();
        this.host=new JLabel("Host");
        this.hostT=new JTextField();
        c.add(host);
        c.add(hostT);
    }//addHost

    public void addPort()//prosthetei to label kai text field gia egrafh tou port apo ton xrhsth
    {
        Container c=this.getContentPane();
        this.port=new JLabel("Port");
        this.portT=new JTextField();
        c.add(port);
        c.add(portT);
    }//addPort

    public void addDBName()//prosthetei to label kai text field gia egrafh tou Data Base Name apo ton xrhsth
    {
        Container c=this.getContentPane();
        this.dbName=new JLabel("Data Base Name");
        this.dbNameT=new JTextField();
        c.add(dbName);
        c.add(dbNameT);
    }//addDBName

    public void addUser()//prosthetei to label kai text field gia egrafh tou user apo ton xrhsth
    {
        Container c=this.getContentPane();
        this.user=new JLabel("User");
        this.userT=new JTextField();
        c.add(user);
        c.add(userT);
    }//addUser

    public void addPassword()//prosthetei to label kai text field gia egrafh tou password apo ton xrhsth
    {
        Container c=this.getContentPane();
        this.pass=new JLabel("Password");
        this.passT=new JTextField();
        c.add(pass);
        c.add(passT);
    }//addPassword

    public void addElements(){
        this.addHost();
        this.addPort();
        this.addDBName();
        this.addUser();
        this.addPassword();
        this.addButtons();
    }//addElements

    public void close() throws InterruptedException{
        this.dispose();
        DosCommandsWindow d=new DosCommandsWindow();
        d.createForm(7,1,3,3);
    }//close

}//SettingsForm

and DosCommandsWindow

public class DosCommandsWindow extends JFrame{

/**
 * 
 */
JLabel progressMsg, firstMsg, enFolderMsg, dbMsg, shortcutMsg, finishedMsg;
ImageIcon img;
int flag;
JProgressBar pbar;

public DosCommandsWindow(){
    super("DNA ENDOCRINO");
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.img = new ImageIcon("logo.png");
    this.setIconImage(this.img.getImage());

    this.flag=1;

    this.firstMsg=new JLabel("Please wait...");
    this.progressMsg=new JLabel("Progress");
    this.enFolderMsg=new JLabel("Create C:\\Endocrino folder...");
    this.dbMsg=new JLabel();
    this.shortcutMsg=new JLabel();
    this.finishedMsg=new JLabel();

    pbar = new JProgressBar();
    pbar.setMinimum(0);
    pbar.setMaximum(4);
    this.pbar.setValue(1);

    this.setSize(300, 200);
    this.setVisible(true);
    this.setLocationRelativeTo(null);
}//constractor

public void createForm(int rows,int columns,int vgap,int hgap)//orizei sto panel tou frame topothethsh sumfwna me to GridLayout
{
    JPanel panel=new JPanel();
    GridLayout g=new GridLayout(rows,columns,vgap,hgap);
    panel.setLayout(g);
    this.setContentPane(panel);

    this.add(this.progressMsg);
    this.add(this.pbar);
    this.add(this.firstMsg);
    this.add(this.enFolderMsg);
    this.add(this.dbMsg);
    this.add(this.shortcutMsg);
    this.add(this.finishedMsg);

    SwingUtilities.invokeLater(new Runnable() {
          public void run() {

            if(flag==1){
                DosCommands.createEnFolder();
                createDB();
            }

            else if(flag==2){
                DosCommands.createDB();
                createShortcut();
            }
            else{
                DosCommands.createShortcut();
                JOptionPane.showMessageDialog(null,"Now you can delete the Endocrino folder from your Desktop");
                System.exit(0);
            }
        }
    });
}//createForm

public void createDB(){
    this.dbMsg.setText("Create Data Base...");
    this.pbar.setValue(2);
    createForm(7,1,3,3);
    this.flag++;
}

public void createShortcut(){
    this.shortcutMsg.setText("Create shortcut...");
    this.pbar.setValue(3);
    createForm(7,1,3,3);
    this.flag++;
    this.finishedMsg.setText("Finished");
    this.pbar.setValue(4);
}
}//DosCommandsWindow
xarlap
  • 157
  • 1
  • 2
  • 14