Article View: borland.public.jbuilder.java.swing
Article #252Re: JPanel.repaint( ) omited by the "drawing system"
From: Hiroshi Nakata
Date: Sat, 14 Apr 2007 09:40
Date: Sat, 14 Apr 2007 09:40
263 lines
10480 bytes
10480 bytes
Thanks again. Ok, I tried your code and visually confirmed that g.draw***()
in your code
are indeed executed when the message dlg is closed. [However I didn't
confirm that all
pieces are redrawn (by the closure) because the frame is too small and black
background
(including 2 buttons.)]
I realized that your case calls JOptionPane.show*** from within ActionEvent
handling
(in the event thread). My case was that it was called from within
paintComponent (though
not directly in paintCompo but in some method initiated from paintCompo). Ie
before
the paint/drawing handling (in the event thread) is completed, another
paint/drawing
request is queued in my case.
To simulate my case, I changed your code a bit in mechanics and executed. (I
trimmed
all un-related stuff such as GridBagLayout, and Graphis2D and employed short
variable
names. In spite of 1000 times improvement in all computing features in such
a short time,
display realestate remains almost the same; we don't have 5 screens of 100
lines each,
requiring us to scroll up and down so many times... This was the reason for
the trim.)
I included the code below. As you run, you can confirm that the result or
effect from
calling (and closing) JOption.show*** at ActionEvent and paint handling are
indeed
different.
To call JOptionPane.show*** within paintCompo, you move the main frame (so
that
the system triggers JPanel.repaint(). Well to be more precise,
JFrame.repaint() first then,
JFrame calls repaint(), or update(), of its child compos in it.)
My stuff is ready to switch to yours or back to mine by // ing a couple of
lines. Your
way seems better. Your way, however, is not quite right because the
(diagonal) line
is drawn in 2 colors; one with intended another with unintended because of
the fact
that the pnl.repaint() call from the dlg closure is queued and the actual
paintCompo
from it occurs afterward, not within the paintCompo (because otherwise
doomed to
stack overflow) and the actual drawing is limited to the dirty region. This
is why I
needed to call pnl.repaint() again after JOption.show*** so that this new
drawing
draws everything, inside and outside the dirty region. But this 2nd
repaint() is not
executed...
As a conclusion, I am still in the woods...
Anyway here is my stuff converted from yours.
package repainttest;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class test extends JFrame {
JButton qBtn=new JButton("Quit"),sBtn=new JButton("Set");
JPanel pnl=new NoCompoPanel();
boolean set=false;
Color colr=Color.black;
public test() {
Container cp=getContentPane();
qBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {qBtn_acted(e);}});
sBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {sBtn_acted(e);}});
pnl.setPreferredSize(new Dimension(100,200));
cp.add(sBtn,BorderLayout.WEST);
cp.add(qBtn,BorderLayout.EAST);
cp.add(pnl,BorderLayout.NORTH);
}
public static void main(String[] args) {
test test1=new test();
test1.setSize(new Dimension(200,250));
test1.setVisible(true);
}
public void qBtn_acted(ActionEvent e) {System.exit(0);}
public void sBtn_acted(ActionEvent e) {
colr=Color.blue;
set=true; //my version - remark this to set to Paul's version
//JOptionPane.showMessageDialog(this,"Message"); //Paul's version
}
public class NoCompoPanel extends JPanel {
public NoCompoPanel() {}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (set) //my version - remark this to set to Paul's version
JOptionPane.showMessageDialog(this,"Message");
g.setColor(colr);
g.drawLine(0,0,200,200);
set=false; //my version - remark this to set to Paul's version
colr=Color.red;
g.dispose();
}
"Paul Furbacher [TeamB]" <pfurbacher@mac.com> wrote in message
news:461f0375$1@newsgroups.borland.com...
> 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:
<46207686@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!proxad.net!212.101.4.254.MISMATCH!solnet.ch!solnet.ch!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<46170d67$1@newsgroups.borland.com> <461c6bba$1@newsgroups.borland.com> <461ef32c$1@newsgroups.borland.com> <461f0375$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 Reply-To: "Hiroshi Nakata" <hiroshinakata@hotmail.com> From: "Hiroshi Nakata" <hiroshinakata@hotmail.com> Newsgroups: borland.public.jbuilder.java.swing References: <46170d67$1@newsgroups.borland.com> <461c6bba$1@newsgroups.borland.com> <461ef32c$1@newsgroups.borland.com> <461f0375$1@newsgroups.borland.com> Subject: Re: JPanel.repaint( ) omited by the "drawing system" Date: Sat, 14 Apr 2007 02:40:11 -0700 Lines: 265 Organization: N/A X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1409 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409 NNTP-Posting-Host: 74.119.19.143 Message-ID: <46207686@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1176532614 74.119.19.143 (13 Apr 2007 23:36:54 -0800) Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!proxad.net!212.101.4.254.MISMATCH!solnet.ch!solnet.ch!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail