Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: JProgressBar not showing

  1. #1
    Junior Member
    Join Date
    Nov 2024
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JProgressBar not showing

    Hello,
    I am new to java and I made a app that installs Tic-tac-toe on your computer and when it is supposed to display a progress bar it doesn't.
    My code is:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.lang.*;
    import java.util.*;
    import java.io.*;
    import java.util.concurrent.TimeUnit;
     
    public class Installer {
     
        JFrame frame = new JFrame("Installer");
        JLabel dir = new JLabel();
        JPanel dirPanel = new JPanel();
        JButton selectButton = new JButton("Browse");
        JButton okButton = new JButton("Confirm");
        JPanel okPanel = new JPanel();
        JFileChooser fc = new JFileChooser(System.getProperty("user.home") + "//Downloads");
        String directoryRaw;
        String directory; 
        JProgressBar loading = new JProgressBar(0, 100);
        JFrame frame2 = new JFrame("Installing...");
     
        public static void main(String[] args) {
            Installer m = new Installer();
            m.frame.setSize(500, 120);
            m.frame.setLocationRelativeTo(null);
            m.frame.setVisible(true);
            m.dir.setText("Select A Directory:       ");
            m.dirPanel.add(m.dir);
            m.dirPanel.add(m.selectButton);
            m.okButton.setEnabled(false);
            m.frame.add(m.dirPanel, BorderLayout.NORTH);
            m.okPanel.add(m.okButton);
            m.frame.add(m.okPanel, BorderLayout.SOUTH);
            m.fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            m.selectButton.addActionListener(new ActionListener() {
     
                // Override the actionPerformed() method
                public void actionPerformed(ActionEvent e){
                    //Handle open button action.
                    if (e.getSource() == m.selectButton) {
                        int returnVal = m.fc.showOpenDialog(null);
     
                        if (returnVal == JFileChooser.APPROVE_OPTION) {
                            File file = m.fc.getSelectedFile();
                            //This is where a real application would open the file.
                            m.directoryRaw = file.getAbsolutePath();
                            System.out.println(m.directoryRaw);
                            m.dir.setText(file.getAbsolutePath() + "       ");
                            m.okButton.setEnabled(true);
                        } else {
                            m.dir.setText("Select A Directory:       ");
                        }
                   }
                }
     
            });
            m.okButton.addActionListener(new ActionListener() {
     
                // Override the actionPerformed() method
                public void actionPerformed(ActionEvent e){
                    m.Install();
                }
     
            });
        }
     
        public void Install() {
            directory = directoryRaw.replace("\\","\\\\");
            frame2.setSize(500, 120);
            frame2.setLocationRelativeTo(null);
            frame2.setVisible(true);
            loading.setBounds(50, 30, 200, 30); 
            loading.setValue(0);
            loading.setStringPainted(true);
            loading.setBackground(new Color(0, 51, 51));
            loading.setVisible(true);
            frame2.add(loading);
            for(int i = 0; i < 100; i++) {
                loading.setString(Integer.toString(loading.getValue()+1) + "%");
                loading.setValue(loading.getValue()+1);
                if(loading.getValue()+1 < 100) {
                    System.out.println(Integer.toString(loading.getValue()+1) + "%");
                }
                try{
                    TimeUnit.MILLISECONDS.sleep(100);
                } catch(Exception e) {
     
                }
            }
            try {
                File ttt = new File(directory + "\\TicTacToe.java");
                if (ttt.createNewFile()) {
     
                } else {
                    System.out.println("File already exists.");
                }
            } catch (IOException e) {
     
            }
            try {
                FileWriter TTT = new FileWriter(directory + "\\TicTacToe.java");
                TTT.write("import java.awt.*; \n import java.awt.event.*; \n import javax.swing.*; \n import java.lang.*; \n import java.util.*; \n import java.io.*; \n public class TicTacToe { \n int boardWidth = 600; \n int boardHeight = 650; \n JFrame frame = new JFrame(\"Tic-Tac-Toe\"); \n JLabel textLabel = new JLabel(); \n JPanel textPanel = new JPanel(); \n JPanel boardPanel = new JPanel(); \n JButton[][] board = new JButton[3][3]; \n String playerX = \"X\"; \n String playerO = \"O\"; \n String currentPlayer = playerX; \n boolean gameOver = false; \n int turns = 0; \n public static void main(String[] args) { \n TicTacToe m = new TicTacToe(); \n m.frame.setVisible(true); \n m.frame.setSize(m.boardWidth, m.boardHeight); \n m.frame.setLocationRelativeTo(null); \n m.frame.setResizable(false); \n m.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); \n m.frame.setLayout(new BorderLayout()); \n m.textLabel.setOpaque(true); \n m.textLabel.setBackground(new Color(100, 100, 100)); \n m.textLabel.setForeground(Color.white); \n m.textLabel.setFont(new Font(\"Arial\", Font.BOLD, 50)); \n m.textLabel.setHorizontalAlignment(JLabel.CENTER); \n m.textLabel.setText(\"Tic-Tac-Toe\"); \n m.textPanel.setLayout(new BorderLayout()); \n m.textPanel.add(m.textLabel); \n m.frame.add(m.textPanel, BorderLayout.NORTH); \n m.boardPanel.setLayout(new GridLayout(3, 3)); \n m.boardPanel.setBackground(new Color(100, 100, 100)); \n m.frame.add(m.boardPanel); \n for(int r = 0; r < 3; r++) { \n for(int c = 0; c < 3; c++) { \n JButton tile = new JButton(); \n m.board[r][c] = tile; \n m.boardPanel.add(tile); \n tile.setBackground(new Color(100, 100, 100)); \n tile.setForeground(Color.white); \n tile.setFont(new Font(\"Arial\", Font.BOLD, 120)); \n tile.setFocusable(false); \n tile.addActionListener(new ActionListener() { \n public void actionPerformed(ActionEvent e) { \n if(m.gameOver) return; \n JButton tile = (JButton) e.getSource(); \n if(tile.getText() == \"\") { \n tile.setText(m.currentPlayer); \n m.turns++; \n m.checkWinner(); \n if(!m.gameOver) { \n m.currentPlayer = m.currentPlayer == m.playerX ? m.playerO : m.playerX; \n m.textLabel.setText(m.currentPlayer + \"'s turn.\"); \n } \n } \n } \n }); \n } \n } \n } \n void checkWinner() { \n for(int r = 0; r < 3; r++) { \n if(board[r][0].getText() == \"\") continue; \n if(board[r][0].getText() == board[r][1].getText() && board[r][1].getText() == board[r][2].getText()) { \n for(int i = 0; i < 3; i++) { \n setWinner(board[r][i]); \n } \n gameOver = true; \n return; \n } \n } \n for(int c = 0; c < 3; c++) { \n if(board[0][c].getText() == \"\") continue; \n if(board[0][c].getText() == board[1][c].getText() && board[1][c].getText() == board[2][c].getText()) { \n for(int i = 0; i < 3; i++) { \n setWinner(board[i][c]); \n } \n gameOver = true; \n return; \n } \n } \n if(board[0][0].getText() == board[1][1].getText() && board[1][1].getText() == board[2][2].getText() && board[0][0].getText() != \"\") { \n for(int i = 0; i < 3; i++) { \n setWinner(board[i][i]); \n } \n gameOver = true; \n return; \n } \n if(board[0][2].getText() == board[1][1].getText() && board[1][1].getText() == board[2][0].getText() && board[0][2].getText() != \"\") { \n setWinner(board[0][2]); \n setWinner(board[1][1]); \n setWinner(board[2][0]); \n gameOver = true; \n return; \n } \n if(turns == 9) { \n for(int r = 0; r < 3; r++) { \n for(int c = 0; c < 3; c++) { \n setTie(board[r][c]); \n } \n } \n gameOver = true; \n } \n } \n void setWinner(JButton tile) { \n tile.setForeground(Color.green); \n tile.setBackground(new Color(127, 127, 127)); \n textLabel.setText(currentPlayer + \" is the winner!!!\"); \n frame.addMouseListener(new MouseListener() { \n public void mouseClicked(MouseEvent e) {} \n public void mousePressed(MouseEvent e) { \n resetGame(); \n } \n public void mouseReleased(MouseEvent e) {} \n public void mouseEntered(MouseEvent e) {} \n public void mouseExited(MouseEvent e) {} \n }); \n } \n void setTie(JButton tile) { \n tile.setForeground(Color.orange); \n tile.setBackground(new Color(127, 127, 127)); \n textLabel.setText(\"TIE!\"); \n frame.addMouseListener(new MouseListener() { \n public void mouseClicked(MouseEvent e) {} \n public void mousePressed(MouseEvent e) { \n resetGame(); \n } \n public void mouseReleased(MouseEvent e) {} \n public void mouseEntered(MouseEvent e) {} \n public void mouseExited(MouseEvent e) {} \n }); \n } \n void Wait(long waitTime) { \n long startTime = System.currentTimeMillis(); \n while(System.currentTimeMillis()-startTime < waitTime) {} \n return; \n } \n void resetGame() { \n for(int r = 0; r < 3; r++) { \n for(int c = 0; c < 3; c++) { \n board[r][c].setText(\"\"); \n board[r][c].setBackground(new Color(100, 100, 100)); \n board[r][c].setForeground(Color.white); \n } \n } \n textLabel.setText(\"X's turn.\"); \n turns = 0; \n currentPlayer = playerX; \n gameOver = false; \n } \n }");
                TTT.close();
            } catch(IOException e) {
                System.out.println("An error occurred.");
                e.printStackTrace();
            }
            System.exit(0);
        }
    }

    Any help is very appreciated.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: JProgressBar not showing

    Take a look at the tutorial: https://docs.oracle.com/javase/tutor.../progress.html
    It has example code that shows how to use a ProgressBar
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. GUI freezes instead of showing JProgressBar
    By ciliegia in forum What's Wrong With My Code?
    Replies: 8
    Last Post: January 31st, 2022, 08:46 AM
  2. JProgressBar
    By Tyluur in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 25th, 2013, 06:59 AM
  3. JProgressBar Issues
    By dr3wmurphy in forum AWT / Java Swing
    Replies: 1
    Last Post: August 11th, 2012, 01:15 AM
  4. Jtable with JprogressBar
    By dibyayan in forum AWT / Java Swing
    Replies: 1
    Last Post: October 17th, 2011, 07:33 AM
  5. Replies: 1
    Last Post: February 16th, 2009, 11:52 AM