refresh JFrame after adding new Components
You have to revalidate();
the frame. If that doesn't work you also have to call repaint();
How to Reload a JFrame - Java
- You only need to use one frame (you are creating 255 of them)
- Don't use a while loop to try and change the background. Use a Swing Timer instead. See How to Use Swing Timers
- Run all your Swing apps on the Event Dispatch Thread. See Initial Threads
Here's your code refactored with those three points
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class Frame1 {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
final JFrame frame = new JFrame();
frame.getContentPane().setBackground(
new Color(0, 0, 0));
Timer timer = new Timer(10, new ActionListener() {
int num = 0;
public void actionPerformed(ActionEvent e) {
if (num > 255) {
((Timer) e.getSource()).stop();
} else {
frame.getContentPane().setBackground(
new Color(num, num, num));
num++;
}
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
timer.start();
}
});
}
}
How to reset or refresh Jframe with new values
For a more definitive answer, post an MCVE
Seeing as you haven't posted any code, I'm guessing you are using a JLabel
or a JList
or something of that sort to display the array. No matter which one you are doing, you need to tell the component to update it's content, it doesn't just do it itself. To do that, you need to call the components .setText()
or similar method.
If you have a JLabel
or JTextArea
it could look like this.
labelOrTextArea.setText("New Text");
If you are using a JList
you should update the lists Default List Model
like this
dlm.addElement("New Text");
UPDATE
I see a couple things wrong with your code. First off JFrame Frame = new JFrame
conventionally, variables should start with a lower case letter and they should not contain underscores '_'. You are also using AWT Components instead of Swing components. You should be using the likes of JTextArea
, JPanel
(Theres no JContainer
), JLabel
etc.
You are also never adding the panel to the frame.
frame.add(panel);
You should also not be adding stuff to the frame or panels after you set its visibility to true. So you should setup your frame like this
import javax.swing.*;
import java.awt.*;
import java.util.List;
public class Project1GUI
{
JTextArea unsorted_words, sorted_words, linked_words;
public Project1GUI()
{
JFrame frame = new JFrame("Title");
JPanel panel = new JPanel(new GridLayout(2, 1));
unsorted_words = new JTextArea();
sorted_words = new JTextArea();
linked_words = new JTextArea();
panel.add(unsorted_words);
panel.add(sorted_words);
panel.add(linked_words);
frame.add(panel);
frame.setSize(400,400);
frame.setLocation(200,200);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
You can then implement the methods you currently have and call them in an ActionListener or such.
Result:
On top of all of that, you should not rely on the use of static
as it takes away from the main points of OOP.
Related Topics
Hibernate Foreign Key Issue:Error Executing Ddl "Alter Table..."
How to Find Out If String Has Already Been Url Encoded
Android Get Height of Webview Content Once Rendered
Checking If a String Is Contained in an Enum Set in O(1)
Disable Spring Security Config Class for @Webmvctest in Spring Boot
Spring Data Repository @Query - Update and Return Modified Entity
How to Convert Xml to Java.Util.Map and Vice Versa
I Get a Status 200 When Connecting to the Websocket, But It Is an Error
Spring Boot Return 204 No-Content When List Is Empty
How to Mock System.Getproperty Using Mockito
How to Pass Variables Between Cucumber-Jvm Steps
How to Remove the Slashes from Json Key While Converting Object into String
How to Find Out My MySQL Url, Host, Port and Username
How to Sort Arraylist of Objects by Timestamp and Get Last Five Elements
Check If Two Objects Are Equal Excluding a Few Properties
How to Pass Authentication Credentials in Vba
@Notnull:Validation Custom Message Not Displaying
How to Externalize Application.Properties in Tomcat Webserver for Spring