How to Refresh or Reload the Jframe

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

  1. You only need to use one frame (you are creating 255 of them)
  2. Don't use a while loop to try and change the background. Use a Swing Timer instead. See How to Use Swing Timers
  3. 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:

Three TextAreas

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



Leave a reply



Submit