examplegui.java
来自「java的swt图形程序」· Java 代码 · 共 2,141 行 · 第 1/5 页
JAVA
2,141 行
/*
de.kupzog.ktable.* Version 1.4
_______________________________________________________________________
This is the source of a custom table class for Java SWT applications.
The original version of this table was written by Konstantin Scheglov.
KTable is a complete new implementation but uses the structure and
some code fragments from the old version.
The features of this table implementation in short terms:
- a table model provides the data for the table (comparable
to the Swing table model)
- cell rendering is done by extern classes and thus can easily
be changed to any rendering one could imagine...
- Columns and rows can be resized by mouse. Note that all rows
except the first row will have the same size.
- There can be horizontal and vertical headers (fixed cells)
as many as needed.
- Different selection modes are available
- In place editing is possibel by using editor classes that
can easily be adjusted to special needs.
For a detailed function description refer to the api documentation that
is included in the sourcefiles.
For examples how to use KTable, see the "ExampleGUI" class. You can
run this class and see different examples of KTables.
Text Table Shows a Table with 1,000,000 rows and 100 columns.
The use of cell editors is shown.
You can resize rows and columns.
Selection Listeners are used (see the console output).
Color Palette Here you can see that a table does not have to
look like a table...
See how the cell renderer is implemented and
what the table model does.
Towns This example shows how images can be included in
table cells together with text. It also shows the use
of the multi line cell editor.
The author welcomes any feedback: fkmk@kupzog.de
Changes in Version 1.1 compared to previous version:
- empty table without header looks better now
- right-click on cell also changes the selection
Changes in Version 1.2
- License changed from GPL to LGPL
- Table does no longer throw NullPointerException if no model is set.
- minor other bug fixes
Changes in Version 1.3
- License changed from LGPL to Eclipse Public License (EPL) (what will be next?)
- Keyboard access for combo editors added (thanks to Larry Moore)
- minor other bug fixes
Changed in Version 1.4
- FLAT style allows more border flexibility: if specified,
no border lines will be drawn.
- Click on fixed cells now also changes the row selection
(only in row selection mode with cells on the left border)
- bug fixes thanks to Chris Grant
*/
/*******************************************************************************
* Copyright (C) 2004 by Friederich Kupzog Elektronik & Software
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Friederich Kupzog - initial API and implementation
* fkmk@kupzog.de
* www.kupzog.de/fkmk
*******************************************************************************/
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTException;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.TraverseEvent;
import org.eclipse.swt.events.TraverseListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ScrollBar;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Text;
/**
* KTable example GUI
*
*
*/
public class ExampleGUI {
public static void main(String[] args) {
// create a shell...
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
shell.setText("KTable examples");
// put a tab folder in it...
TabFolder tabFolder = new TabFolder(shell, SWT.NONE);
// Item 1: a Text Table
TabItem item1 = new TabItem(tabFolder, SWT.NONE);
item1.setText("Text Table");
Composite comp1 = new Composite(tabFolder, SWT.NONE);
item1.setControl(comp1);
comp1.setLayout(new FillLayout());
// put a table in tabItem1...
KTable table = new KTable(comp1, SWT.V_SCROLL | SWT.H_SCROLL);
// table.setRowSelectionMode(true);
table.setMultiSelectionMode(true);
table.setModel(new KTableModelExample());
table.addCellSelectionListener(new KTableCellSelectionListener() {
public void cellSelected(int col, int row, int statemask) {
System.out.println("Cell [" + col + ";" + row + "] selected.");
}
public void fixedCellSelected(int col, int row, int statemask) {
System.out
.println("Header [" + col + ";" + row + "] selected.");
}
});
table.addCellResizeListener(new KTableCellResizeListener() {
public void columnResized(int col, int newWidth) {
System.out.println("Column " + col + " resized to " + newWidth);
}
public void rowResized(int newHeight) {
System.out.println("Rows resized to " + newHeight);
}
});
// Item 2: a Color Palette
TabItem item2 = new TabItem(tabFolder, SWT.NONE);
item2.setText("Color Palette");
Composite comp2 = new Composite(tabFolder, SWT.NONE);
item2.setControl(comp2);
comp2.setLayout(new FillLayout());
// put a table in tabItem2...
final KTable table2 = new KTable(comp2, SWT.NONE);
table2.setModel(new PaletteExampleModel());
table2.setRowSelectionMode(false);
table2.setMultiSelectionMode(false);
final Label label = new Label(comp2, SWT.NONE);
label.setText("Click a cell...");
table2.addCellSelectionListener(new KTableCellSelectionAdapter() {
public void cellSelected(int col, int row, int statemask) {
RGB rgb = (RGB) table2.getModel().getContentAt(col, row);
label.setText("R: " + rgb.red + "\nG: " + rgb.green + "\nB: "
+ rgb.blue);
}
});
// Item 3: Town table
TabItem item3 = new TabItem(tabFolder, SWT.NONE);
item3.setText("Towns");
Composite comp3 = new Composite(tabFolder, SWT.NONE);
item3.setControl(comp3);
comp3.setLayout(new FillLayout());
// put a table in tabItem3...
final KTable table3 = new KTable(comp3, SWT.FLAT | SWT.H_SCROLL);
table3.setBackground(Display.getCurrent().getSystemColor(
SWT.COLOR_LIST_BACKGROUND));
table3.setModel(new TownExampleModel());
table3.setRowSelectionMode(true);
table3.setMultiSelectionMode(false);
// display the shell...
shell.setSize(600, 600);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
/*******************************************************************************
* Copyright (C) 2004 by Friederich Kupzog Elektronik & Software All rights
* reserved. This program and the accompanying materials are made available
* under the terms of the Eclipse Public License v1.0 which accompanies this
* distribution, and is available at http://www.eclipse.org/legal/epl-v10.html
*
* Contributors: Friederich Kupzog - initial API and implementation
* fkmk@kupzog.de www.kupzog.de/fkmk
******************************************************************************/
/**
* Custom drawn tabel widget for SWT GUIs.
*
*
* @see de.kupzog.ktable.KTableModel
* @see de.kupzog.ktable.KTableCellRenderer
* @see de.kupzog.ktable.KTableCellEditor
* @see de.kupzog.ktable.KTableCellSelectionListener
*
* The idea of KTable is to have a flexible grid of cells to display data in it.
* The class focuses on displaying data and not on collecting the data to
* display. The latter is done by the KTableModel which has to be implemented
* for each specific case. The table asks the table model for the amount of
* columns and rows, the sizes of columns and rows and for the content of the
* cells which are currently drawn. Even if the table has a million rows, it
* won't get slower because it only requests those cells it currently draws.
* Only a bad table model can influence the drawing speed negatively.
*
* When drawing a cell, the table calls a KTableCellRenderer to do this work.
* The table model determines which cell renderer is used for which cell. A
* default renderer is available (KTableCellRenderer.defaultRenderer), but the
* creation of self-written renderers for specific purposes is assumed.
*
* KTable allows to resize columns and rows. Each column can have an individual
* size while the rows are all of the same height except the first row. Multiple
* column and row headers are possible. These "fixed" cells will not be scrolled
* out of sight. The column and row count always starts in the upper left corner
* with 0, independent of the number of column headers or row headers.
*
* @author Friederich Kupzog
*
*/
class KTable extends Canvas {
// Daten und Datendarstellung
protected KTableModel m_Model;
protected KTableCellEditor m_CellEditor;
// aktuelle Ansicht
protected int m_TopRow;
protected int m_LeftColumn;
// Selection
protected boolean m_RowSelectionMode;
protected boolean m_MultiSelectMode;
protected HashMap m_Selection;
protected int m_FocusRow;
protected int m_FocusCol;
protected int m_ClickColumnIndex;
protected int m_ClickRowIndex;
// wichtige MaBe
protected int m_RowsVisible;
protected int m_RowsFullyVisible;
protected int m_ColumnsVisible;
protected int m_ColumnsFullyVisible;
// SpaltengroBe
protected int m_ResizeColumnIndex;
protected int m_ResizeColumnLeft;
protected int m_ResizeRowIndex;
protected int m_ResizeRowTop;
protected int m_NewRowSize;
protected boolean m_Capture;
protected Image m_LineRestore;
protected int m_LineX;
protected int m_LineY;
// sonstige
protected GC m_GC;
protected Display m_Display;
protected ArrayList cellSelectionListeners;
protected ArrayList cellResizeListeners;
protected boolean flatStyleSpecified;
// ////////////////////////////////////////////////////////////////////////////
// KONSTRUKTOR
// ////////////////////////////////////////////////////////////////////////////
/**
* Creates a new KTable.
*
* possible styles: SWT.V_SCROLL - show vertical scrollbar and allow
* vertical scrolling by arrow keys SWT.H_SCROLL - show horizontal scrollbar
* and allow horizontal scrolling by arrow keys SWT.FLAT - no border
* drawing.
*
* After creation a table model should be added using setModel().
*/
public KTable(Composite parent, int style) {
// Oberklasse initialisieren
super(parent, SWT.NO_BACKGROUND | SWT.NO_REDRAW_RESIZE | style);
// inits
m_GC = new GC(this);
m_Display = Display.getCurrent();
m_Selection = new HashMap();
m_CellEditor = null;
flatStyleSpecified = ((style | SWT.FLAT) == style);
m_RowSelectionMode = false;
m_MultiSelectMode = false;
m_TopRow = 0;
m_LeftColumn = 0;
m_FocusRow = 0;
m_FocusCol = 0;
m_RowsVisible = 0;
m_RowsFullyVisible = 0;
m_ColumnsVisible = 0;
m_ColumnsFullyVisible = 0;
m_ResizeColumnIndex = -1;
m_ResizeRowIndex = -1;
m_ResizeRowTop = -1;
m_NewRowSize = -1;
m_ResizeColumnLeft = -1;
m_Capture = false;
m_ClickColumnIndex = -1;
m_ClickRowIndex = -1;
m_LineRestore = null;
m_LineX = 0;
m_LineY = 0;
cellSelectionListeners = new ArrayList(10);
cellResizeListeners = new ArrayList(10);
// Listener
createListeners();
}
protected void createListeners() {
addPaintListener(new PaintListener() {
public void paintControl(PaintEvent event) {
onPaint(event);
}
});
addControlListener(new ControlAdapter() {
public void controlResized(ControlEvent e) {
redraw();
}
});
addMouseListener(new MouseAdapter() {
public void mouseDown(MouseEvent e) {
onMouseDown(e);
}
public void mouseUp(MouseEvent e) {
onMouseUp(e);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?