Results 1 to 5 of 5

Thread: Custom JPanel

  1. #1
    morris4019 is offline Member
    Join Date
    Nov 2010
    Location
    Federal Way, WA
    Posts
    18
    Rep Power
    0

    Default Custom JPanel

    Hi,

    I'm designing a custom JPanel which will act sort of like a JTabbedPane. Basically I need ONLY very simple functionality of a JTabbedPane, but I need the ability to control the placement and colors of every aspect of the TabbedPane. I had no luck manually trying to override JTabbedPane background and foreground colors so I started on this class.

    My question is regarding painting components that have been added to this custom container. A simple test is, I've not included any code to actually draw a "tab" at the top of the component because that is not what i'm worried about (it already works perfect).

    Java Code:
    public class CustomPanel extends JPanel {
    
       public static void main(String[] args) {
    
          JFrame frame = new JFrame();
             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             frame.setSize(500,500);
          
          CustomPanel panel = new CustomPanel();
             TextArea text = new JTextArea();
                text.setPreferredSize(new Dimension(500,500));
             panel.add(text);
    
           frame.add(panel);
           frame.setVisible(true);
    
       }
    
    
       public static final int DEF_TAB_HEIGHT = 20; 
    
       public CustomPanel() {
          super();
          setOpaque(false);
          setLayout(new BorderLayout());
       }
    
       @Override
       public void paintComponent(Graphics g) {
     
          // Save old graphics state.
          Color oldColor = g.getColor();
          
          // Draw background of tab panel. (NOT TAB ITSELF). The background
          // sits DEF_TAB_HEIGHT from the top of the component bounds.
          g.setColor(new Color(240,240,240));
          g.fillRect(0, DEF_TAB_HEIGHT,
                       getWidth(), getHeight() - DEF_TAB_HEIGHT, null)
    
          // Paint the child components on top of the panel
          g.drawComponents(g);
    
          // Reset graphics state
          g.setColor(oldColor);
    
       }
    
    }
    So what i get from this is a copy of the JTextArea, very small, not fitting the window size, in the top left corner of the panel, as well as at the very top center of the window. Furthermore, the JTextArea expands as I type in to it, but does not start at the correct size.

    What I want is the JTextArea to act normally, as if i added it to a standard JPanel, and to control how far down the JTextArea sits on the y location. I would like the JTextArea to fit inside the bounds of the rectangle that I drew.

    -Mike

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by morris4019 View Post
    Hi,

    I'm designing a custom JPanel which will act sort of like a JTabbedPane. Basically I need ONLY very simple functionality of a JTabbedPane, but I need the ability to control the placement and colors of every aspect of the TabbedPane. I had no luck manually trying to override JTabbedPane background and foreground colors so I started on this class.
    Perhaps we can attack the underlying problem rather than your current code solution attempt. What visual result are you trying to achieve? Can you post images of what it's supposed to look like?

  3. #3
    morris4019 is offline Member
    Join Date
    Nov 2010
    Location
    Federal Way, WA
    Posts
    18
    Rep Power
    0

    Default

    Ok, sounds good.

    So I don't have pictures uploaded anywhere online, but a really good example of what I am trying to do is not to re-create a JTabbedPane, but just have a JTabbedPane which looks similar to the document layout of NetBeans. Actually i'm modeling the look right after NetBeans.


    I've had no luck trying to get the JTabbedPane to give me the functionality of chaning background colors and placement of components inside the tab itself to the extent I want.

    I would like to be able to have Square Tabs, a close button inside the tab, but be able to align the components inside the tab (I will be using a custom JTextArea which I've created, overloaded paintComponent method) all the way up to the left and right borders. The above example, inside netbeans, shows exactly how I wish the layout to be. You'll notice what i mean about the placement of the text area inside netbeans document window.

    The ultimate goal is the same as netbeans as well because I will be using the tabbed pane to show multiple documents.

    The whole JTabbedPane will be placed inside a split panel.

    -Mike
    Last edited by morris4019; 04-08-2011 at 09:30 PM. Reason: Added picture of netbeans window for example of tabs i would like.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Hm, we may have to wait for one of the Swing pros like Darryl or camickr to help you out. One thing I do know is that NetBeans is not just an IDE, but it's also a platform, and you can develop your own complex programs using this platform and likely using the components that you admire. Don't ask me how though! :)

  5. #5
    morris4019 is offline Member
    Join Date
    Nov 2010
    Location
    Federal Way, WA
    Posts
    18
    Rep Power
    0

    Default

    :D. Ya, I have made lots of custom components in this program I am writing but I am having trouble getting the custom components to display correctly inside the JTabbedPane.

    -Michael

Similar Threads

  1. Adding a jpanel to a customized Jpanel Class
    By trishtren in forum AWT / Swing
    Replies: 7
    Last Post: 04-05-2011, 06:52 PM
  2. Adding Jpanel ontop of another Jpanel
    By Manfizy in forum AWT / Swing
    Replies: 4
    Last Post: 03-05-2011, 10:34 PM
  3. Placing a new JPanel over a paint overriden JPanel
    By Tanshaydar in forum AWT / Swing
    Replies: 4
    Last Post: 12-08-2010, 06:00 PM
  4. Replies: 1
    Last Post: 11-09-2010, 09:28 AM
  5. Custom GUI...Help?!
    By wadhah.alhaddad in forum New To Java
    Replies: 1
    Last Post: 02-07-2010, 03:54 PM

Posting Permissions

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