JFrame Minimize/Maximize Refresh Issues
I noticed that when my program's GUI (using JFrame) window is minimized and then maximized, the frame contents get erased. Only the buttons reappear as the mouse moves over them.
I tried adding a WindowListener to JFrame and extending an internal class from WindowAdapter containing the following action method:
Code :
public class ListenForMaximize extends WindowAdapter {
public void windowActivated(WindowEvent e) {
mainFrame.invalidate();
mainFrame.validate();
mainFrame.repaint();
System.out.println("Received event");
}
}
The "Received Event" println is simply for testing purposes. It prints both when the program is opened and when it is maximized from the taskbar. But, the frame still remains erased. What else do I need to add to the method? Is validating and repainting not the right course of action?
Thanks in advance
Re: JFrame Minimize/Maximize Refresh Issues
Can you make a small simple program that compiles and executes to demonstrate the problem?
Re: JFrame Minimize/Maximize Refresh Issues
Quote:
Originally Posted by
Norm
Can you make a small simple program that compiles and executes to demonstrate the problem?
I was unable to recreate the issue....
Here is the rest of the code for the GUI class I am having the problem with:
Code :
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.util.*;
import java.io.*;
import javax.imageio.ImageReader;
import java.awt.event.*;
public class CalcGUI {
OpeningGraphics open = new OpeningGraphics();
JFrame mainFrame;
JPanel south;
JPanel north;
JPanel east;
JPanel west;
JPanel center;
JTextField inputs;
JTextField output;
ArrayList<JButton> mainButtonList;
ArrayList<JButton> operators;
JButton algebra;
JButton standard;
JButton exit;
public void setUpGUI(String name) {
mainFrame = new JFrame(name);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Image iScreen = open.readFile();
JLabel imageHolder = new JLabel(new ImageIcon(iScreen));
center = new JPanel();
center.add(imageHolder);
mainFrame.getContentPane().add(center);
mainFrame.setBounds(30,30,800,500);
mainFrame.setVisible(true);
mainFrame.addWindowListener(new ListenForMaximize());
try {
Thread.sleep(1500);
} catch (Exception e) {e.printStackTrace();}
center.remove(imageHolder);
south = new JPanel();
south.setBackground(Color.darkGray);
inputs = new JTextField(40);
south.add(inputs);
mainFrame.getContentPane().add(BorderLayout.SOUTH, south);
output = new JTextField(60);
output.setEditable(false);
north = new JPanel();
north.add(output);
mainFrame.getContentPane().add(BorderLayout.NORTH, north);
west = new JPanel();
west.setLayout(new BoxLayout(west, BoxLayout.Y_AXIS));
algebra = new JButton("Algebra");
standard = new JButton("Standard");
exit = new JButton("Exit");
west.add(standard);
west.add(algebra);
west.add(exit);
mainFrame.getContentPane().add(BorderLayout.WEST, west);
GridLayout grid = new GridLayout(4,3);
grid.setVgap(1);
grid.setHgap(2);
center = new JPanel(grid);
mainButtonList = new ArrayList<JButton>();
for(int i=0;i<12;i++) {
JButton b = new JButton("#");
mainButtonList.add(b);
}
for(int i=0;i<12;i++) {
JButton b = mainButtonList.get(i);
if(i == 0) {
b.setText("7");
} else if(i == 1) {
b.setText("8");
} else if(i == 2) {
b.setText("9");
} else if(i == 3) {
b.setText("4");
} else if(i == 4) {
b.setText("5");
} else if(i == 5) {
b.setText("6");
} else if(i == 6) {
b.setText("1");
} else if(i == 7) {
b.setText("2");
} else if(i == 8) {
b.setText("3");
} else if(i == 9) {
b.setText("0");
} else if(i == 10) {
b.setText(".");
} else if(i == 11) {
b.setText("-");
}
mainButtonList.set(i,b);
center.add(b);
b = null;
}
mainFrame.getContentPane().add(BorderLayout.CENTER, center);
mainFrame.validate();
}
public static void main(String[] args) {
CalcGUI gui = new CalcGUI();
gui.setUpGUI("JavaCalc");
}
public class ListenForMaximize extends WindowAdapter {
public void windowActivated(WindowEvent e) {
mainFrame.invalidate();
mainFrame.validate();
mainFrame.repaint();
System.out.println("Received event");
}
}
}
Here is the demo program though in case you were interested. The issue does not appear to occur with this one.
Code :
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Demo {
JFrame frame;
JTextField northText;
//Just creating the GUI window
public void setUp() {
frame = new JFrame("Issue Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel southPanel = new JPanel();
JPanel northPanel = new JPanel();
JButton southButton = new JButton("Hi Norm! Click me!");
southButton.addActionListener(new ButtonClick());
northText = new JTextField(40);
northText.setEditable(false);
southPanel.add(southButton);
northPanel.add(northText);
frame.getContentPane().add(BorderLayout.SOUTH, southPanel);
frame.add(BorderLayout.NORTH, northPanel);
frame.pack();
frame.setVisible(true);
}
//Creating the WindowEvent Listener internal class
class ListenForMaximize extends WindowAdapter {
public void windowActivated(WindowEvent e) {
//Attempting to tell the program to reload the GUI if the window gets minimized and then maximized.
//I have tried windowOpened windowDeiconified and windowGainedFocus as well.
frame.invalidate();
frame.validate();
frame.repaint();
//This just shows that the program is successfully receiving the action but isn't reloading the screen.
System.out.println("Received action");
}
}
class ButtonClick implements ActionListener {
public void actionPerformed(ActionEvent e) {
northText.setText("Try minimizing the window");
}
}
public static void main(String[] args) {
Demo d = new Demo();
d.setUp();
}
}
Re: JFrame Minimize/Maximize Refresh Issues
Quote:
The issue does not appear to occur with this one.
Without code to work with, I don't see how to do anything.
Ok, just noticed the other code and have been able to reproduce your problem.
Look at the components that are added and removed from the content pane.
Are they all being removed? I think there is a conflict between some of the components.
Start with commenting out the first part with the sleep.
Re: JFrame Minimize/Maximize Refresh Issues
Got it. The JPanel center got instantiated twice so the original JPanel was overlapping the grid.
Thanks for your help!
Re: JFrame Minimize/Maximize Refresh Issues
No, reusing a reference variable should be allowed.
Its the not removing the component that is the problem.
1 Attachment(s)
Re: JFrame Minimize/Maximize Refresh Issues
im having a similar issue.. each time i run the program it starts blank. i must minimize and maximize to display all the data in the jframe. here is an image:
On the left is on start up and on the right is on resize.
Attachment 599
and here is my code:
Code :
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class frmClient implements ActionListener{
public static JFrame[] arrFrame = new JFrame[1];
// Define JPanels for layout
public static JPanel[] panel = new JPanel[16];
// Define labels
public static JLabel lblStatus;
// Define JTextFields
public static JFormattedTextField[] txtBox = new JFormattedTextField[7];
// Define JComboBox's
public static JComboBox comBoxA;
// Define integers
public static int[] arrNumber = new int[10];
// Define doubles
public static double[] arrDouble = new double[17];
// Define JButtons
public static JButton btnSave, btnClear, btnClose, btnCalculate;
// Define boolean
public static boolean ctrlLock;
// Define custom RGB colors for table(s)
static Color colTableA = new Color(194, 250, 203);
static Color colTableB = new Color(242, 235, 150);
// Main method
public static void main(String[] args) {
new frmClient();
}
public frmClient(){
// Create the main JFrame that will hold all the panels & Add data Frame
arrFrame[0] = new JFrame("Company Name");
arrFrame[0].setVisible(true);
arrFrame[0].setResizable(true);
arrFrame[0].setBounds(450, 300, 800, 570);
// Create panel to hold all of the panels
panel[0] = new JPanel();
panel[0].setBackground(Color.WHITE);
panel[0].setForeground(Color.GREEN);
// Extracted methods
panelSetup();
}
private void panelSetup() {
// Create a new anwser array
String[] arrComboBox = new String [2];
arrComboBox[0] = "Disabled";
arrComboBox[1] = "Enabled";
// Create comboboxA and add arrComboBox data into selector
comBoxA = new JComboBox(arrComboBox);
comBoxA.setBackground(Color.white);
comBoxA.setForeground(Color.MAGENTA);
comBoxA.addActionListener(this);
// Create panelLogo for the company logo
panel[1] = new JPanel();
panel[1].add(new JLabel(" Factory Emissions "));
// Create panel1 to hold label(s) at top
panel[2] = new JPanel();
panel[2].setBackground(Color.WHITE);
panel[2].add(new JLabel("Time "));
panel[2].add(new JLabel("Monday "));
panel[2].add(new JLabel("Tuesday "));
panel[2].add(new JLabel("Wednesday "));
panel[2].add(new JLabel("Thursday "));
panel[2].add(new JLabel("Friday "));
panel[2].add(new JLabel("Weekly Average"));
// Create panel2 to hold first line of JTextField(s)
panel[3] = new JPanel();
panel[3].setBackground(Color.WHITE);
for(int a = 3; a < 13; a++){
if(a == 12){
// Create panel11 to hold the line seperator(s)
panel[a] = new JPanel();
panel[a].setBackground(Color.WHITE);
panel[a].add(new JLabel("___________________________________________________" +
"_______________________________________________________"));
} // End IF
else {
panel[a] = new JPanel();
panel[a].setBackground(Color.WHITE);
for(int i = 0; i < 7; i++){
txtBox[i] = new JFormattedTextField("0");
txtBox[i].setHorizontalAlignment(JTextField.RIGHT);
txtBox[i].setColumns(9);
arrNumber[1] = 9;
if(i == 0){txtBox[i].setText(Integer.toString(arrNumber[1])); arrNumber[1]++;}
if(i == 0){txtBox[0].setForeground(Color.RED); txtBox[0].setEditable(false);}
if(i == 6){txtBox[6].setBackground(colTableA); txtBox[6].setEditable(false);}
panel[a].add(txtBox[i]);
}
} // End ELSE
}
// Last 2 rows
for(int a = 13; a < 16; a++){
panel[a] = new JPanel();
panel[a].setBackground(Color.WHITE);
for(int i = 0; i < 7; i++){
if(i == 6){txtBox[5].setBackground(colTableA);}
else{
arrNumber[0] = 0;
if(i == 0 && arrNumber[0] == 0){panel[a].add(new JLabel("Total: "));}
txtBox[i] = new JFormattedTextField("0");
txtBox[i].setHorizontalAlignment(JTextField.RIGHT);
txtBox[i].setColumns(9);
txtBox[i].setBackground(colTableB);
txtBox[i].setEditable(false);
panel[a].add(txtBox[i]);
}
}
}
//txtBox[6].setEditable(false);
//Create panel14 for the buttons & Button settings
panel[15] = new JPanel();
panel[15].setBackground(Color.WHITE);
panel[15].add(btnClear = new JButton("Clear this weeks data"));
panel[15].add(btnCalculate = new JButton("Calculate"));
panel[15].add(new JLabel(" "));
panel[15].add(new JLabel("Lock Controls: "));
panel[15].add(comBoxA);
panel[15].add(new JLabel(" "));
panel[15].add(btnSave = new JButton("Save Data"));
panel[15].add(btnClose = new JButton("Close"));
// Add all the panels to the main panel
arrFrame[0].add(panel[0]);
panel[0].add(panel[1]);
panel[0].add(panel[2]);
panel[0].add(panel[3]);
panel[0].add(panel[4]);
panel[0].add(panel[5]);
panel[0].add(panel[6]);
panel[0].add(panel[7]);
panel[0].add(panel[8]);
panel[0].add(panel[9]);
panel[0].add(panel[10]);
panel[0].add(panel[11]);
panel[0].add(panel[12]);
panel[0].add(panel[13]);
panel[0].add(panel[14]);
panel[0].add(panel[15]);
// Set boolean state
ctrlLock = false;
// Add Button ActionListener(s)
btnClear.addActionListener(this);
btnSave.addActionListener(this);
btnClose.addActionListener(this);
btnCalculate.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
// Execute the code if btnCalculate has focus and lock controls is off
if(btnCalculate.hasFocus()){
if(ctrlLock == false){
}
else if(ctrlLock == true){
JOptionPane.showMessageDialog(arrFrame[0], "Control lock is currently enabled");
}
}
// Execute the code if btnClose has focus
if(btnClose.hasFocus()) {
if(ctrlLock == false){
System.exit(0);
}
else if (ctrlLock == true){
JOptionPane.showMessageDialog(arrFrame[0], "Control lock is currently enabled");
}
}
if(comBoxA.getSelectedIndex()== 0){
// Set boolean state
ctrlLock = false;
}
else if(comBoxA.getSelectedIndex()== 1){
ctrlLock = true;
}
// Execute the code if btnSave is clicked
if(e.getSource() == btnSave){
if(ctrlLock == false){
}
else if(ctrlLock == true){
JOptionPane.showMessageDialog(arrFrame[0], "Control lock is currently enabled");
}
}
// Execute the code if btnClear is clicked
if(e.getSource() == btnClear){
if(ctrlLock == false){
}
else if(ctrlLock == true){
JOptionPane.showMessageDialog(arrFrame[0], "Control lock is currently enabled");
}
}
}
//public void week5PM(){
//intCalculate = intMonI + intTueI + intWedI + intThuI + intFriI;
//dblWkI = intCalculate / 5;
//txtWAI.setText(Double.toString(dblWkI));
//}
//public void calcAverage(){
//dblCal = dblWkA + dblWkB + dblWkC + dblWkD + dblWkE + dblWkF + dblWkG
//+ dblWkH + dblWkI;
//dblWA = dblCal;
//txtWAJ.setText(Double.toString(dblWA));
//dblWB = dblCal / 9;
//txtWAK.setText(Double.toString(dblWB));
//}
}
Re: JFrame Minimize/Maximize Refresh Issues
Move the call to setVisible to after you have built the GUI.
Also add code to exit the JVM when the frame is closed.
Re: JFrame Minimize/Maximize Refresh Issues
Quote:
Originally Posted by
Norm
No, reusing a reference variable should be allowed.
Its the not removing the component that is the problem.
Well what I'm saying is that there were two centers so when you maximized the window it showed the blank center JPanel from the opening image.
Re: JFrame Minimize/Maximize Refresh Issues
@Norm
While I have you though (to save another thread), is that button array going to work?
If I get the source using getSource, is it going to return the same thing for all of the buttons?
Re: JFrame Minimize/Maximize Refresh Issues
Whatever. There were two panels being shown in the same place. The blank one blocked the other one from view. The fact that the variable center had referred to both of them wasn't relevant. There could have been different variables.
The Event getSource() method should return a reference to the component that generated the event.
Print it out to see what its value is each call. Then compare the values.
Re: JFrame Minimize/Maximize Refresh Issues
Quote:
Originally Posted by
Norm
Move the call to setVisible to after you have built the GUI.
Also add code to exit the JVM when the frame is closed.
ah what a simple fix mate thanks lol