Refresh a JPanel

K

KenH

I have an applet with 2 JPanels (which both sit inside a third
container JPanel). One is a custom panel I built and I have no problems
with it. The other is a 'control panel' and contains some JButtons,
JTextFields, JLabels, and JRadioButtons.

My problem is that I cannot get it to refresh after it's been covered
by another window or whatever. I tried putting a call to repaint that
panel in the main applet's paint() method, but no joy. Interesting
thing is I use Tooltips for the control panel buttons. When a tooltip
disappears *most" of the panel refreshes (all but the top part of the
border).

I'm guessing at this point. Ideas?

Ken
 
T

Thomas Fritsch

KenH said:
I have an applet with 2 JPanels (which both sit inside a third
container JPanel). One is a custom panel I built and I have no problems
with it. The other is a 'control panel' and contains some JButtons,
JTextFields, JLabels, and JRadioButtons.

My problem is that I cannot get it to refresh after it's been covered
by another window or whatever. I tried putting a call to repaint that
panel in the main applet's paint() method, but no joy. Interesting
thing is I use Tooltips for the control panel buttons. When a tooltip
disappears *most" of the panel refreshes (all but the top part of the
border).

I'm guessing at this point. Ideas?

Ken
I'm guessing at this point, too, without seeing your code.
May be there is something bad in your applet's paint() method.
 
K

KenH

I never like sending snipits of code because I figure I'm going to miss
something. But that said, here is (I hope) the relevent portions of my
code:


