Again, please help me!
In the following code, I want to get started by pushing a timer thread at the push of a button and typing it in a Label. Each press of the button should start a new thread and mark it on each label. But unfortunately, the same timer is written for each label. Can you help us get it right? If you can tell me the mistake, what do I mean?
public class TimerThreads implements ActionListener{
JFrame jFrame = new JFrame();
JLabel[] labels;
int second = 0;
int minute = 0;
String s = "";
String m = "";
int l = 0;
public TimerThreads(){
JLabel one = new JLabel();
JLabel two = new JLabel();
JLabel three = new JLabel();
JLabel four = new JLabel();
labels = new JLabel[]{one, two, three, four};
jFrame.setLayout(new GridLayout(0, 2, 5, 5));
jFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JButton start = new JButton("Start");
start.addActionListener(this);
JButton stop = new JButton("Stop");
stop.addActionListener(this);
jFrame.add(start);
jFrame.add(stop);
jFrame.setVisible(true);
jFrame.pack();
}
public static void main(String[] args) {
new TimerThreads();
}
#Override
public void actionPerformed(ActionEvent e) {
String select = e.getActionCommand();
switch(select){
case "Start":
jFrame.add(labels[l]);
jFrame.revalidate();
jFrame.repaint();
TimerThread t = new TimerThread(labels[l]);
t.start();
l++;
break;
case "Stop":
//
break;
}
}
class TimerThread extends Thread{
JLabel jLabel;
public TimerThread(JLabel jLabel) {
this.jLabel = jLabel;
}
#Override
public void run() {
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
second++;
if(String.valueOf(second).length() == 1){
s = "0";
}
else{
s = "";
}
if(second == 60){
second = 0;
s = "0";
minute++;
}
if(String.valueOf(minute).length() == 1){
m = "0";
}
jLabel.setText(m + String.valueOf(minute) + ":" + s + String.valueOf(second));
}
},0, 1000);
}
}
}
The cause of your bug is here:
public class TimerThreads implements ActionListener {
JFrame jFrame = new JFrame();
JLabel[] labels;
// ***** these fields below
int second = 0;
int minute = 0;
String s = "";
String m = "";
// ***** these fields above
int l = 0;
The four fields are instance fields of the class and are shared by every TimerTask instance that you create, and so all will show the same exact time.
The solution is to make these fields local to the nested class:
public class TimerThreads implements ActionListener {
JFrame jFrame = new JFrame();
JLabel[] labels;
// int second = 0;
// int minute = 0;
// String s = "";
// String m = "";
int l = 0;
public TimerThreads() {
//.....
}
// ....
class TimerThread extends Thread {
JLabel jLabel;
public TimerThread(JLabel jLabel) {
this.jLabel = jLabel;
}
#Override
public void run() {
java.util.Timer timer = new java.util.Timer();
timer.scheduleAtFixedRate(new TimerTask() {
// ***** add these fields here
int second = 0;
int minute = 0;
String s = "";
String m = "";
Having said this, you've got risky code since you're using the wrong Timer, a java.util.Timer, when you should be using a Swing Timer or javax.swing.Timer. This is very important since the latter Timer works well with the Swing event model and prevents thread clashes. Please check out the Swing Timer Tutorial
Other problems: using a fixed size array, you risk index out of bounds exception if the user wants more than 4 threads running. Use an ArrayList instead.
Related
I'm trying to make my version of game of life reset when a reset button is clicked but I have an issue.
After clicking the button, everything successfully reset but not the main Jpanel where we see the generations moving.
I have two JLabel, one showing the number of the current generation and the other showing the amount of alive cells in that generation. They are both reseted successfully but the main JPanel just freezes and I cannot see the animation anymore.
GameOfLife class:
public class GameOfLife extends JFrame implements ActionListener {
private static class GameStep extends TimerTask {
static GameOfLife life = new GameOfLife();
#Override
public void run() {
updateLabels();
}
}
static JLabel aliveLabel = new JLabel("Alive:");
static JLabel GenerationLabel = new JLabel("Generation #");
static CellGrid body = new CellGrid();
static JPanel header = new JPanel();
static int genNumber = 1;
static JButton PlayToggleButton = new JButton("pause");
static JButton ResetButton = new JButton("reset");
static Boolean isPaused = false;
static GameStep game = new GameStep();
static Timer timer = new Timer();
public GameOfLife() {
super("Game of life");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(700, 660);
setLocationRelativeTo(null);
setLayout(new FlowLayout());
GenerationLabel.setName("GenerationLabel");
aliveLabel.setName("aliveLabel");
PlayToggleButton.setName("PlayToggleButton");
ResetButton.setName("ResetButton");
PlayToggleButton.addActionListener(this);
ResetButton.addActionListener(this);
PlayToggleButton.setIcon(new ImageIcon(play));
ResetButton.setIcon(new ImageIcon(reset));
PlayToggleButton.setPreferredSize(new Dimension(40,30));
ResetButton.setPreferredSize(new Dimension(40,30));
header.setLayout(new FlowLayout());
header.setPreferredSize(new Dimension(100, this.getHeight()));
header.add(PlayToggleButton);
header.add(ResetButton);
header.add(GenerationLabel);
header.add(aliveLabel);
body.setLayout(new BorderLayout());
body.setPreferredSize(new Dimension(500, this.getHeight()));
add(header, BorderLayout.WEST);
add(body, BorderLayout.CENTER);
setVisible(true);
}
public static void updateLabels(){
body.run();
GenerationLabel.setText("Generation #"+ genNumber++);
aliveLabel.setText("Alive: "+ body.totalAlive());
}
#Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("pause")){
pauseResume();
}
else if(e.getActionCommand().equals("reset")){
reset();
}
}
static void loopStep(){
timer.schedule(game, 0,1000);
}
static void pauseResume() {
if(!isPaused){
isPaused = true;
timer.cancel();
}
else{
isPaused = false;
timer = new Timer();
timer.schedule(new GameStep(), 0,1000);
}
}
static void reset() {
timer.cancel();
isPaused = false;
genNumber = 1;
header = new JPanel();
body = new CellGrid();
body.repaint();
timer = new Timer();
timer.schedule(new GameStep(), 0,1000);
}
public static void main(String[] args) {
loopStep();
}
}
CellGrid class:
public class CellGrid extends JPanel implements Runnable{
private static final int ROWS = 60;
private static final int COLS = 60;
private static final int CELL_WIDTH = 10;
private static Cell[][] cellGrid = new Cell[ROWS][COLS];
public CellGrid() {
for (int row = 0; row < cellGrid.length; row++) {
for (int col = 0; col < cellGrid[row].length; col++) {
int x = col * CELL_WIDTH;
int y = row * CELL_WIDTH;
cellGrid[row][col] = new Cell(x, y, CELL_WIDTH);
if (new Random().nextBoolean()) {
cellGrid[row][col].setAlive(true);
} else {
cellGrid[row][col].setAlive(false);
}
}
}
}
public int totalAlive(){
int totalAlive = 0;
for (Cell[] cells : cellGrid) {
for (int j = 0; j < cellGrid.length; j++) {
if (cells[j].isAlive())
totalAlive++;
}
}
return totalAlive;
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
for (Cell[] cellRow : cellGrid) {
for (Cell cell : cellRow) {
cell.draw(g2);
}
}
}
#Override
public void run() {
cellGrid = new GenerationMaker4().nextGeneration(cellGrid);
repaint();
}
}
any idea why it's happening ?
Your reset() method:
static void reset() {
timer.cancel();
isPaused = false;
genNumber = 1;
header = new JPanel();
body = new CellGrid();
body.repaint();
timer = new Timer();
timer.schedule(new GameStep(), 0,1000);
}
The problem is a common newbie mistake -- you think that changing a variable reference will change the prior object that the variable originally referenced.
Specifically, you have body = new CellGrid(); and what this does is have the body variable refer to a new CellGrid object, but (and here is the important part), it *does nothing to the CellGrid object that is currently displayed in your GUI, that the body variable previously referred to.
Several alternative solutions:
Add the new CellGrid object now referred to in the body variable to the GUI in the same BorderLayout position, covering the previous one
Better still is to not create a new CellGrid object but instead to create a way to set the current CellGrid back to its initial state.
For example, perhaps if you changed CellGrid to...
public class CellGrid extends JPanel implements Runnable{
private static final int ROWS = 60;
private static final int COLS = 60;
private static final int CELL_WIDTH = 10;
private Cell[][] cellGrid = new Cell[ROWS][COLS]; // make this non-static
public CellGrid() {
reset();
}
public void reset() {
cellGrid = new Cell[ROWS][COLS];
for (int row = 0; row < cellGrid.length; row++) {
for (int col = 0; col < cellGrid[row].length; col++) {
int x = col * CELL_WIDTH;
int y = row * CELL_WIDTH;
cellGrid[row][col] = new Cell(x, y, CELL_WIDTH);
if (new Random().nextBoolean()) {
cellGrid[row][col].setAlive(true);
} else {
cellGrid[row][col].setAlive(false);
}
}
}
}
// ..... more code below
Then all you need to do is to call reset() on the current CellGrid object, and then call repaint().
Other issues:
You grossly over-use the static modifier. Nothing in this program should be static, other than the main method, your constants, and that's it. This may not be important for this small program, but it will become important later when you try to do unit testing or extend or enhance this program, or add it to another larger program.
You use java.util.Timer and java.util.TimerTask to run an animation loop in a Swing GUI program, and this is not safe to do, since these classes are not Swing thread-safe. Much better to use a javax.swing.Timer or "Swing Timer" in place of both of these classes to run the animation since this is thread-safe for this GUI library.
I am having problem accessing the variable int i inside the inner class
Code:
public class OnColumnSelectPanel {
JFrame jFrame;
JPanel mainJPanel;
//JPanel jPanel1;
//JTextField jTextField;
//JButton jButton;
//JComboBox jComboBox [];
MigLayout layout;
MigLayout frameLayout;
//column names
ColumnNames cn = new ColumnNames();
List<String> listedColumnNames = cn.getColumnNames();
String columnNames[] = listedColumnNames.toArray(new String[0]);
public OnColumnSelectPanel(int n) {
//jPanel1 = new JPanel();
jFrame = new JFrame("Create Structure of Columns");
// mainJPanel = new JPanel();
layout = new MigLayout("flowy", "[center]rel[grow]", "[]10[]");
frameLayout = new MigLayout("flowx", "[center]rel[grow]", "[]10[]");
//mainJPanel.setLayout(new BoxLayout(mainJPanel, BoxLayout.X_AXIS));
//MigLayout mainJPanelLayout = new MigLayout("flowy", "[]rel[grow]", "[]5[]");
// declare & initialize array
JPanel jPanel[] = new JPanel[n];
JComboBox jComboBox[] = new JComboBox[n];
JButton jButton[] = new JButton[n];
final int num = 0;
for (int i = 0; i < n; i++) {
//assign array
jComboBox[i] = new JComboBox(columnNames);
jButton[i] = new JButton("add Sub Heading");
jPanel[i] = new JPanel();
System.out.println("Loop number: " + i);
jButton[i].addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
for (int j = 0; j < n; j++) {
if (j <= n) {
jComboBox[j] = new JComboBox(columnNames);
jPanel[j].add(jComboBox[j]);
jFrame.revalidate();
} else {
JOptionPane.showMessageDialog(null, "You have exceeded your limit");
}
}
}
});
//set layout
jPanel[i].setLayout(layout);
//jPanel[i].setPreferredSize(new Dimension(50, 400));
//mainJPanel.setLayout(mainJPanelLayout);
jFrame.setLayout(frameLayout);
System.out.println(" height " + jPanel[i].getHeight());
jPanel[i].add(jComboBox[i], new CC().newline());
//jPanel.add(jTextField);
jPanel[i].add(jButton[i]);
//mainJPanel.add(jPanel[i], "spany");
jPanel[i].repaint();
jFrame.add(jPanel[i]);
jFrame.pack();
jPanel[i].revalidate();
jFrame.revalidate();
//mainJPanel.revalidate();
//jFrame.revalidate();
}
//mainJPanel.setSize(600, 400);
//jFrame.add(jPanel1);
jFrame.setVisible(true);
jFrame.setSize(600, 400);
jFrame.setAlwaysOnTop(true);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Output Frame
As you can see the output image. The problem here is that when i click on the add subheading button, combobox is added to every jpanel. this is because i can;t pass the value i to the inner class.
It would be interesting to know possible solutions for this.
FYI I am using Mig Layout.
Thanks in advance.
Only effectively final variables can be accessed from anonymous classes.
i is modified by the loop, so it is not effectively final.
A workaround would look like this:
for (int i = 0; i < n; i++) {
...
final int effectivelyFinal = i;
jButton[i].addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
...
// use effectivelyFinal instead of i
});
}
But as other suggested, it would be way better to extract the anonymous class into a real class and pass all required parameters using the constructor. This can look like this:
class MyListener implements ActionListener {
private final int index;
// add more fields for other required parameters
public MyListener(int index) {
this.index = index;
}
#Override
public void actionPerformed(ActionEvent e) {
// use index
}
}
Usage:
jButton[i].addActionListener(new MyListener(i));
I am creating a program to take in sets of binary digits and convert them into hammingcodes (Effectively take in 8 digits, turn into 12, print out) but i am having trouble. Currently, i am using a JTextField for the user to enter their number, then they press a JButton to enter the data. I then do funky shit with that number to put it into a list and confirm that this is the last of the numbers they wish to enter. If they click a JButton called yes (New text in button, but same button) if goes on to do what i need. But if they click the other JButton called no, it goes back and repeats the same process. My problem is after clicking no once, the program stops allowing you to press no at the step to check if you want to add another list of numbers. IT appears to skip the check all together and assume they pressed yes as it does the rest of the working out thats done after all entry is finished.
My code is a tad messy due to messing with it for a few hours.
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import javax.swing.*;
public class MainProgram extends JFrame
{
public MainProgram()
{
}
public static void main(String[] args)
{
MainProgram mp = new MainProgram();
mp.run();
}
private void run()
{
java.util.List<Integer> streamSplit = new ArrayList<>();
java.util.List<Integer> tempEight = new ArrayList<>();
java.util.List<Integer> finalStream = new ArrayList<>();
yes.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
checkYes = true;
}
});
no.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
checkNo = true;
}
});
init();
yesChange("Enter");
boolean confirm = false;
int aCheck = 0;
while (aCheck == 0)
{
confirm = false;
while (!confirm)
{
setTopText("<html>Please enter your next 8 bits. Do not enter more than 8 bits.<br> Press Enter when done</html>");
yesChange("Enter");
confirm = checkYes();
}
confirm = false;
setTopText("Digits Successfully added.");
int stream = checkInput();
do
{
streamSplit.add(stream % 10);
stream /= 10;
} while (stream != 0);
setYesNo();
setTopText("<html>Are you finished entering streams?</html>");
yesChange("YES");
noChange("NO");
aCheck = 2;
checkYes();
checkNo();
while (aCheck == 2)
{
if ( checkNo())
{
aCheck = 0;
System.out.println("CrapNo");
}
else if (checkYes())
{
aCheck = 1;
System.out.println("CrapYes");
}
}
}
int arrayLength = streamSplit.size();
int bufferLength = 8 - arrayLength % 8;
int numberOfStreams = 0;
if (bufferLength != 8)
{
numberOfStreams = arrayLength / 8 + 1;
} else
{
numberOfStreams = arrayLength / 8;
}
int tempStreams = numberOfStreams;
System.out.println(numberOfStreams + "<Streams Buffer>" + bufferLength);
while (bufferLength > 0 && bufferLength != 8)
{
streamSplit.add(0);
bufferLength--;
}
while (tempStreams > 0)
{
for (int i = 0; i < 8; i++)
{
tempEight.add(streamSplit.get(i));
}
if ((tempEight.get(0) + tempEight.get(1) + tempEight.get(3) + tempEight.get(4) + tempEight.get(6)) % 2 == 0)
{
tempEight.add(0, 0);
} else
{
tempEight.add(0, 1);
}
if ((tempEight.get(1) + tempEight.get(3) + tempEight.get(5) + tempEight.get(6) + tempEight.get(7)) % 2 == 0)
{
tempEight.add(1, 0);
} else
{
tempEight.add(1, 1);
}
if ((tempEight.get(3) + tempEight.get(4) + tempEight.get(5) + tempEight.get(8) + tempEight.get(9)) % 2 == 0)
{
tempEight.add(3, 0);
} else
{
tempEight.add(3, 1);
}
if ((tempEight.get(7) + tempEight.get(8) + tempEight.get(9) + tempEight.get(10)) % 2 == 0)
{
tempEight.add(7, 0);
} else
{
tempEight.add(7, 1);
}
tempStreams--;
for (int i = 0; i < 12; i++)
{
finalStream.add(tempEight.get(0));
tempEight.remove(0);
}
}
Collections.reverse(streamSplit);
System.out.print("Your original bit-stream was: ");
for (int i = 0; i < numberOfStreams * 2; i++)
{
for (int j = 0; j < 4; j++)
{
System.out.print(streamSplit.get(j + (i * 4)));
}
System.out.print(" ");
}
System.out.println();
System.out.print("Your new HammingCode bit-stream is: ");
for (int i = 0; i < numberOfStreams * 3; i++)
{
for (int j = 0; j < 4; j++)
{
System.out.print(finalStream.get(j + (i * 4)));
}
System.out.print(" ");
}
System.out.println();
}
public Boolean checkYes = false;
public Boolean checkNo = false;
private JFrame frame = new JFrame("Absolute Layout Example");
private JPanel contentPane = new JPanel();
private JLabel topText = new JLabel("Welcome to my Hamming Code Generator", JLabel.CENTER);
private JTextField inputText = new JTextField();
private JButton yes = new JButton("YES");
private JButton no = new JButton("NO");
public void init()
{
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane.setOpaque(true);
contentPane.setBackground(Color.WHITE);
contentPane.setLayout(null);
topText.setLocation(0, 0);
topText.setSize(400, 50);
topText.setBorder(BorderFactory.createLineBorder(Color.black));
inputText.setLocation(0,50);
inputText.setSize(400,75);
inputText.setBorder(BorderFactory.createLineBorder(Color.black));
yes.setSize(80, 40);
yes.setLocation(60, 135);
no.setSize(80, 40);
no.setLocation(260, 135);
contentPane.add(topText);
contentPane.add(inputText);
contentPane.add(yes);
contentPane.add(no);
frame.setContentPane(contentPane);
frame.setSize(400, 225);
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public void setTopText(String s)
{
topText.setText(s);
}
public void setYesNo()
{
checkYes = false;
checkNo = false;
}
public Boolean checkYes() {return checkYes;}
public Boolean checkNo() {return checkNo;}
public int checkInput()
{
String temp1 = inputText.getText();
int temp = Integer.parseInt(temp1);
return temp;
}
public void yesChange(String s)
{
yes.setText(s);
}
public void noChange(String s)
{
no.setText(s);
}
}
I find it tough to answer this question not fully knowing what your code is doing, especially the part where you "... do funky #### with that number..."
But I do know that you have significant issues with your program structure, especially within your lengthy run() method where you have numerous nested while (...) loops and do-while loops, code constructs that might seem at home within a linear processing console program but which seems out of place within an event-driven Swing GUI.
What I suggest that you do is try to use some state-dependent coding. For instance, you could give your class the boolean variables, enteringData and dataValidYet, to represent two key states: whether the user is now entering data into the JTextField, and whether that data has yet been validated yet. And then within your JButton ActionListeners, use if and if/else blocks to decide what to do on button push depending on the state of these boolean fields, and likely other key fields of the class.
For a code "skeleton" example, one that doesn't yet do anything, but hopefully will show you the structure I'm talking about:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
#SuppressWarnings("serial")
public class StateMachine extends JPanel {
private static final String INITIAL_TITLE = "Please enter your next 8 bits. "
+ "Do not enter more than 8 bits.\n"
+ "Press Enter when done";
private static final String ARE_YOU_FINISHED = "Are you finished entering streams?";
private static final String YES = "Yes";
private static final String ENTER = "Enter";
private static final String NO = "No";
private static int GAP = 8;
private static final int COLUMNS = 30;
// this is a JTextArea built to look like a JLabel
private JTextArea topTextArea = new JTextArea(2, COLUMNS);
private JTextField dataEntryField = new JTextField(COLUMNS);
private JButton yesEnterButton = new JButton(ENTER);
private JButton noButton = new JButton(NO);
private boolean enteringData = true;
private boolean dataValidYet = false;
public StateMachine() {
yesEnterButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
yesEnterButtonActionPerfromed(e);
}
});
noButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
noButtonActionPerfromed(e);
}
});
topTextArea.setWrapStyleWord(true);
topTextArea.setLineWrap(true);
topTextArea.setFocusable(false);
topTextArea.setEditable(false);
topTextArea.setOpaque(false);
topTextArea.setText(INITIAL_TITLE);
JPanel innerButtonPanel = new JPanel(new GridLayout(1, 0, GAP, 0));
innerButtonPanel.add(yesEnterButton);
innerButtonPanel.add(noButton);
JPanel outerButtonPanel = new JPanel();
outerButtonPanel.add(innerButtonPanel);
setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
setLayout(new BorderLayout(GAP, GAP));
add(topTextArea, BorderLayout.PAGE_START);
add(dataEntryField, BorderLayout.CENTER);
add(outerButtonPanel, BorderLayout.PAGE_END);
}
protected void noButtonActionPerfromed(ActionEvent e) {
// TODO depending on state of enteringData and dataValidYet booleans
// change text in buttons, do things with JTextField data
// set state of enteringData and dataValidYet booleans
if (enteringData) {
// a no press is meaningless if entering data
return;
}
// .... more
}
private void yesEnterButtonActionPerfromed(ActionEvent e) {
// TODO depending on state of enteringData and dataValidYet booleans
// change text in buttons, do things with JTextField data
// set state of enteringData and dataValidYet booleans
if (enteringData) {
topTextArea.setText(ARE_YOU_FINISHED);
yesEnterButton.setText(YES);
yesEnterButton.setActionCommand(YES);
enteringData = false;
return;
}
// .... more
}
private static void createAndShowGui() {
StateMachine mainPanel = new StateMachine();
JFrame frame = new JFrame("State Machine");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
Also, as a "side" recommendation, one unrelated to your main problem, understand that null layouts and setBounds() might seem to Swing newbies like the easiest and best way to create complex GUI's, the more Swing GUI'S you create the more serious difficulties you will run into when using them. They won't resize your components when the GUI resizes, they are a royal witch to enhance or maintain, they fail completely when placed in scrollpanes, they look gawd-awful when viewed on all platforms or screen resolutions that are different from the original one.
Note that if this were my program, I would use more indirection including creating separate classes to separate out the GUI portion of the program from the logic portion.
I was trying to make a small simulation game with individual "Person" class. I used a JFrame to make a window and have real time simulations with Runnable. However, when I created more instances of Person, the Runnable becomes really slow. The more instances I have, the slower it becomes. Is there a better way of doing this? Or should I try to use SQL database rather than a text file to store data? It is just part of the code, I will add more if needed. Thank you very much.
public class Main extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
private JTextField textField;
private JTextArea textArea;
// create a list of people, each include person.java class
private List<Person> people = new ArrayList<Person>();
// set the number of miliseconds in a day
// 86400 = 24 hr x 60 min x 60 s
private int lengthOfDay = 100;
private int timeCounter = 0;
private int timeCounterPause = lengthOfDay / 24;
private int timeOfDay = 0;
private int hourOfDay = 0;
private int lengthOfYear = lengthOfDay * 365;
private int dayOfWeek = 1; // day starts monday
private String dayOfWeekS = "";
// boolean for stop Runnable
private volatile boolean stopRequested = false;
// runnable "run" class
Runnable simulation = new Runnable() {
public void run() {
try {
while (!stopRequested) {
for (int i = 0; i < people.size(); i++) {
// determine time of day
timeOfDay = timeOfDay + timeCounterPause;
if (timeOfDay > lengthOfDay) {
timeOfDay = timeOfDay % lengthOfDay;
// assign int to string dayOfWeekS
assignDay();
}
// display day of week if set visible (focused)
if (people.get(i).getVisible() && hourOfDay < 1) {
textArea.append(dayOfWeekS + "\n");
}
hourOfDay = timeOfDay / (lengthOfDay / 24);
people.get(i).setAge((float) (people.get(i).getAgeInMs() / (float)lengthOfYear));
// show time if focused
if (people.get(i).getVisible()) {
// textArea.append(hourOfDay +"o'clock\n");
}
// go to school
if (people.get(i).getFinishedSchool() == false && dayOfWeek != 6 && dayOfWeek != 7) {
people.get(i).goToSchool(timeOfDay);
} else if (people.get(i).getFinishedSchool() == true) {
people.get(i).findWork();
}
// go to work if has work, and not on weekends
if (!people.get(i).getProfession().equals("none") && dayOfWeek != 6
&& dayOfWeek != 7)
people.get(i).goToWork(timeOfDay);
// check alive
people.get(i).checkAlive();
// sleep at night
people.get(i).sleep(timeOfDay);
// interval between each time status are checked
Thread.sleep(timeCounterPause);
// adjust ageInMs
people.get(i).setAgeInMs(people.get(i).getAgeInMs() + timeCounterPause);
// if alive is false, then remove the person
// remove person as the last thing to run
if (!people.get(i).getAlive()) {
people.remove(i);
}
}
timeCounter++;
}
} catch (InterruptedException iex) {
}
}
};
// method to stop Runnable
public void requestStop() {
stopRequested = true;
}
public Main() {
super("Simulation");
textField = new JTextField(20);
textField.setText("Enter your commands here.");
textField.selectAll();
textField.setForeground(new Color(0, 204, 0));
textField.setBackground(Color.BLACK);
textField.addActionListener(this);
textArea = new JTextArea(40, 40);
textArea.setEditable(false);
textArea.setForeground(new Color(0, 204, 0));
textArea.setBackground(Color.BLACK);
JScrollPane scrollPane = new JScrollPane(textArea,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
GridBagLayout gridBag = new GridBagLayout();
Container contentPane = getContentPane();
contentPane.setLayout(gridBag);
GridBagConstraints c = new GridBagConstraints();
c.gridwidth = GridBagConstraints.REMAINDER;
c.fill = GridBagConstraints.HORIZONTAL;
gridBag.setConstraints(textField, c);
contentPane.add(textField);
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.weighty = 1.0;
gridBag.setConstraints(scrollPane, c);
contentPane.add(scrollPane);
// things to do when window opens
addWindowListener(new WindowAdapter() {
public void windowOpened(WindowEvent e) {
loadSimulation();
// load all data of each person
loadPeople();
// create thread when window is loaded
Thread thr = new Thread(simulation);
thr.start();
}
});
// things to do when window closes
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
savePeople();
saveSimulation();
}
});
}
public void actionPerformed(ActionEvent event) {
String prompt = textField.getText();
String[] words = prompt.split("\\s+");
// respond to commands
switch (words[0]) {
case "create":
if (words[1].equals("person")) {
createPerson();
} else {
textArea.append("Create what?\n");
}
break;
case "focus":
focus(words[1], words[2]);
break;
case "unfocus":
unfocus();
break;
case "topCash":
topCash();
break;
case "topAge":
topAge();
break;
case "topIQ":
topIQ();
break;
default:
textArea.append("I don't know what you are saying.");
break;
}
textField.selectAll();
}
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
Main window = new Main();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.pack();
window.setVisible(true);
window.getContentPane().setBackground(Color.BLACK);
}
You do a
Thread.sleep(timeCounterPause)
for every Person you check, so for every Person you add, that loop takes 4.166 (lengthOfDay / 24) ms longer.
If you want to make timeCounterPause the update interval for a complete status check, you should put it outside the
for (int i = 0; i < people.size(); i++) {
loop.
I'm making a game client/server and I'm having a new thread update some info on the client GUI (which is using Swing). I'm trying to use SwingUtilities but it's not working. Also, I heard SwingUtilities creates a new thread everytime you use it so I'm looking for a new way as well (I have 10 or so JTextFields to be updated). Is there a way to do it without using SwingUtilities? Here is what I have right now.
SwingUtilities.invokeLater(
new Runnable() {
public void run()
{
Client.status.setText("status = "+status);
}
});
Interestingly, just recently I encountered a similar issue, so In order to get over this I used SwingUtilities.invokeAndWait(Runnable runnable), that's what made the SSCCE I was creating to work as expected, though if I change all calls to invokeAndWait() with invokeLater(), one can clearly see the difference between the two things.
A quote from Java Doc says :
Causes doRun.run() to be executed synchronously on the AWT event dispatching thread.
This call blocks until all pending AWT events have been processed and
(then) doRun.run() returns. This method should be used when an application thread
needs to update the GUI.
This is a small program I had made as an SSCCE to represent a Bubble Sort Animation :
import javax.swing.*;
public class BubbleSortFrame extends JFrame
{
private BubbleSortView contentPane;
private void displayGUI()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = new BubbleSortView();
setContentPane(contentPane);
pack();
setLocationByPlatform(true);
setVisible(true);
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
#Override
public void run()
{
new BubbleSortFrame().displayGUI();
}
});
}
}
import java.awt.*;
import java.awt.event.*;
import java.lang.reflect.InvocationTargetException;
import javax.swing.*;
public class BubbleSortView extends JPanel
{
private JLabel sizeLabel;
private JTextField sizeField;
private JTextField[] vField;
private JLabel[] vLabel;
private JButton startButton, createButton;
private int size;
private JPanel createPanel, animationPanel;
private BubbleSort bubbleSort;
public BubbleSortView()
{
size = 5;
displayAndCreateGUI();
}
private void displayAndCreateGUI()
{
setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
setOpaque(true);
setBackground(Color.WHITE);
JPanel basePanel = new JPanel();
basePanel.setLayout(new GridLayout(2, 1, 5, 5));
basePanel.setOpaque(true);
basePanel.setBackground(Color.WHITE);
JPanel topPanel = new JPanel();
topPanel.setOpaque(true);
topPanel.setBackground(Color.WHITE);
topPanel.setBorder(
BorderFactory.createTitledBorder("Input : "));
topPanel.setLayout(new GridLayout(2, 1, 5, 5));
/*
* This will act as the area
* for taking the input for
* number of elements in an Array.
*/
JPanel sizePanel = new JPanel();
sizePanel.setOpaque(true);
sizePanel.setBackground(Color.WHITE);
sizeLabel = new JLabel("Enter Number of Elements : ");
sizeField = new JTextField(10);
createButton = new JButton("CREATE");
/*
* This will act as the area
* where we will specify the values
* for each index in an Array.
*/
createPanel = new JPanel();
createPanel.setOpaque(true);
createPanel.setBackground(Color.WHITE);
createPanel.setBorder(
BorderFactory.createTitledBorder("Please Enter values for an Array : "));
createPanel.setVisible(false);
animationPanel = new JPanel();
animationPanel.setOpaque(true);
animationPanel.setBackground(Color.WHITE);
animationPanel.setBorder(
BorderFactory.createTitledBorder("Animation : "));
animationPanel.setVisible(false);
createButton.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent ae)
{
if (sizeField.getDocument().getLength() > 0)
{
size = Integer.parseInt(sizeField.getText());
vField = new JTextField[size];
createPanel.setVisible(true);
for (int i = 0; i < size; i++)
{
vField[i] = new JTextField(5);
/*
* Adding the Listener to the
* last JTextField on the Right
* Side.
*/
if (i == (size - 1))
{
vField[i].addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent ae)
{
animationPanel.setLayout(
new GridLayout(1, size, 2, 2));
animationPanel.setVisible(true);
vLabel = new JLabel[size];
for (int i = 0; i < size; i++)
{
vLabel[i] = new JLabel(
vField[i].getText(), JLabel.CENTER);
vLabel[i].setOpaque(true);
vLabel[i].setBackground(Color.YELLOW);
vLabel[i].setForeground(Color.RED);
animationPanel.add(vLabel[i]);
}
animationPanel.revalidate();
animationPanel.repaint();
bubbleSort = new BubbleSort(vLabel, size);
Thread t = new Thread(bubbleSort);
t.start();
}
});
}
createPanel.add(vField[i]);
}
createPanel.revalidate();
createPanel.repaint();
createButton.setEnabled(false);
}
else
size = 5;
}
});
sizePanel.add(sizeLabel);
sizePanel.add(sizeField);
sizePanel.add(createButton);
/*
* Initializing JTextField Array
* so that it can be first presented
* to the USER to take input for
* 5 values.
*/
//for (int i = 0; i < size; i++)
// vField[i] = new JTextField(5);
topPanel.add(sizePanel);
topPanel.add(createPanel);
basePanel.add(topPanel);
basePanel.add(animationPanel);
add(basePanel);
}
private class BubbleSort implements Runnable
{
private int[] arr;
private JLabel[] vLabel;
private int size;
private int pass;
public BubbleSort(JLabel[] label, int size)
{
vLabel = label;
this.size = size;
pass = 1;
for (int i = 0; i < size; i++)
System.out.print("" + vLabel[i].getText() + "\t");
System.out.println("");
}
#Override
public void run()
{
try
{
bubbleSorting();
}
catch (InvocationTargetException ite)
{
ite.printStackTrace();
}
catch(InterruptedException ie)
{
ie.printStackTrace();
}
}
private void bubbleSorting()
throws InterruptedException, InvocationTargetException
{
while (pass < size)
{
for (int i = 0; i < (size - pass); i++)
{
final int j = i;
SwingUtilities.invokeAndWait(new Runnable()
{
#Override
public void run()
{
vLabel[j].setBackground(Color.RED);
vLabel[j].setForeground(Color.WHITE);
vLabel[j + 1].setBackground(Color.RED);
vLabel[j + 1].setForeground(Color.WHITE);
}
});
try
{
Thread.sleep(1500);
}
catch(InterruptedException ie)
{
ie.printStackTrace();
}
int left = Integer.parseInt(vLabel[i].getText());
int right = Integer.parseInt(vLabel[i + 1].getText());
if (left > right)
{
String temp = vLabel[i].getText();
vLabel[i].setText(vLabel[i + 1].getText());
vLabel[i + 1].setText(temp);
}
SwingUtilities.invokeAndWait(new Runnable()
{
#Override
public void run()
{
vLabel[j].setBackground(Color.YELLOW);
vLabel[j].setForeground(Color.RED);
vLabel[j + 1].setBackground(Color.YELLOW);
vLabel[j + 1].setForeground(Color.RED);
}
});
}
System.out.println("Pass : " + pass + "\tSize : " + size);
SwingUtilities.invokeAndWait(new Runnable()
{
#Override
public void run()
{
vLabel[size - pass].setBackground(Color.GREEN);
vLabel[size - pass].setForeground(Color.BLUE);
}
});
pass++;
}
SwingUtilities.invokeAndWait(new Runnable()
{
#Override
public void run()
{
vLabel[0].setBackground(Color.GREEN);
vLabel[0].setForeground(Color.BLUE);
}
});
}
}
}
I don't know where you heard about "SwingUtilities creates a new thread" from, but I think you've either misunderstood or being informed incorrectly. SwingUtilities.invokeLater places the Runnable onto the end of the Event Dispatcher's queue. The queue then processes this event within the it's own thread context (in time), calling Run, there is no "new" thread created for this process.
As to your question.
You may need to call validate() (and possibly repaint()) on the fields parent container to encourage it update ;)
And no, there isn't any other way to sync the UI across threads, that's the reason for SwingUtilities