You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

禁用SWT自动缩放后,如何调整PreferenceDialog中TreeViewer宽度?

问题描述

由于Eclipse4在高DPI显示器上缩放图片存在bug,我通过以下代码禁用了缩放:

System.setProperty("swt.autoScale", "false");

对应的完整启动类代码:

public class Application implements IApplication
{
  private static final String ProcessName = "myName.exe";

  /* (non-Javadoc)
   * @see org.eclipse.equinox.app.IApplication#start(org.eclipse.equinox.app.IApplicationContext)
   */
  public Object start(IApplicationContext context) throws Exception
  {
    OpenDocumentEventProcessor openDocProcessor = new OpenDocumentEventProcessor();
          
    // Eclipse4在高DPI显示器缩放图片存在bug
    System.setProperty("swt.autoScale", "false");
    
    Display display = PlatformUI.createDisplay();
    display.addListener(SWT.OpenDocument, openDocProcessor);
    display.addListener(SWT.OpenUrl, openDocProcessor);

    //other code
  }
}

执行上述设置后,创建首选项页面时内容区域过窄,出现水平滚动条。创建PreferenceDialog的代码如下:

public class SettingsToolItem
{
  // some code

  @Execute
  public void execute()
  {
    try
    {
      String openPreferencesID = lastSelectedPageID;

      // some code

      Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();

      PreferenceDialog prefDialog = PreferencesUtil.createPreferenceDialogOn(shell, openPreferencesID, null, null);

      prefDialog.getTreeViewer().expandToLevel(2);

      prefDialog.getTreeViewer().addSelectionChangedListener(new ISelectionChangedListener()
      {

        @Override
        public void selectionChanged(SelectionChangedEvent event)
        {
          IStructuredSelection selection = (IStructuredSelection) prefDialog.getTreeViewer().getSelection();
          if (selection.size() == 1)
          {
            IPreferenceNode node = (IPreferenceNode) selection.getFirstElement();
            lastSelectedPageID = node.getId();
          }
        }
      });

      prefDialog.open();
    } catch (Exception e)
    {
      e.printStackTrace();
    }
  }

  // some code
}

我尝试过以下方法,但均无效果:

  • 设置Tree的尺寸:
TreeViewer tV = prefDialog.getTreeViewer();
Tree tree = tV.getTree();
tree.setSize(300, 800);
tree.redraw();
tree.layout();
  • 调整对话框第一个子控件(Tree)的宽度
  • 遍历子控件并设置其计算出的默认尺寸:
for (Control c0 : children)
{
    Point c0_cS = c0.computeSize(SWT.DEFAULT, SWT.DEFAULT);
    c0.setSize(c0_cS);
    c0.redraw();
    if (c0 instanceof Composite)
      ((Composite) c0).layout();

}

另外,org.eclipse.jface.preference.PreferenceDialog.lastTreeWidth的默认值为180,但我无法覆盖该值。请问该如何放大这个Composite?


内容的提问来源于stack exchange,提问作者know

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 15:13:20