Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Toolbar max width

  1. #1
    Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    479
    Thanks
    0
    Thanked 92 Times in 77 Posts

    Default Toolbar max width

    Hi there.

    I am really struggling with a toolbar right now. No matter what I try it just wont accept its maximum width.

    This is how it is build:
    1) JToolBar (flow layout)
    2) A JTabbedPane within the JToolBar
    3) A JPanel (border layout) as a tab within the JTabbedPane
    4) A JScrollPane within the JPanel (center)
    5) A JList within the viewport view of the JScrollPane

    I tried to set the MaximumSize of any single or all of these components. It never worked.
    No matter what I did, the toolbar always was the size of the biggest item in the JList.

    Here are some screenshots with the code used to create them:
    toolbar1.jpg
    		JToolBar toolBar = new JToolBar();
    		add(toolBar, BorderLayout.EAST);
     
    		JTabbedPane toolBarPnl = new JTabbedPane(JTabbedPane.TOP);
    		toolBar.add(toolBarPnl);

    toolbar2.jpg
    		JToolBar toolBar = new JToolBar();
    		add(toolBar, BorderLayout.EAST);
     
    		JTabbedPane toolBarPnl = new JTabbedPane(JTabbedPane.TOP);
    		toolBarPnl.setMaximumSize(new Dimension(256, Integer.MAX_VALUE));
    		toolBar.add(toolBarPnl);
    Notice that this time the JScrollPane is indeed capped at 256 pixels in width, but the toolbar itself is still growing as large as the biggest item in the JList.

    I would really appreciate if somebody could tell me what I need to do to keep the toolbar from growing in width.



    On a sidenote, I would also like to ask if anybody could tell me how to make a JToolBar resizable.

    Thank you all very much.


  2. #2
    Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    479
    Thanks
    0
    Thanked 92 Times in 77 Posts

    Default Re: Toolbar max width

    I have actually made a small test program which shows my problem:
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.EventQueue;
     
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    import javax.swing.DefaultListModel;
    import javax.swing.JToolBar;
    import javax.swing.JTabbedPane;
    import javax.swing.JScrollPane;
    import javax.swing.JList;
     
    public class Test extends JFrame {
    	private static final long serialVersionUID = 1L;
     
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					Test frame = new Test();
    					frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
     
    	private static final Dimension MAX_WIDTH_256 = new Dimension(256, Integer.MAX_VALUE);
     
    	public Test() {
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 450, 300);
    		JPanel contentPane = new JPanel();
    		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		contentPane.setLayout(new BorderLayout(0, 0));
    		setContentPane(contentPane);
     
    		JToolBar toolBar = new JToolBar();
    		toolBar.setMaximumSize(MAX_WIDTH_256);
    		contentPane.add(toolBar, BorderLayout.EAST);
     
    		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
    		tabbedPane.setMaximumSize(MAX_WIDTH_256);
    		toolBar.add(tabbedPane);
     
    		JPanel tab = new JPanel();
    		tab.setLayout(new BorderLayout(0, 0));
    		tab.setMaximumSize(MAX_WIDTH_256);
    		tabbedPane.addTab("Some Strings", null, tab, null);
     
    		JScrollPane scrollPane = new JScrollPane();
    		scrollPane.setMaximumSize(MAX_WIDTH_256);
    		tab.add(scrollPane, BorderLayout.CENTER);
     
    		DefaultListModel<String> listModel = new DefaultListModel<String>();
    		for (String str : new String[] {
    				"Small",
    				"Bigger",
    				"Even Bigger",
    				"Already quite Big",
    				"This one is really really big, it has an insane size and showing it in a list would be a really stupid idea!",
    		}) {
    			listModel.addElement(str);
    		}
     
    		JList<String> list = new JList<>();
    		list.setMaximumSize(MAX_WIDTH_256);
    		list.setModel(listModel);
    		scrollPane.setViewportView(list);
     
    		JPanel panel = new JPanel();
    		panel.setPreferredSize(new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE));
    		contentPane.add(panel, BorderLayout.CENTER);
    	}
     
    }
    If anybody could make the toolbar accept its maximum size I would really appreciate.

Similar Threads

  1. Replies: 2
    Last Post: March 6th, 2014, 04:35 AM
  2. Can't adjust width of the X.
    By Kris45 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 9th, 2013, 06:12 PM
  3. JAVA TaskBar toolbar integration
    By WilyZ in forum Java Theory & Questions
    Replies: 1
    Last Post: October 27th, 2012, 04:15 PM
  4. Multi-D array. daily max/min/avg, weekly max/min/avg, sum total (99% done)
    By TheWhopper858 in forum Collections and Generics
    Replies: 1
    Last Post: November 6th, 2011, 07:50 PM
  5. Java program to Add a JMenu toolbar to a Java Swing application
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 3
    Last Post: April 30th, 2010, 09:00 AM