Java Tutorial/SWT/CTabFolder — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 15:21, 31 мая 2010

Add CTabFolder2Adapter to CTabFolder

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabFolder2Adapter;
import org.eclipse.swt.custom.CTabFolderEvent;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class CTabFolderCTabFolder2Adapter {
public static void main (String [] args) {
  Display display = new Display ();
  final Shell shell = new Shell (display);
  shell.setLayout(new GridLayout());
  final CTabFolder folder = new CTabFolder(shell, SWT.BORDER);
  folder.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
  
  folder.setMinimizeVisible(true);
  folder.setMaximizeVisible(true);
  
  for (int i = 0; i < 4; i++) {
    CTabItem item = new CTabItem(folder, SWT.CLOSE);
    item.setText("Item "+i);
    Text text = new Text(folder, SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL);
    text.setText("Text for item "+i);
    item.setControl(text);
  }
  folder.addCTabFolder2Listener(new CTabFolder2Adapter() {
    public void minimize(CTabFolderEvent event) {
      System.out.println("min");
      folder.setMinimized(true);
      shell.layout(true);
    }
    public void maximize(CTabFolderEvent event) {
      System.out.println("max");
      folder.setMaximized(true);
      folder.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
      shell.layout(true);
    }
    public void restore(CTabFolderEvent event) {
      System.out.println("restore");
      folder.setMinimized(false);
      folder.setMaximized(false);
      folder.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
      shell.layout(true);
    }
  });
  
  
  
  shell.setSize(300, 300);
  shell.open ();
  while (!shell.isDisposed ()) {
    if (!display.readAndDispatch ()) display.sleep ();
  }
  display.dispose ();
}
}





Add CTabFolder2Listener

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabFolder2Listener;
import org.eclipse.swt.custom.CTabFolderEvent;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class CTabFolderCTabFolder2Listener {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Show CTabFolder");
    shell.setLayout(new GridLayout(1, true));
    CTabFolder tabFolder = new CTabFolder(shell, SWT.TOP);
    tabFolder.setBorderVisible(true);
    tabFolder.setLayoutData(new GridData(GridData.FILL_BOTH));
    // Set up a gradient background for the selected tab
    tabFolder.setSelectionBackground(new Color[] {
        display.getSystemColor(SWT.COLOR_WIDGET_DARK_SHADOW),
        display.getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW),
        display.getSystemColor(SWT.COLOR_WIDGET_LIGHT_SHADOW) }, new int[] { 50, 100 });
    new CTabItem(tabFolder, SWT.NONE, 0).setText("Tab index 1 ");
    new CTabItem(tabFolder, SWT.NONE, 1).setText("Tab index 1 ");
    tabFolder.addCTabFolder2Listener(new CTabFolder2Listener() {
      public void close(CTabFolderEvent arg0) {
        System.out.println("close");
      }
      public void minimize(CTabFolderEvent arg0) {
        // TODO Auto-generated method stub
      }
      public void maximize(CTabFolderEvent arg0) {
        // TODO Auto-generated method stub
      }
      public void restore(CTabFolderEvent arg0) {
        // TODO Auto-generated method stub
      }
      public void showList(CTabFolderEvent arg0) {
        // TODO Auto-generated method stub
      }
    });
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





