Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 23rd, 2005, 9:13 AM   #1
susam_pal
Encoder
 
Join Date: Sep 2005
Location: India
Posts: 12
Rep Power: 0 susam_pal is on a distinguished road
Send a message via Yahoo to susam_pal
repaint() and validate() - When to use?

i don't quite understand when to use validate() and when to use repaint().

the book "Core Java" says, when we add or remove components from a panel, we must call the repaint() method of the panel as panel.repaint().

when we change the size of any component in the panel, like changing the size of a JTextField, we must use validate() of the panel as panel.validate() in order to recompute the dimensions of all components in the panel. it also says when validate() is called, repaint() automatically happens!

but I find that some of my programs work just fine even without using repaint() or validate() methods. I am including one such progam below.

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

public class TextFieldTest
{
	public static void main(String[] args)
	{
		TextFrame frame = new TextFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.show();
	}
}

class TextFrame extends JFrame
{
	public TextFrame()
	{
		defaultPlaf = UIManager.getLookAndFeel();

		plafLabel = new JLabel("Look and Feel:");
		sizeLabel = new JLabel("Textfield Size:");
		resultLabel = new JLabel("Result Textfield:");

		button1 = new JRadioButton("Default");
		button2 = new JRadioButton("Metal");
		button3 = new JRadioButton("Motif");
		button4 = new JRadioButton("Windows");

		sizeText = new JTextField(2);
		resultText = new JTextField(textLength);

		changeButton = new JButton("Change");

		group = new ButtonGroup();

		plafPanel = new JPanel();
		sizePanel = new JPanel();
		resultPanel = new JPanel();

		group.add(button1);
		group.add(button2);
		group.add(button3);
		group.add(button4);

		plafPanel.add(plafLabel);
		plafPanel.add(button1);
		plafPanel.add(button2);
		plafPanel.add(button3);
		plafPanel.add(button4);

		sizePanel.add(sizeLabel);
		sizePanel.add(sizeText);
		sizePanel.add(changeButton);

		resultPanel.add(resultLabel);
		resultPanel.add(resultText);

		Container contentPane = getContentPane();
		contentPane.add(plafPanel, BorderLayout.NORTH);
		contentPane.add(sizePanel, BorderLayout.CENTER);
		contentPane.add(resultPanel, BorderLayout.SOUTH);

		changeButton.addActionListener(new
			ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					try
					{
						if(button2.isSelected())
							UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
						else if(button3.isSelected())
							UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
						else if(button4.isSelected())
							UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
						else
							UIManager.setLookAndFeel(defaultPlaf);
					}
					catch(Exception ex)
					{
						System.out.println("Exception occured at UIManager.setLookAndFeel() statement!");
					}
					SwingUtilities.updateComponentTreeUI(TextFrame.this);
					try
					{
						resultText.setColumns(Integer.parseInt(sizeText.getText().trim()));
					}
					catch(NumberFormatException ex)
					{
						new PopupFrame("Error!", "The size entered is not a paresable number!", TextFrame.this);
					}

				}
			});

			pack();
			Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
			setLocation((screen.width-getWidth())/2, (screen.height-getHeight())/2);
			setResizable(false);
			setTitle("TextFrame Test");

	}

	private JLabel plafLabel, sizeLabel, resultLabel;
	private JRadioButton button1, button2, button3, button4;
	private JTextField sizeText, resultText;
	private JButton changeButton;
	private ButtonGroup group;
	private JPanel plafPanel, sizePanel, resultPanel;

	private int textLength = 10;
	private LookAndFeel defaultPlaf;
}

class PopupFrame extends JFrame
{
	public PopupFrame(String title, String message, final JFrame frame)
	{
		frame.setEnabled(false);

		msgLabel = new JLabel(message, SwingConstants.CENTER);
		okButton = new JButton("Ok");
		okPanel = new JPanel();
		Container contentPane = getContentPane();

		okPanel.add(okButton);
		contentPane.add(msgLabel, BorderLayout.CENTER);
		contentPane.add(okPanel, BorderLayout.SOUTH);

		okButton.addActionListener(new
			ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					frame.setEnabled(true);
					PopupFrame.this.dispose();
				}
			});

		pack();
		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
		setLocation((screen.width-getWidth())/2, (screen.height-getHeight())/2);
		setResizable(false);
		setTitle(title);
		show();
	}
	private JLabel msgLabel;
	private JButton okButton;
	private JPanel okPanel;
}

this code works fine without any validate() or repaint() method calls.

please tell me whether i should use any validate() or repaint() methods in this
code.

according to the book i must add a validate() method to actionPerformed() method of the anonymous ActionListener class added to changeButton in the default constructor of the TextFrame class because there is a resultText.setColumns(Integer.parseInt(sizeText.getText().trim())); statment in the actionPerformed() method.

what do you say?
susam_pal is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:52 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC