java gui delays output

java gui hangs / lags when called by another gui class

Page 1 of 1

6 Replies - 3093 Views - Last Post: 06 August 2009 - 09:54 AM Rate Topic: -----

#1 drummer_sniper  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 70
  • Joined: 05-August 09

java gui delays output

Posted 05 August 2009 - 06:49 PM

hi. im making a gui magic square. it has 2 classes, GuiMsV2(main window/where the user can input) and the GuiOutput(a window where the magic square will be shown. it only contains JTextFields )

my problem is that when i try to use the GuiOutput directly, it does what i want. it shows the total dimension, then displays 1, delays, then 2, delays, then 3, and etc. it's like i want the user to see the slow individual output of the program.

BUT.. when i use the GuiMsV2 class to input and call the GuiOutput class. it hangs / lags then displays everything as one. its like it wont display until every process is finished and i cant see the displaying of the nos. with delays.

here are the codes of the 2 classes:


GuiMsV2:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class GuiMsV2 extends JFrame implements ActionListener
{

	int dimensionInt;
	int row;
	int col;
	
	JPanel inputPanel=new JPanel();
		JLabel lbl_1=new JLabel("  Enter an odd dimension for the square: ");
		JLabel lbl_2=new JLabel("  Input the starting coordinates: ");
		JLabel lbl_space=new JLabel("");
		JLabel lbl_x=new JLabel("				   X = ");
		JLabel lbl_y=new JLabel("				   Y = ");

		JTextField txt_dimension=new JTextField(3);
		JTextField txt_x=new JTextField(3);
		JTextField txt_y=new JTextField(3);
		JTextField txt_space=new JTextField("");

	JPanel buttonsPanel=new JPanel();
		JButton btn_solve=new JButton("SOLVE");
		JButton btn_clear=new JButton("CLEAR");

	public GuiMsV2() {
		setTitle(" Magic Square");
		setSize(500,130);
		setLocation((1024-200)/2,(768-700)/2);

		inputPanel.setLayout(new GridLayout(4,2));
		buttonsPanel.setLayout(new GridLayout(1,2));

		inputPanel.add(lbl_1);		  inputPanel.add(txt_dimension);
		inputPanel.add(lbl_2);		  inputPanel.add(lbl_space);
		inputPanel.add(lbl_x);		  inputPanel.add(txt_x);
		inputPanel.add(lbl_y);		  inputPanel.add(txt_y);

		buttonsPanel.add(btn_solve);
		buttonsPanel.add(btn_clear);

		btn_solve.addActionListener(this);
		btn_clear.addActionListener(this);

		add(inputPanel,BorderLayout.NORTH);
		add(buttonsPanel,BorderLayout.CENTER);
		setResizable(false);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setVisible(true);
	}
	
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource()==btn_solve)
		{
			
			if(checking())
			{
				String dim=txt_dimension.getText();
				String x=txt_x.getText();
				String y=txt_y.getText();

				dimensionInt = Integer.parseInt(dim);
				row = Integer.parseInt(x);
				col = Integer.parseInt(y);
			
				GuiOutput gm = new GuiOutput(dimensionInt, row, col);
			}
			
		}
		if(e.getSource()==btn_clear)
		{
			txt_dimension.setText("");
			txt_x.setText("");
			txt_y.setText("");
		}

	}
	
	private boolean checking()
	{
		boolean valid = true;
		try
		{
			
			String dim=txt_dimension.getText();
			String x=txt_x.getText();
			String y=txt_y.getText();
						   
			int dimensionInt = Integer.parseInt(dim);
			int row = Integer.parseInt(x);
			int col = Integer.parseInt(y);
 
			if(dimensionInt%2==0)
			{
				JOptionPane.showMessageDialog(null,"Your dimension is not an odd number.\nPlease enter an odd number.","Error!",JOptionPane.ERROR_MESSAGE);
				valid = false;
			}
		
			else if(row >= dimensionInt || col >= dimensionInt)
			{
				String d=(dimensionInt-1) + "";
				JOptionPane.showMessageDialog(null,"Your coordinates cannot be accessed right now.\nPlease input properly from 0 to "+d,"Error!",JOptionPane.ERROR_MESSAGE);
				valid = false;
			}
					   
		}
		catch (Exception e)
		{
			JOptionPane.showMessageDialog(null,"Please input number only.","Error!",JOptionPane.ERROR_MESSAGE);txt_dimension.setText(null);
			valid = false;
		}
		return valid;
	}
	
	public static void main(String args[]) {
		GuiMsV2 s=new GuiMsV2();
		

	}
}





GuiOutput:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Color.*;


public class GuiOutput extends JFrame
{
	   JPanel magicSquarePanel;
	   JTextField txt_boxArray [] [];

	   
	   public GuiOutput (int dim, int row, int col)
	   {
		   setTitle(" Magic Square");
		   setSize(500,600);
		   setLocation((1024-200)/2,(768-700)/2);
		
		   txt_boxArray =new JTextField[dim] [dim];
		   magicSquarePanel=new JPanel();  
		   magicSquarePanel.setLayout(new GridLayout(dim,dim));
		
		   for(int h=0; h<dim;h++)
		   {
				for (int i=0;  i<dim;  i++)
				{
					magicSquarePanel.add(txt_boxArray[h][i]=new JTextField());
					txt_boxArray[h][i].setEditable(false);
					txt_boxArray[h][i].setHorizontalAlignment(JTextField.CENTER);
				}
			}
		
			add(magicSquarePanel,BorderLayout.CENTER);
			setResizable(false);
			setDefaultCloseOperation(EXIT_ON_CLOSE);
			setVisible(true);  
			solve(dim, row, col);
	   }
	   