Add CTabItem to CTabFolder

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class CTabFolderAddCTabItem {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    CTabFolder folder = new CTabFolder(shell, SWT.BORDER);
    for (int i = 0; i < 4; i++) {
      CTabItem item = new CTabItem(folder, SWT.CLOSE);
      item.setText("Item " + i);
      Text text = new Text(folder, SWT.MULTI);
      text.setText("Content for Item " + i);
      item.setControl(text);
    }
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





Add Image to CTabFolder

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabFolder2Adapter;
import org.eclipse.swt.custom.CTabFolderEvent;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class CTabFolderAddImage {
public static void main (String [] args) {
  Display display = new Display ();
  Image image = new Image(display, "yourFile.gif");
  final Shell shell = new Shell (display);
  shell.setLayout(new GridLayout());
  final CTabFolder folder = new CTabFolder(shell, SWT.BORDER);
  folder.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
  folder.setSimple(false);
  folder.setUnselectedImageVisible(false);
  folder.setUnselectedCloseVisible(false);
  
  for (int i = 0; i < 8; i++) {
    CTabItem item = new CTabItem(folder, SWT.CLOSE);
    item.setText("Item "+i);
    item.setImage(image);
    Text text = new Text(folder, SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL);
    text.setText("Text for item "+i);
    item.setControl(text);
  }
  
  shell.setSize(300, 300);
  shell.open ();
  while (!shell.isDisposed ()) {
    if (!display.readAndDispatch ()) display.sleep ();
  }
  image.dispose();
  display.dispose ();
}
}





Creating a CTabFolder

Create a CTabFolder by passing a parent and a style to the constructor:



CTabFolder(Composite parent, int style)



CTabFolder Style Constants

StyleDescriptionSWT.TOPDisplays the children tabs along the top edge of this CTabFolder.SWT.BOTTOMDisplays the children tabs along the bottom edge of this CTabFolder.SWT.FLATIf borders are visible, displays the children tabs with a flat look. If SWT.FLAT isn"t specified and borders are visible, displays the children tabs with a three-dimensional look. Also, displays any scrolling controls with a flat look.

You can combine SWT.FLAT with either SWT.TOP or SWT.BOTTOM using the bitwise OR operator,


Displaying a Gradient Background

You can configure the selected tab to display a gradient background using CTabFolder"s setSelectionBackground() method.



import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class CTabFolderGradientBackground {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Show CTabFolder");
    shell.setLayout(new GridLayout(1, true));
    CTabFolder tabFolder = new CTabFolder(shell, SWT.TOP);
    tabFolder.setBorderVisible(true);
    tabFolder.setLayoutData(new GridData(GridData.FILL_BOTH));
    // Set up a gradient background for the selected tab
    tabFolder.setSelectionBackground(new Color[] {
        display.getSystemColor(SWT.COLOR_WIDGET_DARK_SHADOW),
        display.getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW),
        display.getSystemColor(SWT.COLOR_WIDGET_LIGHT_SHADOW) }, new int[] { 50, 100 });
    new CTabItem(tabFolder, SWT.NONE, 0).setText("Tab index 1 ");
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





Set CTabFolder Minimize and Maximize Visible

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class CTabFolderMinMax {
public static void main (String [] args) {
  Display display = new Display ();
  final Shell shell = new Shell (display);
  shell.setLayout(new GridLayout());
  final CTabFolder folder = new CTabFolder(shell, SWT.BORDER);
  folder.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
  folder.setSimple(false);
  folder.setMinimizeVisible(true);
  folder.setMaximizeVisible(true);
  
  for (int i = 0; i < 8; i++) {
    CTabItem item = new CTabItem(folder, SWT.CLOSE);
    item.setText("Item "+i);
    Text text = new Text(folder, SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL);
    text.setText("Text for item "+i);
    item.setControl(text);
  }
  
  shell.setSize(300, 300);
  shell.open ();
  while (!shell.isDisposed ()) {
    if (!display.readAndDispatch ()) display.sleep ();
  }
  display.dispose ();
}
}





TabFolder/TabItem vs. CTabFolder/CTabItem

CTabFolder and CTabItem add flexibility to the standard tabs.



Description                                   TabFolder/TabItem      CTabFolder/CTabItem
Tab position                                  On top or on bottom    On top or on bottom
Supports text                                 Yes                    Yes
Supports tool tips                            Yes                    Yes
Supports images                               Yes                    Yes
Supports disabled images                      No                     Yes
Supports flat look                            No                     Yes
Supports customizable margins                 No                     Yes
Supports a control in the top-right corner    No                     Yes
Supports a gradient background                No                     Yes
Supports an image background                  No                     Yes