GuidesJava Discussion: Need Guru for Panels

Java Discussion: Need Guru for Panels

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Welcome. Click here to access the
new home of Gamelan’s
discussions
on Java.

From the Gamelan Forum…


NEED GURU FOR JPANELS

Cunduro Caca – 01:56am Apr 8, 2002 EDT

Hi! I am implementing a program where I have six panels which extend JPanel. In
each of them, I have little dots moving according to how they are sorted by a
sorting algorithm (quicksort, etc.) I want to be able to switch the panels and
place them in order of performance, from left to right and top to bottom (so first
row 1st, 2nd, 3rd and second row 4th, 5th, 6th) as the sorting algorithms are running
(so they should be switching around the whole time). The way I repaint my panels
is all at once by using the paintImmediately method, which I trigger at regular time
intervals by a timer. My question is, how can I switch the panels, which are in a
gridBagLayout? In handling the timer event, just before repainting, I tried
re-specifying myConstraints, applying them to each panel, calling
content.remove(specificPanel), and then content.add(specificPanel), but the
program just stops.

Suggestions?

Thanks,
Cunduro




Norm R – 05:57pm Apr 8, 2002 EDT

Here’s an idea: Instead of moving the JPanels around, have their paintComponent()
method call another class with a method: paintIt() to do the painting.
Have an array in the main program that holds instances of this class with an instance
for each panel. When you create the JPanel, pass it the index position it is to use
when calling the paintIt() method. To change where a paintIt()
method displays its stuff, swap the contents of the array around.

Some code samples:

   // Define a class that will do the painting for the Panels 
   abstract class DrawPanel { 
      public abstract void paint(Graphics g, JPanel p); 
   } 
   // Define an array to hold pointers to all the painting routines 
   DrawPanel[] painters = new DrawPanel[2]; 

..... 

painters[0] = new DrawPanel() { 
         public void paint(Graphics g, JPanel p) { 
// System.out.println("paint p1 " + p.getBounds()); 
            g.setColor(Color.red); 
            g.fillOval(0,0, 50, 50); 
            g.setColor(Color.black); 
            g.drawString("Panel One", 10, 25); 
         } 
      }; 

JPanel p1 = new JPanel() { 
         int dpIdx = 0; // index to this panel's painter 
         public Dimension getPreferredSize() { 
            return new Dimension(100, 50); 
         } 
         public void paintComponent(Graphics g) { 
            painters[dpIdx].paint(g, this); // go use our painter 
         } 
      }; 

We hope you’ll participate in the give and take of information with your
peers that has been a hallmark of our site since the early days of Java technology.

If you’ve got a problem, here’s the perfect place to ask others for help
solving it. If you’ve got advice, here’s the perfect place to help build a
community of like-minded Java specialists. Enjoy!

The Dalang

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories