954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Unworking GUI, window not showing

Its pretty standard, why the hell isnt the window showing?? Cant figure it out :(

import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*;

public  class GUIpart {   
    //All ze variables required
    int tempboard[][]=new int[9][9];
    SudokuBoard fresh= new SudokuBoard();
    JFrame sudoku = new JFrame("Srik Sudoku");
    JFormattedTextField guiboard[][] = new JFormattedTextField[9][9];
    
    public void GUIpart(){
        sudoku.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel grids = new JPanel();
        grids.setLayout(new GridLayout(10,9));
        try{
            MaskFormatter format=new MaskFormatter("#");
            
            for(int i=0;i<9;i++){
                for(int j=0;j<9;j++){
                    guiboard[i][j]=new JFormattedTextField(format);
                    guiboard[i][j].setColumns(1);
                    grids.add(guiboard[i][j]);
                }
            }
        }
        catch(Exception e){
            System.out.println("Jai");
        }
        
        JButton solve = new JButton("Solve");
        solve.addActionListener(new solveActionListener());
        grids.add(solve);
        
        sudoku.getContentPane().add(grids);
        sudoku.pack();
        sudoku.setVisible(true);
        
        
    }
    
    public  static void gogui(){
        JFrame.setDefaultLookAndFeelDecorated(true);

        GUIpart yay= new GUIpart(); 
        
    }
    private  class solveActionListener implements ActionListener{
           public void actionPerformed(ActionEvent e){
                for(int i=0;i<9;i++){
                    for(int j=0;j<9;j++){
 tempboard[i][j]=Integer.parseInt(guiboard[i][j].getText());
                    }               
                }
                fresh.inputBoard(tempboard);
                fresh.generateSolution();
                tempboard=fresh.sendBoard();
                for(int i=0;i<9;i++){
                    for(int j=0;j<9;j++){
                        guiboard[i][j].setText(Integer.toString(tempboard[i][j]));
                    }               
                }
           }
        
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                gogui();
            }
        });
    }
    
}
jetru
Light Poster
27 posts since Jun 2006
Reputation Points: 10
Solved Threads: 0
 

public static void gogui() you do not realy need this method as you can set L&F before in the main method. Your code would work if you called yay.GUIpart() . Bellow is simpler solution. Remove public static void gogui() and replace main method with code bellow

public static void main(String[] args) 
{
    	JFrame.setDefaultLookAndFeelDecorated(true);
    	
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                GUIpart gui = new GUIpart();
                gui.GUIpart();
            }
        });
    }
peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

Hmm yes that is much more simpler and easier to read!

And the problem in the my program was that GUIpart wasnt a constructor because I had put public void GUIpart() and constructors shouldn't have return types. Which is why I see you've called GUIpart()...

Thanks! :)

jetru
Light Poster
27 posts since Jun 2006
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You