JDK-4346610 : Adding JSeparator to JToolBar "pushes" buttons added after separator to edge
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.2.2,1.4.2
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: windows_nt,windows_xp
  • CPU: x86
  • Submitted: 2000-06-19
  • Updated: 2023-08-21
  • Resolved: 2023-08-16
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 22
22 b11Fixed
Related Reports
Duplicate :  
Description
Name: jk109818			Date: 06/19/2000


Classic VM (build JDK-1.2.2-W, native threads, symcjit)

Adding buttons in a JToolBar after a JSeparator will push the button to the far
right (for a toolbar anchored at the top).

I have tried may different methods (including overriding the LayoutManager for
JToolBar, adding a Box.createGlue() after the separator), but nothing "logical"
keeps the buttons added after the separator next to the separator.

Here is a source file showing the behavior.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ToolBarTest
{

	ToolBarTest()
	{
		JFrame frame = new JFrame("Troy's ToolBarTest");
		
		frame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent we)
			{
				System.exit(0);
			}
		});
		
		JToolBar toolBar = buildToolBar();
		frame.getContentPane().setLayout(new BorderLayout());
		frame.getContentPane().add(toolBar, BorderLayout.NORTH);
		frame.getContentPane().add(new JPanel(), BorderLayout.CENTER);
		frame.setSize(400,200);
		frame.show();
	}

	JToolBar buildToolBar()
	{
		JToolBar toolBar = new JToolBar();
		toolBar.add(new JButton("button 1"));
		toolBar.add(new JButton("button 2"));
		toolBar.add(new JSeparator(SwingConstants.VERTICAL));
		toolBar.add(new JButton("button 3"));
		
		return toolBar;
	}

	public static void main(String[] args)
	{
		ToolBarTest test = new ToolBarTest();
	}
}
(Review ID: 105252) 
======================================================================

Comments
Changeset: 2bd2faeb Author: Prasanta Sadhukhan <psadhukhan@openjdk.org> Date: 2023-08-16 05:35:40 +0000 URL: https://git.openjdk.org/jdk/commit/2bd2faeb7632703192ff8f58db5e58cfd0dfe120
16-08-2023

A pull request was submitted for review. URL: https://git.openjdk.org/jdk/pull/15054 Date: 2023-07-27 14:30:23 +0000
27-07-2023

EVALUATION Low priority bug - decommitted from JDK7
26-01-2011

EVALUATION Contribution forum : https://jdk-collaboration.dev.java.net/servlets/ProjectForumMessageView?forumID=1463&messageID=13394
08-06-2006

EVALUATION JToolBar uses a BoxLayout, which divides free space between components that are smaller than their maximum size. The problem is that BasicButtonUI defines maximum size == preferred size, while BasicSeparatorUI does not define a maximum size. This leads to the separator getting all the extra width in this test case. leif.samuelsson@Eng 2000-12-19 Name: apR10133 Date: 08/27/2001 We can limit the separator's maximum size to the preferred size of corresponding orientation (i.e. width for VERTICAL and height for HORIZONTAL). ###@###.### ======================================================================
25-09-2004

SUGGESTED FIX Name: apR10133 Date: 08/27/2001 ------- BasicSeparatorUI.java ------- *** /tmp/d_Kaasc Mon Aug 27 18:47:43 2001 --- BasicSeparatorUI.java Mon Aug 27 18:39:36 2001 *************** *** 99,105 **** } public Dimension getMinimumSize( JComponent c ) { return null; } ! public Dimension getMaximumSize( JComponent c ) { return null; } } --- 99,112 ---- } public Dimension getMinimumSize( JComponent c ) { return null; } ! ! public Dimension getMaximumSize( JComponent c ) { ! Dimension d = getPreferredSize(c); ! if ( ((JSeparator)c).getOrientation() == JSeparator.VERTICAL ) ! return new Dimension(d.width, Integer.MAX_VALUE); ! else ! return new Dimension(Integer.MAX_VALUE, d.height); ! } } ###@###.### ======================================================================
25-09-2004

WORK AROUND Name: jk109818 Date: 06/19/2000 I have been able to get around this by adding the toolbar to the top using GridBagLayout and setting the fill value to NONE. However this is not ideal. ====================================================================== Antother workaround is to subclass the JSeparator and define a maximum width: toolBar.add(new JSeparator(SwingConstants.VERTICAL) { public Dimension getMaximumSize() { return new Dimension(getPreferredSize().width, Integer.MAX_VALUE); } }); Better yet is to use JToolBar.addSeparator() instead. This method creates a separator with correct maximum size. leif.samuelsson@Eng 2000-12-19
19-12-2000