	   private void solve(int dimensionInt, int row, int col)
	   {
	   
		String d=(dimensionInt-1) + ""; 
		int array [][]=new int[dimensionInt][dimensionInt];
		  
		array[row][col]=1;
		int prow = row;
		int pcol = col;	
		int maxDimension = dimensionInt * dimensionInt;
		
				Delay();
				
		txt_boxArray[row][col].setText("1");
		
		for(int i=2;i<=maxDimension;i++)
		{
				Delay();
				
			row--;
			col--;
			
			if(row < 0)
				row=dimensionInt-1;
			if(col < 0)
				col=dimensionInt-1;
				
			if(array[row][col]!=0)
			{
				row=prow;
				col=pcol;
				
				if(row+1 > dimensionInt-1)
					row=0;
				else
					row++;
			}
			
			array[row][col] = i;
			txt_boxArray[row][col].setBackground(Color.BLUE);
			Delay();
			txt_boxArray[row][col].setBackground(null);
			Delay();
				txt_boxArray[row][col].setText("" + i);
				
				
			prow = row;
			pcol = col;
				
		}
			
		
	}
	
	private void Delay()
	{
		try {
					Thread.sleep(500);
				}
				catch (InterruptedException e) {
					// ignore
				}
	}
	
	  
}




i think there's nothing wrong with the logic already but just the output.. thanks

Is This A Good Question/Topic? 0
  • +

Replies To: java gui delays output

#2 skaoth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 91
  • View blog
  • Posts: 601
  • Joined: 07-November 07

Re: java gui delays output

Posted 05 August 2009 - 08:58 PM

You simply need to have the guioutput class a chance to do both the solving and the drawing of the UI.
I was able to get your program working correctly by doing putting the guioutput class on a separate thread

new Thread(new Runnable() {

					public void run() {
						GuiOutput gm = new GuiOutput(dimensionInt, row, col);
			 
					}
				}).start();


Was This Post Helpful? 1
  • +
  • -

#3 drummer_sniper  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 70
  • Joined: 05-August 09

Re: java gui delays output

Posted 05 August 2009 - 11:19 PM

thanks for the code skaoth.

i have another problem. when i exit the GuiOutput window while its still running the window disappears but the process continues.

what i want is when i close the GuiOutput window i want the process to stop and the main window(GuiMsV2) wont close also.

thanks
Was This Post Helpful? 0
  • +
  • -

#4 skaoth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 91
  • View blog
  • Posts: 601
  • Joined: 07-November 07

Re: java gui delays output

Posted 05 August 2009 - 11:42 PM

One way to do this is to tell the solve() function to stop when the window is closing. This requires you to setup an event handler for the windowClosing event.

The first step is to put a handler in guioutput like this
public GuiOutput (int dim, int row, int col)
	   {
		  ...
			
			this.addWindowListener(new WindowAdapter() {
				@Override
				public void windowClosing(WindowEvent e) {
					bContineProcessing = false;
				}
			});
			
			solve(dim, row, col);
	   }



You'll also need to add a boolean variable at the top of the class
private boolean bContinueProcessing = false;



Lastly, we need to top the processing that is being done in solve(). That is done like this
private void solve(int dimensionInt, int row, int col)
	   {
		   ...
		   bContineProcessing = true;
		   for (int i = 2; i <= maxDimension && bContineProcessing; i++) {
			   Delay();

			...
		}



When the window is closed. The bContinueProcessing variable is set to false and stops the main loop.
Was This Post Helpful? 1
  • +
  • -

#5 drummer_sniper  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 70
  • Joined: 05-August 09

Re: java gui delays output

Posted 06 August 2009 - 05:18 AM

thanks again!

if it's not a problem,i have another question. how do i change the default icon(java) in the title bar in a gui window?
Was This Post Helpful? 0
  • +
  • -

#6 skaoth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 91
  • View blog
  • Posts: 601
  • Joined: 07-November 07

Re: java gui delays output

Posted 06 August 2009 - 07:29 AM

call the JFrame's setImageIcon() function
Was This Post Helpful? 0
  • +
  • -

#7 drummer_sniper  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 70
  • Joined: 05-August 09

Re: java gui delays output

Posted 06 August 2009 - 09:54 AM

i already placed this code in my GuiMsV2:

public GuiMsV2() {
		setTitle(" Magic Square");
		setSize(500,130);
		setLocation((1024-200)/2,(768-700)/2);
		
		ImageIcon ImageIcon = new ImageIcon("d:\\16x16.jpg");							 //these are the added code
		Image Image = ImageIcon.getImage();														//these are the added code
		this.setIconImage(Image);																		   //these are the added code



		inputPanel.setLayout(new GridLayout(4,2));
		buttonsPanel.setLayout(new GridLayout(1,2));

		inputPanel.add(lbl_1);		  inputPanel.add(txt_dimension);
		inputPanel.add(lbl_2);		  inputPanel.add(lbl_space);
		inputPanel.add(lbl_x);		  inputPanel.add(txt_x);
		inputPanel.add(lbl_y);		  inputPanel.add(txt_y);

		buttonsPanel.add(btn_solve);
		buttonsPanel.add(btn_clear);

		btn_solve.addActionListener(this);
		btn_clear.addActionListener(this);

		add(inputPanel,BorderLayout.NORTH);
		add(buttonsPanel,BorderLayout.CENTER);
		setResizable(false);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setVisible(true);
	}



but nothing happened the icon in the title bar is still the bluej(editor used) icon. what could i be doing wrong?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1