When I set the min, value, and max of my JProgressBar they do not update unless I close the window and reopen it.
Picture:
Can anyone give me any insight? I am scratching my head on this one. I tested to make sure that the parsing is done correctly(which is is). I tested to just put numbers in directly. Clearly it works, it just does not show the first time the window is opened(which makes me think that if I update the values it will only show the last values.
* EDIT *
Ladies and Gentleman... may I present... SSCCE. I am sorry to post this because now you will feel my pain with this :x
package com.jayavon.game.helper;
import javax.swing.*;
import java.awt.event.*;
public class SSCCE extends JFrame implements WindowListener {
private static final long serialVersionUID = 1L;
JFrame frame;
JPanel panel;
JButton characterButton;
JInternalFrame characterFrame;
/* Character Window */
JProgressBar totalExpProgressBar;
Action ClassCharacterButton = new ClassCharacterButton();
public static void main(String[] args){
//for thread safety
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new SSCCE();
}
});
}
SSCCE() {
initGUI();
}
public void initGUI(){
frame = new JFrame("SSCCE");
panel = (JPanel)frame.getContentPane();
/**********************************
**
** Buttons
**
*********************************/
characterButton = new JButton("");
characterButton.setBounds(50,175,395,100);
characterButton.setVisible(true);
characterButton.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("C"), "characterButtonPress");
characterButton.getActionMap().put("characterButtonPress", ClassCharacterButton);
characterButton.setAction(ClassCharacterButton);
characterButton.setText("Click me three times(open/close/open) to get progress bar to fill");
panel.add(characterButton);
/**********************************
**
** Internal Frames
**
*********************************/
//#### Character frame start ####
characterFrame = new JInternalFrame("Character", true, true, false, false);
characterFrame.setLocation(50, 50);
characterFrame.setSize(300,105);
totalExpProgressBar = new JProgressBar();
totalExpProgressBar.setString("0/0");
totalExpProgressBar.setStringPainted(true);
characterFrame.add(totalExpProgressBar);
characterFrame.setResizable(false);
panel.add(characterFrame);
//#### Character frame end ####
/**********************************
**
** Panel Code
**
*********************************/
panel.setLayout(null);
panel.setFocusable(true);
/**********************************
**
** Frame Code
**
*********************************/
frame.setLocation(100, 100);
frame.setSize(500, 350);
frame.setVisible(true);
frame.setFocusable(true);
frame.addWindowListener(this);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
class ClassCharacterButton extends AbstractAction {
private static final long serialVersionUID = 1L;
public void actionPerformed(ActionEvent e) {
if (characterFrame.isVisible()){
characterFrame.setVisible(false);
} else {
fakeGetServerResponse();
}
}
}
public void fakeGetServerResponse(){
String incommingReply = "proskier-charactersWindow@20|10|10|10|0|234|3|200|400"; //fake message from server
final String splitAt[] = incommingReply.split("@"); //split the character name from the incommingReply at the '@' sign
String beforeAt[] = splitAt[0].split("-");
String commandName = beforeAt[1];
final String afterAt[] = splitAt[1].split("\\|");
if (commandName.equals("charactersWindow")){
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
totalExpProgressBar.setString(afterAt[5] + "/" + afterAt[8]);
totalExpProgressBar.setMinimum(0);
totalExpProgressBar.setMinimum(Integer.parseInt(afterAt[7])); //TODO::SCREW YOU JAVA
totalExpProgressBar.setValue(Integer.parseInt(afterAt[5])); //TODO::SCREW YOU JAVA
totalExpProgressBar.setMaximum(Integer.parseInt(afterAt[8])); //TODO::SCREW YOU JAVA
characterFrame.setVisible(true);
}
});
}
}
@Override
public void windowClosing(WindowEvent arg0){
frame.dispose();
System.exit(1);
}
@Override
public void windowActivated(WindowEvent arg0) {
}
@Override
public void windowClosed(WindowEvent arg0) {
}
@Override
public void windowDeactivated(WindowEvent arg0) {
}
@Override
public void windowDeiconified(WindowEvent arg0) {
}
@Override
public void windowIconified(WindowEvent arg0) {
}
@Override
public void windowOpened(WindowEvent arg0) {
}
}
The problem appears to be that you set the progress bar's maximal value after setting its value, and that this will reset the value to the minimum value.
import javax.swing.*;
public class ProgressBarMinValue {
private static void createAndShowGui() {
JProgressBar progressBar = new JProgressBar();
int value = 234;
int denominator = 400;
int minValue = 200;
progressBar.setString(value + "/" + denominator);
progressBar.setMinimum(minValue);
System.out.println("value := " + value);
progressBar.setValue(value); // (A)
progressBar.setMaximum(denominator);
// progressBar.setValue(value); // (B)
JPanel mainPanel = new JPanel();
mainPanel.add(progressBar);
JOptionPane.showMessageDialog(null, mainPanel);
System.out.println("progressBar.getValue() := " + progressBar.getValue());
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
Swap commenting and uncommenting lines (A) and (B) and see that all works well if you set the progress bar's value after setting its minimal and maximal value.
And that's your solution.
Note that I obtained this minimal SSCCE by continuously whittling down your code until the problem could not be reproduced.