Forum Stats

  • 3,728,682 Users
  • 2,245,675 Discussions
  • 7,853,700 Comments

Discussions

Any layout suggestions for this?

843804
843804 Member Posts: 45,751
edited June 2005 in Swing
How can I place a JCheckBox and JButton components on the area beside the TabbedPane tabs (Top right corner position)?

Final design should be like this image
http://www.rejimani.com/backup/tabbed.jpg

Any suggestions?

Reji Mani

Comments

  • 843804
    843804 Member Posts: 45,751
    import javax.swing.*;
    import java.awt.*;
    
    /**
     * <dl>
     * <dt><b> Creation date :</b></dt>
     * <dd> 15 juin 2005 </dd>
     * </dl>
     *
     * @author weebib
     */
    public class WeirdLayoutTest extends JPanel {
    	private JTabbedPane theTabbedPane;
    	private JPanel theLittlePanel;
    
    	private class WeirdLayout implements LayoutManager {
    		public void removeLayoutComponent(Component comp) {}
    
    		public void addLayoutComponent(String name, Component comp) {}
    
    		public void layoutContainer(Container parent) {
    			Insets insets = parent.getInsets();
    			int availableWidth = parent.getWidth() - insets.left - insets.right;
    			int availableHeight = parent.getHeight() - insets.top - insets.bottom;
    
    			Dimension littlePanelPrefDim = theLittlePanel.getPreferredSize();
    			theLittlePanel.setBounds(insets.left + availableWidth - littlePanelPrefDim.width,
    									 insets.top,
    									 littlePanelPrefDim.width,
    									 littlePanelPrefDim.height);
    
    			int tabHeight = getTabsHeight();
    			int delta = theLittlePanel.getHeight() - tabHeight;
    			if (delta < 0) {
    				theTabbedPane.setBounds(insets.left, insets.top, availableWidth, availableHeight);
    			} else {
    				theTabbedPane.setBounds(insets.left, insets.top + delta, availableWidth, availableHeight - delta);
    			}
    		}
    
    		private int getTabsHeight() {
    			int tabCount = theTabbedPane.getTabCount();
    			int height = 0;
    			for (int i = 0; i < tabCount; i++) {
    				height = Math.max(height, theTabbedPane.getBoundsAt(i).height);
    			}
    			return Math.max(height, 0);
    		}
    
    		private int getTabsWidth() {
    			int tabCount = theTabbedPane.getTabCount();
    			int width = 0;
    			for (int i = 0; i < tabCount; i++) {
    				width += theTabbedPane.getBoundsAt(i).width;
    			}
    			return Math.max(width, 0);
    		}
    
    		public Dimension minimumLayoutSize(Container parent) {
    			Insets insets = parent.getInsets();
    			Dimension littlePanelMinDim = theLittlePanel.getMinimumSize();
    			Dimension tabbedPaneMinDim = theTabbedPane.getMinimumSize();
    			return new Dimension(Math.max(littlePanelMinDim.width, tabbedPaneMinDim.width) + insets.left + insets.right,
    								 Math.max(littlePanelMinDim.height, tabbedPaneMinDim.height) + insets.top + insets.bottom);
    		}
    
    		public Dimension preferredLayoutSize(Container parent) {
    			Insets insets = parent.getInsets();
    			Dimension littlePanelPrefDim = theLittlePanel.getPreferredSize();
    			int tabsWidth = getTabsWidth();
    			Dimension tabbedPanePrefDim = theTabbedPane.getPreferredSize();
    			return new Dimension(Math.max(littlePanelPrefDim.width + tabsWidth, tabbedPanePrefDim.width) + insets.left + insets.right,
    								 Math.max(littlePanelPrefDim.height, tabbedPanePrefDim.height) + insets.top + insets.bottom);
    		}
    	}
    
    	public WeirdLayoutTest() {
    		super();
    		setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    		setLayout(new WeirdLayout());
    		theLittlePanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
    		theLittlePanel.add(new JCheckBox("Check me"));
    		theLittlePanel.add(new JButton("Press me"));
    		add(theLittlePanel);
    		theTabbedPane = new JTabbedPane();
    		theTabbedPane.add("Pane1", new JLabel("Pane 1"));
    		theTabbedPane.add("Pane2", new JLabel("Pane 2"));
    		add(theTabbedPane);
    	}
    
    	public static void main(String[] args) {
    		final JFrame frame = new JFrame(WeirdLayoutTest.class.getName());
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setContentPane(new WeirdLayoutTest());
    		SwingUtilities.invokeLater(new Runnable() {
    			public void run() {
    				frame.pack();
    				frame.pack();
    				frame.show();
    			}
    		});
    	}
    }
    Of course, there is no need to create a LayoutManager for this. Using a JLayeredPane or a JPanel(null layout) could have worked as well.
  • camickr
    camickr Member Posts: 24,931
    I came across the OverlayLayout in the API. I don't really understand how it works, or if it will help in this situation, but I got a simple example to work:
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.plaf.*;
    
    public class TabbedPaneWithComponent extends JFrame
    {
    	public TabbedPaneWithComponent()
    	{
    		JPanel panel = new JPanel();
    		panel.setLayout( new OverlayLayout(panel) );
    		getContentPane().add(panel);
    
    		JTabbedPane tabbedPane = new JTabbedPane();
    		tabbedPane.add("1", new JTextField("one"));
    		tabbedPane.add("2", new JTextField("two"));
    		tabbedPane.setAlignmentX(1.0f);
    		tabbedPane.setAlignmentY(0.0f);
    
    		JCheckBox checkBox = new JCheckBox("Check Me");
    		checkBox.setAlignmentX(1.0f);
    		checkBox.setAlignmentY(0.0f);
    
    		panel.add( checkBox );
    		panel.add(tabbedPane);
    	}
    
    	public static void main(String args[])
    	{
    		TabbedPaneWithComponent frame = new TabbedPaneWithComponent();
    		frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
    		frame.pack();
    		frame.setLocationRelativeTo( null );
    		frame.setVisible(true);
    	}
    }
    I tried adding two components to a panel and then adding the panel to the container but I couldn't get it working.
  • 843804
    843804 Member Posts: 45,751
    Hi Weebib & Camickr

    Thanks for your suggestions... It works fine now..
  • 843804
    843804 Member Posts: 45,751
    /*
     * Test_AbsoluteLayout.java
     */
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class Test_AbsoluteLayout extends JFrame {
        public Test_AbsoluteLayout() {
            initComponents();
        }
        private void initComponents() {
            checkbox = new JCheckBox();
            button = new JButton();
            tabbedpane = new JTabbedPane();
            panel1 = new JPanel();
            panel2 = new JPanel();
            getContentPane().setLayout(new AbsoluteLayout());
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            checkbox.setPreferredSize(new Dimension(83, 21));
            checkbox.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    checkboxActionPerformed(evt);
                }
            });
            getContentPane().add(checkbox, new AbsoluteConstraints(350, 0, 20, -1));
            button.setText("OK");
            button.setMargin(new Insets(0, 0, 0, 0));
            button.setPreferredSize(new Dimension(71, 21));
            button.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    buttonActionPerformed(evt);
                }
            });
            getContentPane().add(button, new AbsoluteConstraints(370, 0, 30, -1));
            tabbedpane.setPreferredSize(new Dimension(400, 300));
            tabbedpane.addTab("tab1", panel1);
            tabbedpane.addTab("tab2", panel2);
            getContentPane().add(tabbedpane, new AbsoluteConstraints(0, 0, -1, -1));
            pack();
        }
        private void checkboxActionPerformed(ActionEvent evt) {
            System.out.println("check");
        }
        private void buttonActionPerformed(ActionEvent evt) {
            System.out.println("ok");
        }
        public static void main(String args[]) {
            new Test_AbsoluteLayout().setVisible(true);
        }
        private JButton button;
        private JCheckBox checkbox;
        private JPanel panel1;
        private JPanel panel2;
        private JTabbedPane tabbedpane;
    }
    class AbsoluteConstraints implements java.io.Serializable {
        static final long serialVersionUID = 5261460716622152494L;
        public int x;
        public int y;
        public int width = -1;
        public int height = -1;
        public AbsoluteConstraints(Point pos) {
            this(pos.x, pos.y);
        }
        public AbsoluteConstraints(int x, int y) {
            this.x = x;
            this.y = y;
        }
        public AbsoluteConstraints(Point pos, Dimension size) {
            this.x = pos.x;
            this.y = pos.y;
            if (size != null) {
                this.width = size.width;
                this.height = size.height;
            }
        }
        public AbsoluteConstraints(int x, int y, int width, int height) {
            this.x = x;
            this.y = y;
            this.width = width;
            this.height = height;
        }
        public int getX() {
            return x;
        }
        public int getY() {
            return y;
        }
        public int getWidth() {
            return width;
        }
        public int getHeight() {
            return height;
        }
        public String toString() {
            return super.toString() +" [x="+x+", y="+y+", width="+width+", height="+height+"]";
        }
    }
    class AbsoluteLayout implements LayoutManager2, java.io.Serializable {
        static final long serialVersionUID = -1919857869177070440L;
        public void addLayoutComponent(String name, Component comp) {
            throw new IllegalArgumentException();
        }
        public void removeLayoutComponent(Component comp) {
            constraints.remove(comp);
        }
        public Dimension preferredLayoutSize(Container parent) {
            int maxWidth = 0;
            int maxHeight = 0;
            for (java.util.Enumeration e = constraints.keys(); e.hasMoreElements();) {
                Component comp = (Component)e.nextElement();
                AbsoluteConstraints ac = (AbsoluteConstraints)constraints.get(comp);
                Dimension size = comp.getPreferredSize();
                int width = ac.getWidth();
                if (width == -1) width = size.width;
                int height = ac.getHeight();
                if (height == -1) height = size.height;
                if (ac.x + width > maxWidth)
                    maxWidth = ac.x + width;
                if (ac.y + height > maxHeight)
                    maxHeight = ac.y + height;
            }
            return new Dimension(maxWidth, maxHeight);
        }
        public Dimension minimumLayoutSize(Container parent) {
            int maxWidth = 0;
            int maxHeight = 0;
            for (java.util.Enumeration e = constraints.keys(); e.hasMoreElements();) {
                Component comp = (Component)e.nextElement();
                AbsoluteConstraints ac = (AbsoluteConstraints)constraints.get(comp);
                Dimension size = comp.getMinimumSize();
                int width = ac.getWidth();
                if (width == -1) width = size.width;
                int height = ac.getHeight();
                if (height == -1) height = size.height;
                if (ac.x + width > maxWidth)
                    maxWidth = ac.x + width;
                if (ac.y + height > maxHeight)
                    maxHeight = ac.y + height;
            }
            return new Dimension(maxWidth, maxHeight);
        }
        public void layoutContainer(Container parent) {
            for (java.util.Enumeration e = constraints.keys(); e.hasMoreElements();) {
                Component comp = (Component)e.nextElement();
                AbsoluteConstraints ac = (AbsoluteConstraints)constraints.get(comp);
                Dimension size = comp.getPreferredSize();
                int width = ac.getWidth();
                if (width == -1) width = size.width;
                int height = ac.getHeight();
                if (height == -1) height = size.height;
                comp.setBounds(ac.x, ac.y, width, height);
            }
        }
        public void addLayoutComponent(Component comp, Object constr) {
            if (!(constr instanceof AbsoluteConstraints))
                throw new IllegalArgumentException();
            constraints.put(comp, constr);
        }
        public Dimension maximumLayoutSize(Container target) {
            return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
        }
        public float getLayoutAlignmentX(Container target) {
            return 0;
        }
        public float getLayoutAlignmentY(Container target) {
            return 0;
        }
        public void invalidateLayout(Container target) {
        }
        protected java.util.Hashtable constraints = new java.util.Hashtable();
    }
This discussion has been closed.