public class displayType1 extends JApplet
implements ActionListener {

displayCanvas dc = new displayCanvas();

// INITILIZE THE CONTROLS TO BE ADDED TO APPLET
JButton reset = new JButton("Reset"); // create Reset JButton
JButton print_bt = new JButton("Print"); // create Print JButton
JButton table_bt = new JButton("Table"); // create Table JButton

JRadioButton major_axis = new JRadioButton("Major");
JRadioButton minor_axis = new JRadioButton("Minor");
ButtonGroup cbg = new ButtonGroup(); // setup the radio buttons

JLabel lMin = new JLabel("Min:", JLabel.RIGHT);
JTextField tMin = new JTextField(6);
JLabel lMax = new JLabel("Max:", JLabel.RIGHT);
JTextField tMax = new JTextField(6);

JPanel panelCtrl = new JPanel();
JPanel bigBox = new JPanel();

// called when browser first launches applet
public void init() {

// create a content object
Container content = getContentPane();

// SET TOOLTIPS FOR BUTTONS
reset.setToolTipText("Return the chart to it's default X-Y axis
limits.");
print_bt.setToolTipText("Print the chart. You will be warned that the
Applet is trying to print. Just click Ok.");
table_bt.setToolTipText("Display the raw data used to generate this
chart.");
major_axis.setToolTipText("Set the major axis as the active axis in
the chart.");
minor_axis.setToolTipText("Set the minor axis as the active axis in
the chart.");

// add radio buttons, textbox inputs, and labels
major_axis.setSelected(true); // select Major axis radio button
cbg.add(major_axis); // group radio buttons
cbg.add(minor_axis);

panelCtrl.setBorder(BorderFactory.createLineBorder (Color.blue, 1));
panelCtrl.setLayout(new FlowLayout(FlowLayout.CENTER,4,2));

panelCtrl.add(major_axis);
panelCtrl.add(minor_axis);

panelCtrl.add(lMin);
panelCtrl.add(tMin);
panelCtrl.add(lMax);
panelCtrl.add(tMax);

panelCtrl.add(reset);
panelCtrl.add(table_bt);
panelCtrl.add(print_bt);

bigBox.setLayout (new BoxLayout(bigBox, BoxLayout.Y_AXIS));

panelCtrl.setPreferredSize(new Dimension((int)(D_WIDTH),
(int)(D_HEIGHT * .09))); // width, height
bigBox.add(panelCtrl);
dc.setPreferredSize(new Dimension((int)(D_WIDTH), (int)(D_HEIGHT *
..91))); // width, height
bigBox.add(dc);

content.add(bigBox);
}

public void paint(Graphics g) {
panelCtrl.repaint();
dc.repaint();
}

public void update(Graphics g) {
paint(g);
}
}
 
T

Thomas Fritsch

KenH said:
I never like sending snipits of code because I figure I'm going to miss
something. But that said, here is (I hope) the relevent portions of my
code: Uuuuh, what a mess... ;-)


public class displayType1 extends JApplet
implements ActionListener { [...]

public void paint(Graphics g) {
super.paint(); // leaving out this line effectively kills all painting
// the following 2 work-around lines should not be needed anymore
 
K

KenH

Well...the good news is nothing broke and I learned something new. You
were right, I put 'super.paint(g)' in and was able to get rid of the
other lines, but there was no change in that top JPanel.
 
R

Roedy Green

panelCtrl.setPreferredSize(new Dimension((int)(D_WIDTH),
(int)(D_HEIGHT * .09))); // width, height
bigBox.add(panelCtrl);
dc.setPreferredSize(new Dimension((int)(D_WIDTH), (int)(D_HEIGHT *
.91))); // width, height


I always use setPreferredSize in conjunction with setMinimumSize and
setMaximum size. What may be happening is your component has
logically shrunk to 0x0. After you use setPreferredSize on a Component
you need to validate its container. Your Component does not have a
size until after validate does the layout, calls getMinimumSize,
getPreferredSize and getMaximumSize and decides how big the component
shoud be and calls setSize.
 
T

Thomas Fritsch

KenH said:
Well...the good news is nothing broke and I learned something new. You
were right, I put 'super.paint(g)' in and was able to get rid of the
other lines, but there was no change in that top JPanel.
Eeeh, which one do you mean with "that top JPanel"? I'm totally confused.
BTW: I wasn't able to understand your container hierarchy until drawing
this tree:
Container content = this.getContentPane()
+-- JPanel bigbBox
+-- JPanel panelCtrl
| +-- JRadioButton majorAxis
| +-- JRadioButton minorAxis
| +-- JLabel lMin
| +-- JTextField tMin
| +-- JLabel lMax
| +-- JTextField tMax
| +-- JButton reset
| +-- JButton table_bt
| +-- JButton print_bt
+-- displayCanvas dc

Even then your code is still *very* hard to understand because of all
the cryptic variable names. Remember that people (including me) can't
memorize more than 5-7 names simultaenously. I strongly recommend to
rename all variables, for example to
| +-- JRadioButton minorAxisRadioButton
| +-- JLabel minLabel
| +-- JTextField minTextField
| +-- JButton resetButton
in order to expect help from anyone here (and to understand your own
code in 2 weeks).
 
T

Thomas Fritsch

KenH wrote:
[...]
bigBox.setLayout (new BoxLayout(bigBox, BoxLayout.Y_AXIS));
panelCtrl.setPreferredSize(new Dimension((int)(D_WIDTH),
(int)(D_HEIGHT * .09))); // width, height
bigBox.add(panelCtrl);
dc.setPreferredSize(new Dimension((int)(D_WIDTH), (int)(D_HEIGHT *
.91))); // width, height
bigBox.add(dc);
Not an answer to your original question, but only a hint: Instead of the
code above, you could/should achieve the same layout in a cleaner way,
i.e. without using setPreferredSize and the magic constants
bigBox.setLayout (new BorderLayout());
bigBox.add(panelCtrl, BorderLayout.NORTH);
bigBox.add(dc, BorderLayout.CENTER);[...]
 
K

KenH

I see what you're saying but the applet sits in a webpage and isn't
resizable. Also, I don't have a problem *seeing* the control panel I
made or any of it's buttons. It just doesn't get repainted after being
covered by another window, or after its own window has been minimized
and restored.

It will repaint if I hit refresh though.
 
K

KenH

I'll try to do that. Nobody else around me works on this sort of stuff
so I basically write it for me to understand.
 
K

KenH

Damn. Don't know why that worked but it did. Layouts confuse the hell
out of me...just bought a book on Swing which helped some.

What I don't understand about the lines you gave is how does the applet
know how to size everything? It minimized my control panel and
maximized the chart (which is what I wanted). I think the reason I
abandoned that method was it was drawing everything equal size before.
Drove me up the wall. So when I found this BoxLayout I ran with it.
 
R

Roedy Green

What I don't understand about the lines you gave is how does the applet
know how to size everything? It minimized my control panel and
maximized the chart (which is what I wanted). I think the reason I
abandoned that method was it was drawing everything equal size before.
Drove me up the wall. So when I found this BoxLayout I ran with it.

If you have a null layout, then it is up to you to set the sizes and
locations of everything in the container. They are often size 0 to
start. You use methods like setSize, setLocation, SetBounds.

If you have a layout, then when you call validate, the layout asks the
min, max and preferred size of everything in the container, figures
out how big everything should be and where it should be then tells
each component its size and location.

It might help if you wrote a tiny LayoutManager yourself to understand
how it all works.

See http://mindprod.com/jgloss/layout.html
 
T

Thomas Fritsch

KenH said:
Damn. Don't know why that worked but it did. Layouts confuse the hell
out of me...just bought a book on Swing which helped some.

What I don't understand about the lines you gave is how does the applet
know how to size everything? It minimized my control panel and
maximized the chart (which is what I wanted).
:) The magic is in the pudding, as the British say. It is buried in Sun's
code of LayoutManager, Container and Component. Actually the applet (like
any container) does *not* know how to size everything. Instead it delegates
all the resizing to its LayoutManager. The LayoutManager then asks each of
the container's components for their preferred and minimum sizes, tries to
honour those sizes as much as possible, and moves each component to its
place. The trick is, that the components themselves can be containers with
their own layout-managers.
The BorderLayout, for example, takes the minimum y-size and preferred x-size
of the North component, takes the minimum x-size and preferred y-size of the
West-component, ..., stretches some components here and there. And the magic
goes its way.
 
R

Roedy Green

Damn. Don't know why that worked but it did. Layouts confuse the hell
out of me...just bought a book on Swing which helped some.

There is a tool that can help understand the granddaddy of all
LayoutMangers the grid bag layout. It lets you fiddle and see the
results immediately on screen. You soon develop an intuition for what
all the constraints can do for you.

see http://mindprod.com/jgloss/gridbagger.html
 
K

KenH

Fixed my problem!

Bottom line, I had a bug in the JPanel that the chart was being drawn
on (I had a line in paintComponent method to set the border, forgetting
that I had already set that in the constructor). Oddly, this didn't
kick ANY error.

The first indication of the problem was the fact that when I removed
all the repaint lines for that charting panel (and the applet started
with a big blank space), the bottom control JPanel worked - refreshed
itself whenever anything passed over it, window was restored, etc.

I just stripped the charting panel down to the bone and rebuilt it,
piece by piece until I found the buggy line.

One for the books.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,200
Latest member
LaraHunley

Latest Threads

Top