Results 1 to 3 of 3
  1. #1
    Wolfik is offline Member
    Join Date
    May 2014
    Posts
    2
    Rep Power
    0

    Default JTabbedPanel and Paint

    Hello,

    I have small issue:
    JTabbedPanel and Paint-1.png
    JTabbedPanel and Paint-2.png
    Main
    Java Code:
    JTabbedPane tabsPane = new JTabbedPane();
            add(tabsPane,BorderLayout.CENTER);
            JPanel tab1Panel = new JPanel();
            JPanel tab2Panel = new JPanel();
     
            //DrawingWindow drawingWindow= new DrawingWindow();
            //add(drawingWindow);
     
     
            tabsPane.addTab("Animacja", tab1Panel); 
            tabsPane.addTab("Wykresy", tab2Panel);
            JButton test = new JButton("Press");
            tab2Panel.add(test);

    Drawing WIndow
    Java Code:
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
     
    import javax.swing.JPanel;
     
    public class DrawingWindow extends JPanel {
     
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        public static Balls balls=new Balls();
     
        public DrawingWindow() {
     
            MakeBall();
        }
     
     
     
     
        private void MakeBall()
        {    
                balls=new Balls(10,205,5,10);
     
     
        }
     
        public void paint(Graphics gg){
            super.paint(gg);
     
            Graphics2D g = (Graphics2D) gg;    
            g.setColor(Color.GRAY);
            g.fillRect(0,70,515,410);
            g.setColor(Color.WHITE);
            g.drawLine(10, 285, 57, 265);
            g.drawLine(10, 285, 57, 305);
     
            g.drawLine(515, 285, 458, 265);
            g.drawLine(515, 285, 458, 305);
            for(int ii=0;ii<Parameters.numberOfCovers;ii++)
            {
                if(Parameters.whatCovers[ii]==0)
                {
                    g.setColor(Color.YELLOW);
                    g.fillRect(132+(57*2*ii), 205, 29+2*Parameters.cmCovers[ii], 150 );
                }
                if(Parameters.whatCovers[ii]==1)
                {
                    g.setColor(Color.GREEN);
                    g.fillRect(132+(57*2*ii), 205, 29+2*Parameters.cmCovers[ii], 150 );
                }
            //    Ellipse2D.Double shape = new Ellipse2D.Double(balls.getX(), balls.getY(), balls.getVelocity(),balls.getRadius());
            //    g.fill(shape);
                repaint();
            } 
        }
        public void funkcja()
        {
            repaint();
        }
    }
    Java Code:
     DrawingWindow drawingWindow= new DrawingWindow();
            add(drawingWindow);
    My problem is that after uncommenting this last thing second picture happens. It hides JTabbedPanel :(. Any idea how to fix that?

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: JTabbedPanel and Paint

    Java Code:
    //add(drawingWindow);
    A JFrame uses a BorderLayout by default. When you add a component to the frame it will go to the "CENTER" by default if you don't specify a constraint. Only one component can be added to the CENTER of the frame so only the last component added is displayed. You can try the following to see the difference:

    Java Code:
    //add(drawingWindow, BorderLayout.SOUTH);
    Read the section from the Swing tutorial on How to Use BorderLayout (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container) for more information and examples.

    Also:

    1. Custom painting is done by overriding the paintComponent(...) method, not the paint() method. And don't forget to invoke super.paintComponent(...).
    2. NEVER invoke the repaint() method inside a painting method. This will cause an infinite loop

    The Swing tutorial also has a section on Custom Painting for more information.

  3. #3
    Wolfik is offline Member
    Join Date
    May 2014
    Posts
    2
    Rep Power
    0

Similar Threads

  1. Paint--- trying to paint an oval
    By noobie_nate in forum Java 2D
    Replies: 2
    Last Post: 06-05-2013, 06:45 PM
  2. How to get this paint?
    By Monster in forum New To Java
    Replies: 3
    Last Post: 04-15-2013, 02:01 PM
  3. Paint everything at once?
    By Lionlev in forum AWT / Swing
    Replies: 11
    Last Post: 10-15-2012, 04:01 AM
  4. Paint is invalid type for variable paint.
    By minibronya in forum New To Java
    Replies: 3
    Last Post: 05-25-2012, 06:52 AM
  5. Paint
    By ninjaturtlez in forum AWT / Swing
    Replies: 4
    Last Post: 12-17-2011, 05:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •