#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    13
    Rep Power
    0

    Java Swing- how to add shapes onto JTabbedPane?


    Suppse I had a paint method:

    Code:
    public void paintComponent(Graphics g) {
    super.paintComponents(g);
    
    g.drawString("BLAH", 20, 20);
    g.drawRect(200, 200, 200, 200);
    
    }

    How would I specify to add the shapes to one of the tabs I have in my program with a layout like this for example.


    Code:
    public void createPage1()
    {
    panel = new JPanel();
    panel.setLayout( null );
    
    // I want to add shapes here
    
    }

    From my understanding you need a init method to add shapes but when you have five separate interfaces how do you add shapes to just one?

    If you want me to add more info or code tell me.
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    If the shapes are components, use the container's add() method to add them.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    13
    Rep Power
    0
    Originally Posted by NormR
    If the shapes are components, use the container's add() method to add them.
    Are the shapes (g) components? Because I tried that and it didnt work.

    Also if all shapes are g- how do I specify which shapes to add to a page.
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    By component I meant a class that extends the Component class.
    I don't know what a (g) component is or what g- means.

    What does "didn't work" mean? Where there errors? Copy the full text of the error messages and paste it here.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    13
    Rep Power
    0
    Originally Posted by NormR
    By component I meant a class that extends the Component class.
    I don't know what a (g) component is or what g- means.

    What does "didn't work" mean? Where there errors? Copy the full text of the error messages and paste it here.

    There are no syntax issues present.

    The issue is like I said I don't know how to incorporate shapes from a graphics method into one of the JTab methods. Your container method would work assuming there was only one interface.
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    Can you make a small program that compiles, executes and shows what you are trying to do?

    shapes from a graphics method
    Are you talking about the draw methods? The first code in post#1 shows an example of the use of a draw method.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    13
    Rep Power
    0
    Code:
    package components;
    
    import java.awt.*;
    import javax.swing.*;
    
    public class Example
        extends JFrame
    
    {
        private JTabbedPane tabbedPane;
        private JPanel panel;
    
    
        public Example ()
        {
            // NOTE: to reduce the amount of code in this example, it uses
            // panels with a NULL layout.  This is NOT suitable for
            // production code since it may not display correctly for
            // a look-and-feel.
    
            setTitle ("Program");
            setSize (900, 600);
            setBackground (Color.gray);
    
            JPanel topPanel = new JPanel ();
            topPanel.setLayout (new BorderLayout ());
            getContentPane ().add (topPanel);
    
            // Create the tab pages
            createPage1 ();
    
            // Create a tabbed pane
            tabbedPane = new JTabbedPane ();
            tabbedPane.addTab ("Shape Panel", panel);
    
            topPanel.add (tabbedPane, BorderLayout.CENTER);
        }
    
    
        public void createPage1 ()
        {
            panel = new JPanel ();
            panel.setLayout (null);
    
          // I want to add shapes onto this panel
        }
    
        public void Paint (Graphics g) { // I am using no framework
            super.paintComponents(g);
    
            g.drawString("BLAH", 20, 20);
            g.drawRect(200, 200, 200, 200);
        }
    
    
        public static void main (String args[])
        {
            // Create an instance of the test application
         Example mainFrame = new Example ();
            mainFrame.setVisible (true);
    
    
        }
    }
    Is it because I have no frame attached?
  14. #8
  15. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    Is it because I have no frame attached?
    Can you explain what "it" means?

    See the tutorial: http://docs.oracle.com/javase/tutori...ing/index.html
    Last edited by NormR; April 16th, 2013 at 07:11 PM.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    13
    Rep Power
    0
    Originally Posted by NormR
    Can you explain what "it" means?

    See the tutorial: http://docs.oracle.com/javase/tutori...ing/index.html
    I was referring to the program.

    I cannot implement a shape without a frame and then calling the pack (); command after initializing it.

    One thing I don't understand. Where are all the shapes saved to? Are they each individual or are they all part of the same component (g)? What I want to do within the program is have a paint method with all the shapes and then with each panel call a certain shape to appear within it from the graphics method. However that doesn't seem so easy.
  18. #10
  19. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    Where are all the shapes saved to?
    What is the shape you are talking about? Are they classes?

    Java is case sensitive. Paint is not the same as paint. If you want to override a method in a class, you should code the @Override statement just before the method statement to have the compiler test that the method is being correctly overridden.
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    13
    Rep Power
    0
    Originally Posted by NormR
    What is the shape you are talking about? Are they classes?

    Java is case sensitive. Paint is not the same as paint. If you want to override a method in a class, you should code the @Override statement just before the method statement to have the compiler test that the method is being correctly overridden.
    I dont know what to say and I don't know too much to begin with. I guess Il start looking at the API tutorials.

IMN logo majestic logo threadwatch logo seochat tools logo