3 Replies - 360 Views - Last Post: 28 June 2012 - 05:57 AM Rate Topic: -----

#1 abhyudaya  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 22-June 12

JPanel display problem

Posted 23 June 2012 - 10:48 PM

I have designed a JFrame that includes four JPanels. The first two JPanels contain only JLabels and couple of JButtons within respective JInternalFrames. The other two JPanels contain a JMenuBar each within respective JInternalFrame. I've used NetBeans IDE 7.0.1 to design the above GUI. When I test this in NetBeans by Run Project option it works all fine but when I go to the project folder and run the .jar file, only one of the four panels is visible and the other 3 are invisible. (I have set their visibilities to true) However on rolling the cursor around the screen the buttons come up and upon clicking the region of menu bars they come into view. I have played around quite a bit with the repaint() method placing it in various places but to no effect. Instead using more repaints messes up the frame even more.

The code of the main frame is as below:
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import javax.swing.JButton;

/**
 *
 * @author A
 */
public class FrontEnd extends javax.swing.JFrame implements ActionListener
{

    /**
     * Creates new form FrontEnd
     */
    private TestImagePanel tip;
    private StandardImagePanel sip;
    private TestDetailsPanel tdp;
    private ConclusionDisplayPanel cdp;
    private BufferedImage stdImg=null,testImg=null;
    //private JButton reset;
    public FrontEnd() {
        initComponents();
        setLayout(new GridLayout(2,2));
        
        cdp=new ConclusionDisplayPanel();
        tdp=new TestDetailsPanel();
        sip=new StandardImagePanel();
        tip=new TestImagePanel(cdp,sip,tdp);
        tdp.setVisible(true);
        cdp.setVisible(true);
        sip.setVisible(true);
        tip.setVisible(true);
        add(tdp);
        add(cdp);
        add(sip);
        add(tip);
        
        //this.repaint();
        //tdp.addButton(reset);
        
    }
    
    @Override
    public void paint(Graphics g)
    {
       
       if(tdp.reset)
        {
            stdImg=null;
            testImg=null;
            sip.reset();
            //tip.reset();
            //sip.repaint();
            tip.repaint();
            
        }
       else  if((tip.getImage()!=null)&&(sip.getImage()!=null))
        {
            stdImg=sip.getImage();
            testImg=tip.getImage();
            //tdp.setLabels(sip.stdRed, sip.stdGreen, sip.stdBlue, tip.testRed, tip.testGreen, tip.testBlue);
            tip.repaint();
            sip.repaint();
           // cdp.setLabels(tip.testRed-sip.stdRed, tip.testGreen-sip.stdGreen, tip.testBlue-sip.stdBlue);
            
            //tdp.repaint();
            
        }
        else if(tip.getImage()!=null&&sip.getImage()==null)
        {
            testImg=tip.getImage();
            //tip.analyseImage(testImg);
            tdp.setLabels(0, 0, 0, tip.testRed, tip.testGreen, tip.testBlue, 0, tip.testAvg);
            tip.repaint();
            //tdp.repaint();
        }
        else if(tip.getImage()==null&&sip.getImage()!=null)
        {
            stdImg=sip.getImage();
            tdp.setLabels(sip.stdRed, sip.stdGreen, sip.stdBlue, 0, 0, 0, sip.stdAvg, 0);
            //sip.repaint();
            tdp.repaint();
        } 
        else
        {
            //tdp.setLabels(0, 0, 0, 0, 0, 0,0,0);
            tdp.repaint();
            cdp.repaint();
            tip.repaint();
            sip.repaint();
           // cdp.repaint();
            
        }
        
       
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 1343, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 790, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>                        

    /**
     * @param args the command line arguments
     */
   
    // Variables declaration - do not modify                     
    // End of variables declaration                   

    @Override
    public void actionPerformed(ActionEvent e) {
        
    }
}



Though the code is partially generated by NetBeans (a GUI builder) the only automatic code I've used in my project is for buttons or menu items. There probably is some problem with the repainting (a guess from my little over one year experience in JAVA). I would really be thankful if anyone could just help me. Sorry for posting again but din't get any reply first time and I'm in dire need of help. If other file codes are necessary I'll post them soon. Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: JPanel display problem

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2657
  • View blog
  • Posts: 11,214
  • Joined: 20-September 08

Re: JPanel display problem

Posted 24 June 2012 - 03:49 AM

Generally speaking, you should let the system decide when it needs to paint something. Why are you overriding paint and then painting panels explicitly in the first place?
Was This Post Helpful? 1
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: JPanel display problem

Posted 24 June 2012 - 07:55 PM

why are you overloading paint() here ?
You want to play the apprentice sorcerer or what ?
And don't expect much help from us if you use a GUI Builder
http://www.dreaminco...9&#entry1535889
Was This Post Helpful? 0
  • +
  • -

#4 abhyudaya  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 22-June 12

Re: JPanel display problem

Posted 28 June 2012 - 05:57 AM

Actually I need to call some methods of the JFrame from within the JPanels so I only had the option of calling getParent().repaint() and I mentioned sir that the GUI builder's code is only restricted to menu and button actions. And thanks for your help sir, I realised that the design was totally messy so I completely re-designed the whole software and cleared up places where I needed repaint() so the problem has been solved today. Thanks alot for your guidance.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1