|
|
@ -5,39 +5,36 @@ import com.fr.design.event.UIObserver; |
|
|
|
import com.fr.design.event.UIObserverListener; |
|
|
|
import com.fr.design.event.UIObserverListener; |
|
|
|
import com.fr.design.gui.ilable.UILabel; |
|
|
|
import com.fr.design.gui.ilable.UILabel; |
|
|
|
import com.fr.design.gui.itextfield.UITextField; |
|
|
|
import com.fr.design.gui.itextfield.UITextField; |
|
|
|
import com.fr.design.utils.gui.GUICoreUtils; |
|
|
|
import org.jetbrains.annotations.NotNull; |
|
|
|
|
|
|
|
|
|
|
|
import javax.swing.BorderFactory; |
|
|
|
import javax.swing.BorderFactory; |
|
|
|
import javax.swing.JComponent; |
|
|
|
import javax.swing.JComponent; |
|
|
|
import javax.swing.JFrame; |
|
|
|
|
|
|
|
import javax.swing.JPanel; |
|
|
|
|
|
|
|
import javax.swing.JTable; |
|
|
|
import javax.swing.JTable; |
|
|
|
import javax.swing.event.CellEditorListener; |
|
|
|
import javax.swing.event.CellEditorListener; |
|
|
|
import javax.swing.event.ChangeEvent; |
|
|
|
import javax.swing.event.ChangeEvent; |
|
|
|
import javax.swing.event.ChangeListener; |
|
|
|
import javax.swing.event.ChangeListener; |
|
|
|
import javax.swing.plaf.TableUI; |
|
|
|
import javax.swing.plaf.TableUI; |
|
|
|
|
|
|
|
import javax.swing.table.TableCellEditor; |
|
|
|
import javax.swing.table.TableCellRenderer; |
|
|
|
import javax.swing.table.TableCellRenderer; |
|
|
|
import javax.swing.table.TableColumn; |
|
|
|
import javax.swing.table.TableColumn; |
|
|
|
import java.awt.AWTEvent; |
|
|
|
import java.awt.AWTEvent; |
|
|
|
import java.awt.BorderLayout; |
|
|
|
|
|
|
|
import java.awt.Component; |
|
|
|
import java.awt.Component; |
|
|
|
import java.awt.Point; |
|
|
|
import java.awt.Point; |
|
|
|
import java.awt.Rectangle; |
|
|
|
import java.awt.Rectangle; |
|
|
|
import java.awt.Toolkit; |
|
|
|
import java.awt.Toolkit; |
|
|
|
import java.awt.event.AWTEventListener; |
|
|
|
|
|
|
|
import java.awt.event.FocusAdapter; |
|
|
|
import java.awt.event.FocusAdapter; |
|
|
|
import java.awt.event.FocusEvent; |
|
|
|
import java.awt.event.FocusEvent; |
|
|
|
import java.awt.event.MouseEvent; |
|
|
|
import java.awt.event.MouseEvent; |
|
|
|
import java.util.ArrayList; |
|
|
|
|
|
|
|
import java.util.List; |
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
|
|
|
public class UITable extends JTable implements UIObserver { |
|
|
|
public class UITable extends JTable implements UIObserver { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static final int OFF_LEFT = 10; |
|
|
|
private static final int OFF_LEFT = 10; |
|
|
|
private static final int DEFAULT_ROW_HEIGHT =20; |
|
|
|
private static final int STEP = 2; |
|
|
|
|
|
|
|
private static final int DEFAULT_ROW_HEIGHT = 20; |
|
|
|
private UIObserverListener uiObserverListener; |
|
|
|
private UIObserverListener uiObserverListener; |
|
|
|
UITableEditor editor ; |
|
|
|
UITableEditor editor; |
|
|
|
private boolean shouldResponseAwt; |
|
|
|
private boolean shouldResponseAwt; |
|
|
|
private boolean isEditingStopped; |
|
|
|
private boolean isEditingStopped; |
|
|
|
|
|
|
|
|
|
|
@ -53,16 +50,19 @@ public class UITable extends JTable implements UIObserver { |
|
|
|
initComponents(); |
|
|
|
initComponents(); |
|
|
|
iniListener(); |
|
|
|
iniListener(); |
|
|
|
shouldResponseAwt = false; |
|
|
|
shouldResponseAwt = false; |
|
|
|
// kunsnat: 屏蔽: 对于下拉框, 无法等待选择结果之后在stop..
|
|
|
|
|
|
|
|
// Toolkit.getDefaultToolkit().addAWTEventListener(awt, AWTEvent.MOUSE_EVENT_MASK);
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public UITable (int columnSize, boolean needAWTEventListener) { |
|
|
|
public UITable(int columnSize, boolean needAWTEventListener) { |
|
|
|
this(columnSize); |
|
|
|
this(columnSize); |
|
|
|
shouldResponseAwt = needAWTEventListener; |
|
|
|
shouldResponseAwt = needAWTEventListener; |
|
|
|
isEditingStopped = true; |
|
|
|
isEditingStopped = true; |
|
|
|
if (needAWTEventListener) { |
|
|
|
if (needAWTEventListener) { |
|
|
|
Toolkit.getDefaultToolkit().addAWTEventListener(awt, AWTEvent.MOUSE_EVENT_MASK); |
|
|
|
Toolkit.getDefaultToolkit().addAWTEventListener(event -> { |
|
|
|
|
|
|
|
if (!UITable.this.isShowing()) { |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
doSomeInAll(event); |
|
|
|
|
|
|
|
}, AWTEvent.MOUSE_EVENT_MASK); |
|
|
|
this.addFocusListener(new FocusAdapter() { |
|
|
|
this.addFocusListener(new FocusAdapter() { |
|
|
|
public void focusGained(FocusEvent e) { |
|
|
|
public void focusGained(FocusEvent e) { |
|
|
|
isEditingStopped = false; |
|
|
|
isEditingStopped = false; |
|
|
@ -86,21 +86,16 @@ public class UITable extends JTable implements UIObserver { |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 传入列表数据 |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param values 数据 |
|
|
|
|
|
|
|
*/ |
|
|
|
public void populateBean(List<Object[]> values) { |
|
|
|
public void populateBean(List<Object[]> values) { |
|
|
|
getTableDataModel().populateBean(values); |
|
|
|
getTableDataModel().populateBean(values); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private AWTEventListener awt = new AWTEventListener() { |
|
|
|
|
|
|
|
public void eventDispatched(AWTEvent event) { |
|
|
|
|
|
|
|
if(!UITable.this.isShowing()){ |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
doSomeInAll(event); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void doSomeInAll(AWTEvent event) { |
|
|
|
private void doSomeInAll(AWTEvent event) { |
|
|
|
Rectangle bounds = new Rectangle(getLocationOnScreen().x, getLocationOnScreen().y, getWidth(), getHeight()); |
|
|
|
Rectangle bounds = new Rectangle(getLocationOnScreen().x, getLocationOnScreen().y, getWidth(), getHeight()); |
|
|
|
if (event instanceof MouseEvent) { |
|
|
|
if (event instanceof MouseEvent) { |
|
|
@ -118,6 +113,11 @@ public class UITable extends JTable implements UIObserver { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 获取模型中数据 |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @return 数据 |
|
|
|
|
|
|
|
*/ |
|
|
|
public List<Object[]> updateBean() { |
|
|
|
public List<Object[]> updateBean() { |
|
|
|
return getTableDataModel().updateBean(); |
|
|
|
return getTableDataModel().updateBean(); |
|
|
|
} |
|
|
|
} |
|
|
@ -131,6 +131,7 @@ public class UITable extends JTable implements UIObserver { |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 在table底部增加一行内容 |
|
|
|
* 在table底部增加一行内容 |
|
|
|
|
|
|
|
* |
|
|
|
* @param line 该行的内容 |
|
|
|
* @param line 该行的内容 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public void addLine(Object[] line) { |
|
|
|
public void addLine(Object[] line) { |
|
|
@ -166,7 +167,8 @@ public class UITable extends JTable implements UIObserver { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
*格子是否可编辑,可置顶某一列column不可编辑 |
|
|
|
* 格子是否可编辑,可置顶某一列column不可编辑 |
|
|
|
|
|
|
|
* |
|
|
|
* @param row 行号 |
|
|
|
* @param row 行号 |
|
|
|
* @param column 列号 |
|
|
|
* @param column 列号 |
|
|
|
* @return 是否可编辑 |
|
|
|
* @return 是否可编辑 |
|
|
@ -221,7 +223,7 @@ public class UITable extends JTable implements UIObserver { |
|
|
|
setBackground(UIConstants.NORMAL_BACKGROUND); |
|
|
|
setBackground(UIConstants.NORMAL_BACKGROUND); |
|
|
|
setDefaultEditor(UITable.class, editor); |
|
|
|
setDefaultEditor(UITable.class, editor); |
|
|
|
setDefaultRenderer(UITable.class, new UITableRender()); |
|
|
|
setDefaultRenderer(UITable.class, new UITableRender()); |
|
|
|
// setUI(getUI());
|
|
|
|
setUI(getUI()); |
|
|
|
|
|
|
|
|
|
|
|
TableColumn deleteTableColumn = new TableColumn(getTableDataModel().getColumnCount()); |
|
|
|
TableColumn deleteTableColumn = new TableColumn(getTableDataModel().getColumnCount()); |
|
|
|
deleteTableColumn.setCellEditor(null); |
|
|
|
deleteTableColumn.setCellEditor(null); |
|
|
@ -232,22 +234,20 @@ public class UITable extends JTable implements UIObserver { |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 鼠标悬浮再某一行时触发的事件 |
|
|
|
* 鼠标悬浮再某一行时触发的事件 |
|
|
|
|
|
|
|
* |
|
|
|
* @param index 行号 |
|
|
|
* @param index 行号 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public void dealWithRollOver(int index){ |
|
|
|
public void dealWithRollOver(int index) { |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void iniListener(){ |
|
|
|
private void iniListener() { |
|
|
|
if(shouldResponseChangeListener()){ |
|
|
|
if (shouldResponseChangeListener()) { |
|
|
|
this.addChangeListener(new ChangeListener() { |
|
|
|
this.addChangeListener(e -> { |
|
|
|
@Override |
|
|
|
if (uiObserverListener == null) { |
|
|
|
public void stateChanged(ChangeEvent e) { |
|
|
|
return; |
|
|
|
if(uiObserverListener == null){ |
|
|
|
|
|
|
|
return ; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
uiObserverListener.doChange(); |
|
|
|
uiObserverListener.doChange(); |
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -258,7 +258,8 @@ public class UITable extends JTable implements UIObserver { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
*停止编辑事件 |
|
|
|
* 停止编辑事件 |
|
|
|
|
|
|
|
* |
|
|
|
* @param e 事件 |
|
|
|
* @param e 事件 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public void tableCellEditingStopped(ChangeEvent e) { |
|
|
|
public void tableCellEditingStopped(ChangeEvent e) { |
|
|
@ -266,7 +267,8 @@ public class UITable extends JTable implements UIObserver { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
*取消编辑事件 |
|
|
|
* 取消编辑事件 |
|
|
|
|
|
|
|
* |
|
|
|
* @param e 事件 |
|
|
|
* @param e 事件 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public void tableCellEditingCanceled(ChangeEvent e) { |
|
|
|
public void tableCellEditingCanceled(ChangeEvent e) { |
|
|
@ -275,6 +277,7 @@ public class UITable extends JTable implements UIObserver { |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 编辑器 |
|
|
|
* 编辑器 |
|
|
|
|
|
|
|
* |
|
|
|
* @return 编辑器 |
|
|
|
* @return 编辑器 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public UITableEditor createTableEditor() { |
|
|
|
public UITableEditor createTableEditor() { |
|
|
@ -289,21 +292,24 @@ public class UITable extends JTable implements UIObserver { |
|
|
|
return new UITableUI(); |
|
|
|
return new UITableUI(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// @Override
|
|
|
|
@Override |
|
|
|
/** |
|
|
|
public TableCellEditor getDefaultEditor(Class<?> columnClass) { |
|
|
|
* |
|
|
|
return super.getDefaultEditor(UITable.class); |
|
|
|
*/ |
|
|
|
} |
|
|
|
// public TableCellEditor getDefaultEditor(Class<?> columnClass) {
|
|
|
|
|
|
|
|
// return super.getDefaultEditor(UITable.class);
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// @Override
|
|
|
|
@Override |
|
|
|
/** |
|
|
|
public TableCellRenderer getDefaultRenderer(Class<?> columnClass) { |
|
|
|
* |
|
|
|
// 处理null的情况用于创建时未指定数据结构的情况
|
|
|
|
*/ |
|
|
|
if (columnClass == null) { |
|
|
|
// public TableCellRenderer getDefaultRenderer(Class<?> columnClass) {
|
|
|
|
return createDefaultRenderer(); |
|
|
|
// return super.getDefaultRenderer(UITable.class);
|
|
|
|
} |
|
|
|
// }
|
|
|
|
return super.getDefaultRenderer(columnClass); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private @NotNull TableCellRenderer createDefaultRenderer() { |
|
|
|
|
|
|
|
Object renderer = defaultRenderersByColumnClass.get(UITable.class); |
|
|
|
|
|
|
|
return renderer != null ? (TableCellRenderer) renderer : new UITableRender(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 给组件登记一个观察者监听事件 |
|
|
|
* 给组件登记一个观察者监听事件 |
|
|
@ -338,7 +344,7 @@ public class UITable extends JTable implements UIObserver { |
|
|
|
repaint(); |
|
|
|
repaint(); |
|
|
|
Object[] listeners = listenerList.getListenerList(); |
|
|
|
Object[] listeners = listenerList.getListenerList(); |
|
|
|
|
|
|
|
|
|
|
|
for (int i = listeners.length - 2; i >= 0; i -= 2) { |
|
|
|
for (int i = listeners.length - STEP; i >= 0; i -= STEP) { |
|
|
|
if (listeners[i] == ChangeListener.class) { |
|
|
|
if (listeners[i] == ChangeListener.class) { |
|
|
|
((ChangeListener) listeners[i + 1]).stateChanged(new ChangeEvent(this)); |
|
|
|
((ChangeListener) listeners[i + 1]).stateChanged(new ChangeEvent(this)); |
|
|
|
} |
|
|
|
} |
|
|
@ -347,6 +353,7 @@ public class UITable extends JTable implements UIObserver { |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 增加监听 |
|
|
|
* 增加监听 |
|
|
|
|
|
|
|
* |
|
|
|
* @param l 监听 |
|
|
|
* @param l 监听 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public void addChangeListener(ChangeListener l) { |
|
|
|
public void addChangeListener(ChangeListener l) { |
|
|
@ -354,36 +361,11 @@ public class UITable extends JTable implements UIObserver { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
*移除监听 |
|
|
|
* 移除监听 |
|
|
|
|
|
|
|
* |
|
|
|
* @param l 监听 |
|
|
|
* @param l 监听 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public void removeChangeListener(ChangeListener l) { |
|
|
|
public void removeChangeListener(ChangeListener l) { |
|
|
|
this.listenerList.remove(ChangeListener.class, l); |
|
|
|
this.listenerList.remove(ChangeListener.class, l); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
*测试主函数 |
|
|
|
|
|
|
|
* @param args 参数 |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public static void main(String... args) { |
|
|
|
|
|
|
|
JFrame jf = new JFrame("test"); |
|
|
|
|
|
|
|
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
|
|
|
|
|
|
|
JPanel content = (JPanel) jf.getContentPane(); |
|
|
|
|
|
|
|
content.setLayout(new BorderLayout()); |
|
|
|
|
|
|
|
List<Object[]> data = new ArrayList<Object[]>(); |
|
|
|
|
|
|
|
String[] a = {"1", "11"}; |
|
|
|
|
|
|
|
String[] b = {"2", "22"}; |
|
|
|
|
|
|
|
String[] c = {"3", "33"}; |
|
|
|
|
|
|
|
String[] d = {"4", "44"}; |
|
|
|
|
|
|
|
data.add(a); |
|
|
|
|
|
|
|
data.add(b); |
|
|
|
|
|
|
|
data.add(c); |
|
|
|
|
|
|
|
data.add(d); |
|
|
|
|
|
|
|
UITable pane = new UITable(2); |
|
|
|
|
|
|
|
pane.populateBean(data); |
|
|
|
|
|
|
|
content.add(pane, BorderLayout.CENTER); |
|
|
|
|
|
|
|
GUICoreUtils.centerWindow(jf); |
|
|
|
|
|
|
|
jf.setSize(400, 400); |
|
|
|
|
|
|
|
jf.setVisible(true); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |