Article View: borland.public.jbuilder.java.swing
Article #249Re: JPanel.repaint( ) omited by the "drawing system"
From: Paul Furbacher [TeamB]
Date: Fri, 13 Apr 2007 04:14
Date: Fri, 13 Apr 2007 04:14
150 lines
6152 bytes
6152 bytes
Hiroshi Nakata wrote:
> Thank you Paul for replying to my question. I am aware that all Swing/AWT
> components
> (in Container) which are entirely or partially in the dirty region will be
> automatically
> redrawn by super.paintComponent() [which is typically called at the very
> beggining
> of jpnl.paintComponent().] Therefore if no g.draw***()s are needed in jpnl,
> no need to
> call jpnl.repaint(). Your example is exactly that. But when jpnl doesn't
> have any such
> components but needs to be drawn with g.draw***(), things are different
> because
> super.paintComponent() won't draw anything but clear the panel in that case.
> I think [...]
I'm not sure I understand what or how you are drawing -- a code
snippet of the custom component may have been more helpful.
Is the following, which does repaint correctly, close to what
you are trying to do?
package repaint_issue;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/** Shows that you don't have to do anything to update
* the UI after showing a JOptionPane.showXXXDialog()
*/
public class Frame1
extends JFrame {
JLabel jLabel1 = new JLabel();
JTextField tf = new JTextField();
GridBagLayout gridBagLayout1 = new GridBagLayout();
JButton quitButton = new JButton();
JButton showButton = new JButton();
JPanel specialPanel = new SpecialPanel();
GridBagLayout gridBagLayout2 = new GridBagLayout();
public Frame1() {
try {
jbInit();
}
catch (Exception exception) {
exception.printStackTrace();
}
}
private void jbInit() throws Exception {
getContentPane().setLayout(gridBagLayout1);
jLabel1.setForeground(Color.yellow);
jLabel1.setText("Date: ");
quitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
quitButton_actionPerformed(actionEvent);
}
});
showButton.setOpaque(false);
showButton.setText("Show message");
showButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
showButton_actionPerformed(actionEvent);
}
});
quitButton.setOpaque(false);
quitButton.setText("Quit");
specialPanel.setLayout(gridBagLayout2);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
specialPanel.add(tf, new GridBagConstraints(1, 2, 2, 1, 1.0, 0.0
, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
new Insets(12, 0, 0, 11), 322, 0));
specialPanel.add(jLabel1,
new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0
, GridBagConstraints.WEST, GridBagConstraints.NONE,
new Insets(12, 12, 0, 8), 0, 0));
specialPanel.add(quitButton,
new GridBagConstraints(2, 3, 1, 1, 0.0, 0.0
, GridBagConstraints.EAST, GridBagConstraints.NONE,
new Insets(12, 0, 11, 11), 0, 0));
specialPanel.add(showButton,
new GridBagConstraints(0, 3, 2, 1, 0.0, 0.0
, GridBagConstraints.WEST, GridBagConstraints.NONE,
new Insets(12, 12, 11, 0), 0, 0));
this.getContentPane().add(specialPanel,
new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0
, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH,
new Insets(0, 0, 0, 0), 0, 0));
}
public static void main(String[] args) {
Frame1 frame1 = new Frame1();
frame1.pack();
frame1.setVisible(true);
}
public void quitButton_actionPerformed(ActionEvent actionEvent) {
System.exit(0);
}
public void showButton_actionPerformed(ActionEvent actionEvent) {
String date = new Date().toString();
JOptionPane.showMessageDialog(this, "A message" + date);
tf.setText(date);
}
public class SpecialPanel extends JPanel {
public SpecialPanel() {
super();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Insets insets = getInsets();
int currentWidth = getWidth() - insets.left - insets.right;
int currentHeight = getHeight() - insets.top - insets.bottom;
Graphics2D g2d = (Graphics2D)g.create();
g2d.fillRect(0, 0, currentWidth, currentHeight);
g2d.drawRect(2, 2, currentWidth - 2, currentHeight - 2);
g2d.setColor(Color.RED);
g2d.drawLine(3,3, currentWidth - 3, currentHeight - 3);
g2d.drawString("Testing .................", 200, 70);
g2d.dispose();
}
}
}
--
Paul Furbacher (TeamB)
Save time, search the archives:
http://info.borland.com/newsgroups/ngsearch.html
Is it in Joi Ellis's Faq-O-Matic?
http://www.visi.com/~gyles19/fom-serve/cache/1.html
Finally, please send responses to the newsgroup only.
That means, do not send email directly to me.
Thank you.
Technical Details
Message-ID:
<461f0375$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!newspeer1.nwr.nac.net!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mailReferences:
<46170d67$1@newsgroups.borland.com> <461c6bba$1@newsgroups.borland.com> <461ef32c$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 111d45,46b9de0c8529390 X-Google-Attributes: gid111d45,public X-Google-Language: ENGLISH,ASCII-7-bit Date: Fri, 13 Apr 2007 00:14:07 -0400 From: "Paul Furbacher [TeamB]" <pfurbacher@mac.com> Organization: Reverse Alchemy, Inc. User-Agent: Thunderbird 1.5.0.10 (Macintosh/20070221) MIME-Version: 1.0 Newsgroups: borland.public.jbuilder.java.swing Subject: Re: JPanel.repaint( ) omited by the "drawing system" References: <46170d67$1@newsgroups.borland.com> <461c6bba$1@newsgroups.borland.com> <461ef32c$1@newsgroups.borland.com> In-Reply-To: <461ef32c$1@newsgroups.borland.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 69.142.194.228 Message-ID: <461f0375$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1176437621 69.142.194.228 (12 Apr 2007 21:13:41 -0800) Lines: 150 Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!newspeer1.nwr.nac.net!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail