I would like to get a set variable of the object inst, then set it to a JLabel and set that to the current JLabel that's already on the panel. But I would like for the variable (inst.Time (which represents seconds)) of the object 'inst' to decrement by one and remove the current JLabel and add to the panel the updated, decremented inst.Time.
I would like to decrement by 1 second as countdown timer until it reaches 0 and come out of the method WaitAndEnterIntoWorkArea. Could someone please show how to do so with the given code? Help please. Thank you
int.Time is an integer
private int WaitAndEnterIntoWorkArea(Instruction inst) // 'inst' is an object of Instruction arrayList
{
int index = 0;
int nFirstYDirectionToMove = 1;
JLabel busy = new JLabel(String.valueOf(inst.Time) + "s"); //inst.Time is a time that was set for that particular 'inst' and set the busy JLabel to show time + s, e.g. 5s or 3s (s represents seconds)
busy.setFont(font3);
busy.setForeground(Color.RED);
if(inst.WorkArea.startsWith("anvil")) //If inst.WorkArea is set to "anvil" Go into the block of code
{
nFirstYDirectionToMove = 1; //Move over one (Not important)
//I would like to decrement inst.Time 1 at a time to 0 and add to JPanel every second
while(true)
{
synchronized("row1" + String.valueOf(index)){
if(row1[index].getText().equalsIgnoreCase("Open")) //If JLabel row1[index].getText() is already set as text "Open" execute the if statement;
{
//I would like to remove the current JLabel (row1[index]) and add the 1 second decremented JLabel to the panel
panel.remove(row1[index]); //Remove the JLabel of row1[indexOfArea] (row1 is an array of JLabels) from the panel
row1[index] = busy; //Set JLabel text of row1[index] to the busy JLabel //Set the 1 second decremented JLabel to the current JLabel
panel.add(row1[index]); //Add the JLabel with the new text label with something like e.g. 5s or 4s, etc. to the panel
revalidate();
repaint();
break;
}
}
}
}
If I'm not mistaken, simply calling busy.setText(String.valueOf(inst.Time) + "s"); should be all you need. But as always, I would highly recommend using JavaFX instead of Swing.
As for the timing try
Timer timer = new Timer();
timer.scheduleAtFixedRate(()-> busy.setText(...), 0, 1000);
Related
I am creating a GUI in java using gridLayout 6 x elevatorNum. My purpose is to create the number of columns based on the user input "elevatorNum" so that it can automatically be expanded. My problem now is that I want to populate each column with the same 6 JLabel fields and then be able to use these fields to change their values whenever I want to.
I am not able to do this since I will have to create a new set of 6 JLabels for each column but for that I would need to either hardcode each column or automatically expand them using user input. I want to automatically expand them using userInput but I don't know how to name each set of 6 JLabels without hardcoding them.
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setLayout(new GridLayout(7,elevatorNum));
JLabel totalFloors = new JLabel("Total floors: " + floorNum);
JLabel currentFloor = new JLabel("CurrentFloor : N/A");
JLabel destFloor = new JLabel("Destination Floor: N/A");
JLabel doorStatus = new JLabel("Doors Status: N/A");
JLabel motorStatus = new JLabel("Motor Status: N/A");
JLabel passengerStatus = new JLabel("Passenger Status: N/A");
JLabel elevatorStatus = new JLabel("Elevator Direction: N/A");
frame.add(totalFloors);
frame.add(currentFloor);
frame.add(destFloor);
frame.add(doorStatus);
frame.add(motorStatus);
frame.add(passengerStatus);
frame.add(elevatorStatus);
Now i want to create another 6 set of JLabels but I will have to use different name but I don't know the userinput so I cannot hardcode the 6 JLabels. I also want to use each set of JLabels later on to update/edit their values.
I would greatly appreciate your help.
I had a similar experience of populating the layout with buttons and need to change their values anytime I want. I think you don't need to have user input to create labels, try simply create JLabels in a for loop, and track them by adding each of them to a List. you could find exactly that label through its index in array.
for example, create a list to track them:
List<JLabel> labelList = new ArrayList<>();
then create a method to create JLabels:
private JLabel createLabel() {
JLabel newLabel = new JLabel("give_it_an_initial_name");
// maybe do something on each label
return newButton;
}
finally add them to frame:
for(int i = 0; i < 6 * elevatorNum; i++) {
JLabel addLabel = createLabel();
frame.add(addLabel);
labelList.add(addLabel);
}
when you want to change something on label, fetch it from list and edit, no need to give each of them a name.It's a very simple way, might not be very good, but it works.
When I try to run this, I keep getting an empty frame that cannot be closed, and I have to terminate the program from eclipse. I am fairly certain that the problem is a result of the while loop (about 2/3 of the way down the code block), but I haven't been able to figure out a way to fix this
public Draw()
{
super( "Uno");
JMenuBar bar = new JMenuBar(); // create menu bar
JMenu gameMenu = new JMenu( "Play Game" );
JMenu quitMenu = new JMenu( "Quit" ); //Quit Program
bar.add( gameMenu ); // add Add menu to menu bar
bar.add( quitMenu ); // add quit button to menu bar
setJMenuBar( bar ); // set menu bar for this application
window = new JDesktopPane();
add(window);
gameMenu.addMouseListener(
new MouseListener()
{
public void mouseClicked(MouseEvent e)
{
Dimension size = window.getSize();
double height = size.getHeight();
double width = size.getWidth();
JInternalFrame frame = new JInternalFrame("Uno", true, true, true, true );
JFrame input = new JFrame();
input.setTitle(" ");
input.setSize(350, 155);
input.setLayout(new FlowLayout());
input.setLocationRelativeTo(null);
input.setVisible(true);
JLabel label1 = new JLabel("How many total players? (2-10)");
final JTextField text1 = new JTextField(3);
JLabel label2 = new JLabel("How many computer players? *");
final JTextField text2 = new JTextField(3);
JLabel label3 = new JLabel("* Computer players must be <= total number of players");
JButton confirm = new JButton("OK");
label1.setVisible(true);
label2.setVisible(true);
label3.setVisible(true);
text1.setVisible(true);
text2.setVisible(true);
confirm.setVisible(true);
input.add(label1);
input.add(text1);
input.add(label2);
input.add(text2);
input.add(label3);
input.add(confirm);
while(valid == false)
{
confirm.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String players = text1.getText();
String computers = text2.getText();
int temp1 = Integer.parseInt(players);
System.out.println("Number of players = " + temp1);
int temp2 = Integer.parseInt(computers);
System.out.println("Number of computers = " + temp2);
if (temp1 >= 2 && temp1 <= 10)
if (temp2 <= temp1 && temp2 >= 0)
valid = true;
} //close actionPerformed
}); //close ActionListener
} //close while loop
} //close mouseClicked
} //close mouseListener
} //close Draw
Initially, I was having an issue where it kept infinitely drawing the frame on top of itself because the while loop kept returning false. That issue was solved by removing the components from the while loop, and placing them before it as they are above. Now when I run it, all I get is this: picture
The goal of this program is to make an Uno game, but the first step is to determine the number of players. This is the only way I know how to do this, and this is only the second program I have done with graphics, so I am by no means an expert at Java. I will appreciate any help I can get because I have been stuck for a few days now.
The problem is that you are not giving the event handlers any chance to do their job. Your while loop continually creates event handlers. However, the actionPeformed() method can never be called because the while loop has control of the CPU and the button will not respond to any clicks.
I suggest that you learn a bit more about event handling. You will need to redesign your code in order to get the behavior you wish. Most likely you need to remove the while loop entirely and just create an event handler which will execute code when the button is clicked.
For future reference:
In general, checking equality with a boolean value is unnecessary. The result of == is a bool, but the variable you are checking is already a bool. So use it directly: instead of while (valid == false), you can do while (!valid). If you read the ! as "not", this actually makes a lot of sense.
In this case, the point is moot, since you need to remove the while loop entirely. However, this might be useful information for you some time in the future.
I'm trying to make randomized JTextField's yellow by using Arrays. I've only managed to make 1 textfield yellow, and then white after 1 sec at each click. What I want to do is when you press the button the first randomized Textfield get yellow and then white. And when you press the second time the first randomzied textfield AND the seconds will turn yellow and then White and so on.. The array works as you can see I Printing it and when you press the button it prints all the randomized numbers in order.
The problem is that JTextField can't handle int's, and it apparently have to be "Final" so it makes it very hard to make multiply JTextField's yellow. I pasted my arrays to you so that you can get a better understanding what I'm trying to do. Does anyone know the solution?
//my Arrays
JTextField[] boxes = new JTextField[9]; //Array for the textfields
int[] clicked = new int[100];
int clickAmount = 0;
//At startup it fills the boxes array with the textfield:
boxes[0] = textfield1;
boxes[1] = textfield2;
boxes[2] = textfield3;
boxes[3] = textfield4;
boxes[4] = textfield5;
boxes[5] = textfield6;
boxes[6] = textfield7;
boxes[7] = textfield8;
boxes[8] = textfield9;
public void timePaus (final JTextField textfield) {
new Timer(1000, new ActionListener() {
public void actionPerformed (ActionEvent e) {
textfield.setBackground(Color.white);
// stop the timer
((Timer) e.getSource()).stop();
}
}).start();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { //Button
int randomint = this.randomBox(); //Finds a number between 0-8
final JTextField ThatTextfield = boxes[randomint]; //Puts a textfield into an array
clicked[clickAmount] = randomint+1; //adds textfield number into an array
clickAmount++;
ThatTextfield.setBackground(Color.yellow); //make choosen textfield yellow
for (int i = 0; i < clickAmount; i++)
{
timePaus(ThatTextfield); //make choosen textfield white after 1 sec
System.out.println(clicked[i]);
}
}
Considerations:
Place your JTextFields in an ArrayList<JTextField>, but create them in a for loop for the sake of avoiding unnecessary repetation.
When you want to randomize the ArrayList, call Collections.shuffle(myTextFieldList) on your List of JTextFields.
Give your class an int maxIndex field.
In your Swing timer, iterate through the now randomized List of JTextFields, displaying each one up to the value held by the maxIndex and not above the size of the List.
Increment the maxIndex in the timer.
Alternatively, you could create two more ArrayList<JTextField>, one to shuffle the collected text fields. Then remove the 0th JTextField from this list in your button's listener, and place it into the other ArrayList, and then iterate through the 2nd ArrayList's JTextfield in your Timer.
I have a class that creates a JFrame and needs to have one image displayed in the JFrame. Images must rotate every few seconds.
I've tried using repaint(), revalidate(), and validate() on both the JFrame and JPanel and none seem to work. Following is my current code to update the JFrame/JPanel. This code will wait 4 seconds and then display the fourth image, but I would like it to update every second with a new image.
public void startSlideshow(){
int i = 0;
Long oldTime = System.currentTimeMillis();
do {
Long currentTime = System.currentTimeMillis();
if( currentTime - oldTime >= 1000 ) {
img = new JLabel(new ImageIcon("Albums/" + album + "/" + imageNames.get(i)));
panel.removeAll();
panel.setBackground(Color.BLACK);
panel.add(img);
panel.revalidate();
panel.repaint();
i++;
oldTime = System.currentTimeMillis(); // reset reference time
}
} while(i<imageNames.size());
panel.removeAll();
panel.setBackground(Color.BLACK);
panel.add(img);
panel.getRootPane().revalidate();
panel.repaint();
}
Any help is greatly appreciated as I've been banging my head for a while now. Thanks.
You're blocking the Event Dispatching Thread, preventing it from process new repaint requests.
Instead of using a loop and Thread.sleep, try using a javax.swing.Timer
Take a look at How to Use Swing Timers and Concurrency in Swing
You may also find it simpler to change the JLabel's icon instead of creating a new JLabel on each time through...
I'm currently programming a Calendar in Java. The Calendar itself fills out perfectly fine with the Month and Year being displayed in JComboBoxes above the grid below it. The Grid itself is full of empty JLabels or JButtons (for the days of the month). The JComboBoxes are linked to ActionListeners, which detect the user changing information (confirmed by System.out.print statements). However, I cannot find a way to "redraw" the JPanel after this occurs. The method being called completely creates a new JPanel and adds in the new JLabels/JButtons, but nothing on the JFrame is updated after this happens. I have attempted using both the repaint and revalidate on the whole JPanel (which includes the JComboBoxes and the grid below it in a BorderLayout), the JPanel with only the grid, and the JFrame, but nothing happens. Does anyone have any ideas what is wrong?
// This code is within the build() method that builds the GUI.
// This method adds the centerPanel into the mainPanel
yearChoice.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
int i = yearChoice.getSelectedIndex();
if (i >= 0) {
yy = Integer.parseInt(yearChoice.getSelectedItem()
.toString());
//System.out.println("Year=" + yy);
compute();
}
}
});
private void compute(){
elements = new int[6][7];
// OMITTED is the code that determines whether a cell in the array should be an empty
// JLabel or a JButton. This code is based on the Calendar, and does work properly,
// but I can't figure out how to get the new Month/Year to show up on a new Panel
centerPanel = new JPanel(new GridLayout(7, 7));
JLabel sunday = new JLabel("S"),
monday = new JLabel("M"),
tuesday = new JLabel("T"),
wednesday = new JLabel("W"),
thursday = new JLabel("Th"),
friday = new JLabel("F"),
saturday = new JLabel("S");
centerPanel.add(sunday);
centerPanel.add(monday);
centerPanel.add(tuesday);
centerPanel.add(wednesday);
centerPanel.add(thursday);
centerPanel.add(friday);
centerPanel.add(saturday);
for(int i = 0; i < 6; i++){
for(int j = 0; j < 7; j++){
if(elements[i][j] == -1){
centerPanel.add(new JLabel(" "));
}else{
centerPanel.add(new JButton("" + elements[i][j]));
}
}
}
// Here is where I attempt to repaint the centerPanel for the JPanel, but it
// doesn't work
}
The method being called completely creates a new JPanel and adds in the
new JLabels/JButtons, but nothing on the JFrame is updated after this
happens. I have attempted using both the repaint and revalidate on the
whole JPanel (which includes the JComboBoxes and the grid below it in
a BorderLayout), the JPanel with only the grid, and the JFrame, but
nothing happens. Does anyone have any ideas what is wrong?
you call another JPanel for (re)validate & repaint
updates are out of visible rectangle on the screen, put JPanel to the JScrollPane
if updates are out of visible rectangle you can to call JFrame.pack(), but in this case JFrame will be changed its Dimension on the screen
The method being called completely creates a new JPanel and adds in
the new JLabels/JButtons, but nothing on the JFrame is updated after
this happens.
for why reason you re_create a JPanel, create that once time and to play with setVisible(false/true)
don't re_create a JPanel, put these (two or three JPanels) to the CardLayout, then any changes will be only switch betweens views
for better help sooner post an SSCCE, short, runnable, compilable
code example