Question about custom Swing widgets

Swing Designer allows you to quickly create the frames, panels, dialogs, applets and other UI elements that comprise Java Swing applications.

Moderators: Konstantin.Scheglov, gnebling, Alexander.Mitin, jwren, Eric Clayberg

Question about custom Swing widgets

Postby Andreas » Mon Oct 25, 2004 3:09 am

I have a build custom Swing widget that is a panel with a title bar. The custom widget generaly consists of three parts: the base JPanel (BorderLayout) a JPanel as the title panel (added "North" to base panel) and a 'content' JPanel (added "Center" to base panel) supposed to hold whatever is the user wants to put in the custom widget. Here my question: How can I make the Swing designer only recognize the 'content' panel when setting the layouts or adding controls?

Regards,
Andreas
Andreas
 

Re: Question about custom Swing widgets

Postby Eric Clayberg » Mon Oct 25, 2004 1:50 pm

Andreas wrote:I have a build custom Swing widget that is a panel with a title bar. The custom widget generaly consists of three parts: the base JPanel (BorderLayout) a JPanel as the title panel (added "North" to base panel) and a 'content' JPanel (added "Center" to base panel) supposed to hold whatever is the user wants to put in the custom widget. Here my question: How can I make the Swing designer only recognize the 'content' panel when setting the layouts or adding controls?

You can't. Designer does not currently support custom nested containers like that. Designer supports simple custom containers (e.g., that have no predefined children) or complex, custom panels as self-contained, encapsulated units. We don't allow complex panels to have additional children added to them.
Eric Clayberg
Software Engineering Manager
Google
http://code.google.com/webtoolkit/download.html

Author: "Eclipse Plug-ins"
http://www.qualityeclipse.com
Eric Clayberg
Moderator
 
Posts: 4503
Joined: Tue Sep 30, 2003 6:39 am
Location: Boston, MA USA

Postby Guest » Tue Oct 26, 2004 4:49 am

Are there plans to add such a functionality in the future?
Guest
 

Postby Eric Clayberg » Mon Nov 01, 2004 11:28 am

Anonymous wrote:Are there plans to add such a functionality in the future?

Yes. In fact, this is now available in the latest v2.1.1 build. For example, here is an example TitlePanel class...

Code: Select all
package com.swtdesigner.testcases.swing;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingConstants;

public class TitlePanel extends JPanel {
   private JTabbedPane m_TabbedPane;
   private static final long serialVersionUID = 1307772213100636561L;
   private JPanel m_Content;
   private JLabel m_Label;
   public TitlePanel() {
      super();
      setBackground(Color.YELLOW);
      setLayout(new BorderLayout());
      {
         m_Label = new JLabel();
         m_Label.setOpaque(true);
         m_Label.setBackground(Color.ORANGE);
         m_Label.setHorizontalAlignment(SwingConstants.CENTER);
         add(m_Label, BorderLayout.NORTH);
         m_Label.setText("New JLabel");
      }
      {
         m_Content = new JPanel();
         m_Content.setLayout(new GridBagLayout());
         add(m_Content, BorderLayout.CENTER);
      }
   }
   public String getTitle() {
      return m_Label.getText();
   }
   public void setTitle(String title) {
      m_Label.setText(title);
   }
   public JPanel getContent() {
      return m_Content;
   }
}

Note that the inner panel is exposed via a public accessor. The text on the embedded label is also exposed by public accessors.
    Image
We can then use this in another class...

Code: Select all
package com.swtdesigner.testcases.swing;

import javax.swing.JFrame;
import com.swtdesigner.testcases.swing.TitlePanel;
import java.awt.BorderLayout;
import javax.swing.JButton;

public class TitlePanelTestCase extends JFrame {

    public static void main(String args[]) {
        TitlePanelTestCase frame = new TitlePanelTestCase();
        frame.setBounds(100, 100, 414, 326);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
    public TitlePanelTestCase() {
        super();

        final TitlePanel titlePanel = new TitlePanel();
        titlePanel.getContent().setLayout(null);

        final JButton button = new JButton();
        button.setBounds(35, 35, 100, 65);
        titlePanel.getContent().add(button);
        button.setText("New JButton");

        final JButton button_1 = new JButton();
        button_1.setBounds(270, 190, 110, 70);
        titlePanel.getContent().add(button_1);
        button_1.setText("New JButton");
        titlePanel.setTitle("This is a new title");
        getContentPane().add(titlePanel, BorderLayout.CENTER);
    }
}

We can add widgets to the inner container and even change the label on the embedded label...
    Image
Eric Clayberg
Software Engineering Manager
Google
http://code.google.com/webtoolkit/download.html

Author: "Eclipse Plug-ins"
http://www.qualityeclipse.com
Eric Clayberg
Moderator
 
Posts: 4503
Joined: Tue Sep 30, 2003 6:39 am
Location: Boston, MA USA


Return to Swing Designer

Who is online

Users browsing this forum: Google [Bot] and 1 guest