-
-
-
importjava.awt.Component;
-
importjava.awt.Point;
-
importjava.awt.Rectangle;
-
importjava.awt.event.MouseEvent;
-
importjava.awt.event.WindowAdapter;
-
importjava.awt.event.WindowEvent;
-
importjava.util.EventObject;
-
-
importjavax.swing.DefaultCellEditor;
-
importjavax.swing.JComponent;
-
importjavax.swing.JFrame;
-
importjavax.swing.JScrollPane;
-
importjavax.swing.JTable;
-
importjavax.swing.JTextField;
-
importjavax.swing.SwingUtilities;
-
importjavax.swing.UIManager;
-
importjavax.swing.event.CellEditorListener;
-
importjavax.swing.event.ChangeEvent;
-
importjavax.swing.event.MouseInputListener;
-
importjavax.swing.plaf.basic.BasicTableHeaderUI;
-
importjavax.swing.table.JTableHeader;
-
importjavax.swing.table.TableCellEditor;
-
importjavax.swing.table.TableColumn;
-
importjavax.swing.table.TableColumnModel;
-
-
-
-
publicclassEditableHeaderTableExampleextendsJFrame{
-
-
publicEditableHeaderTableExample(){
-
super("EditableHeaderExample");
-
-
JTabletable=newJTable(7,5);
- TableColumnModelcolumnModel=table.getColumnModel();
-
table.setTableHeader(newEditableHeader(columnModel));
-
-
JScrollPanepane=newJScrollPane(table);
- getContentPane().add(pane);
- }
-
-
publicstaticvoidmain(String[]args){
-
EditableHeaderTableExampleframe=newEditableHeaderTableExample();
-
frame.addWindowListener(newWindowAdapter(){
-
publicvoidwindowClosing(WindowEvente){
-
System.exit(0);
- }
- });
-
frame.setSize(300,100);
-
frame.setVisible(true);
- }
- }
-
-
classEditableHeaderextendsJTableHeaderimplementsCellEditorListener{
-
publicfinalintHEADER_ROW=-10;
-
-
transientprotectedinteditingColumn;
-
-
transientprotectedTableCellEditorcellEditor;
-
-
transientprotectedComponenteditorComp;
-
-
publicEditableHeader(TableColumnModelcolumnModel){
-
super(columnModel);
-
setReorderingAllowed(false);
-
cellEditor=null;
- recreateTableColumn(columnModel);
- }
-
-
publicvoidupdateUI(){
-
setUI(newEditableHeaderUI());
- resizeAndRepaint();
- invalidate();
- }
-
-
protectedvoidrecreateTableColumn(TableColumnModelcolumnModel){
-
intn=columnModel.getColumnCount();
-
EditableHeaderTableColumn[]newCols=newEditableHeaderTableColumn[n];
-
TableColumn[]oldCols=newTableColumn[n];
-
for(inti=0;i<n;i++){
- oldCols[i]=columnModel.getColumn(i);
-
newCols[i]=newEditableHeaderTableColumn();
- newCols[i].copyValues(oldCols[i]);
- }
-
for(inti=0;i<n;i++){
- columnModel.removeColumn(oldCols[i]);
- }
-
for(inti=0;i<n;i++){
- columnModel.addColumn(newCols[i]);
- }
- }
-
-
publicbooleaneditCellAt(intindex){
-
returneditCellAt(index);
- }
-
-
publicbooleaneditCellAt(intindex,EventObjecte){
-
if(cellEditor!=null&&!cellEditor.stopCellEditing()){
-
returnfalse;
- }
-
if(!isCellEditable(index)){
-
returnfalse;
- }
- TableCellEditoreditor=getCellEditor(index);
-
-
if(editor!=null&&editor.isCellEditable(e)){
- editorComp=prepareEditor(editor,index);
- editorComp.setBounds(getHeaderRect(index));
- add(editorComp);
- editorComp.validate();
- setCellEditor(editor);
- setEditingColumn(index);
-
editor.addCellEditorListener(this);
-
-
returntrue;
- }
-
returnfalse;
- }
-
-
publicbooleanisCellEditable(intindex){
-
if(getReorderingAllowed()){
-
returnfalse;
- }
-
intcolumnIndex=columnModel.getColumn(index).getModelIndex();
- EditableHeaderTableColumncol=(EditableHeaderTableColumn)columnModel
- .getColumn(columnIndex);
-
returncol.isHeaderEditable();
- }
-
-
publicTableCellEditorgetCellEditor(intindex){
-
intcolumnIndex=columnModel.getColumn(index).getModelIndex();
- EditableHeaderTableColumncol=(EditableHeaderTableColumn)columnModel
- .getColumn(columnIndex);
-
returncol.getHeaderEditor();
- }
-
-
publicvoidsetCellEditor(TableCellEditornewEditor){
- TableCellEditoroldEditor=cellEditor;
- cellEditor=newEditor;
-
-
-
-
if(oldEditor!=null&&oldEditorinstanceofTableCellEditor){
- ((TableCellEditor)oldEditor)
-
.removeCellEditorListener((CellEditorListener)this);
- }
-
if(newEditor!=null&&newEditorinstanceofTableCellEditor){
- ((TableCellEditor)newEditor)
-
.addCellEditorListener((CellEditorListener)this);
- }
- }
-
-
publicComponentprepareEditor(TableCellEditoreditor,intindex){
- Objectvalue=columnModel.getColumn(index).getHeaderValue();
-
booleanisSelected=true;
-
introw=HEADER_ROW;
- JTabletable=getTable();
- Componentcomp=editor.getTableCellEditorComponent(table,value,
- isSelected,row,index);
-
if(compinstanceofJComponent){
-
((JComponent)comp).setNextFocusableComponent(this);
- }
-
returncomp;
- }
-
-
publicTableCellEditorgetCellEditor(){
-
returncellEditor;
- }
-
-
publicComponentgetEditorComponent(){
-
returneditorComp;
- }
-
-
publicvoidsetEditingColumn(intaColumn){
- editingColumn=aColumn;
- }
-
-
publicintgetEditingColumn(){
-
returneditingColumn;
- }
-
-
publicvoidremoveEditor(){
- TableCellEditoreditor=getCellEditor();
-
if(editor!=null){
-
editor.removeCellEditorListener(this);
-
- requestFocus();
- remove(editorComp);
-
-
intindex=getEditingColumn();
- RectanglecellRect=getHeaderRect(index);
-
-
setCellEditor(null);
-
setEditingColumn(-1);
-
editorComp=null;
-
- repaint(cellRect);
- }
- }
-
-
publicbooleanisEditing(){
-
return(cellEditor==null)?false:true;
- }
-
-
-
-
-
publicvoideditingStopped(ChangeEvente){
- TableCellEditoreditor=getCellEditor();
-
if(editor!=null){
- Objectvalue=editor.getCellEditorValue();
-
intindex=getEditingColumn();
- columnModel.getColumn(index).setHeaderValue(value);
- removeEditor();
- }
- }
-
-
publicvoideditingCanceled(ChangeEvente){
- removeEditor();
- }
-
-
-
-
-
-
- }
-
-
classEditableHeaderUIextendsBasicTableHeaderUI{
-
-
protectedMouseInputListenercreateMouseInputListener(){
-
returnnewMouseInputHandler((EditableHeader)header);
- }
-
-
publicclassMouseInputHandlerextendsBasicTableHeaderUI.MouseInputHandler{
-
privateComponentdispatchComponent;
-
-
protectedEditableHeaderheader;
-
-
publicMouseInputHandler(EditableHeaderheader){
-
this.header=header;
- }
-
-
privatevoidsetDispatchComponent(MouseEvente){
- ComponenteditorComponent=header.getEditorComponent();
- Pointp=e.getPoint();
- Pointp2=SwingUtilities.convertPoint(header,p,editorComponent);
- dispatchComponent=SwingUtilities.getDeepestComponentAt(
- editorComponent,p2.x,p2.y);
- }
-
-
privatebooleanrepostEvent(MouseEvente){
-
if(dispatchComponent==null){
-
returnfalse;
- }
- MouseEvente2=SwingUtilities.convertMouseEvent(header,e,
- dispatchComponent);
- dispatchComponent.dispatchEvent(e2);
-
returntrue;
- }
-
-
publicvoidmousePressed(MouseEvente){
-
if(!SwingUtilities.isLeftMouseButton(e)){
-
return;
- }
-
super.mousePressed(e);
-
-
if(header.getResizingColumn()==null){
- Pointp=e.getPoint();
- TableColumnModelcolumnModel=header.getColumnModel();
-
intindex=columnModel.getColumnIndexAtX(p.x);
-
if(index!=-1){
-
if(header.editCellAt(index,e)){
- setDispatchComponent(e);
- repostEvent(e);
- }
- }
- }
- }
-
-
publicvoidmouseReleased(MouseEvente){
-
super.mouseReleased(e);
-
if(!SwingUtilities.isLeftMouseButton(e)){
-
return;
- }
- repostEvent(e);
-
dispatchComponent=null;
- }
-
- }
-
- }
-
-
classEditableHeaderTableColumnextendsTableColumn{
-
-
protectedTableCellEditorheaderEditor;
-
-
protectedbooleanisHeaderEditable;
-
-
publicEditableHeaderTableColumn(){
- setHeaderEditor(createDefaultHeaderEditor());
-
isHeaderEditable=true;
- }
-
-
publicvoidsetHeaderEditor(TableCellEditorheaderEditor){
-
this.headerEditor=headerEditor;
- }
-
-
publicTableCellEditorgetHeaderEditor(){
-
returnheaderEditor;
- }
-
-
publicvoidsetHeaderEditable(booleanisEditable){
- isHeaderEditable=isEditable;
- }
-
-
publicbooleanisHeaderEditable(){
-
returnisHeaderEditable;
- }
-
-
publicvoidcopyValues(TableColumnbase){
- modelIndex=base.getModelIndex();
- identifier=base.getIdentifier();
- width=base.getWidth();
- minWidth=base.getMinWidth();
- setPreferredWidth(base.getPreferredWidth());
- maxWidth=base.getMaxWidth();
- headerRenderer=base.getHeaderRenderer();
- headerValue=base.getHeaderValue();
- cellRenderer=base.getCellRenderer();
- cellEditor=base.getCellEditor();
- isResizable=base.getResizable();
- }
-
-
protectedTableCellEditorcreateDefaultHeaderEditor(){
-
returnnewDefaultCellEditor(newJTextField());
- }
-
- }
http://www.java2s.com/Code/Java/Swing-Components/EditableHeaderTableExample.htm
分享到:
相关推荐
在IT领域,"editable-table"通常指的是一个特性或者组件,允许用户在表格的单元格内直接进行编辑。这种功能在数据管理和展示时非常实用,尤其是在Web应用中,它提供了直观且交互性强的数据输入方式。下面将详细介绍...
editable-table, 使 HTML table 可以编辑的小型 jquery/Bootstrap 小部件 可以编辑表这个小( 3 kb,行) 插件插件将任何 table 变成可以编辑的电子表格。 以下是主要功能:无魔术- 在普通的HTML table 上工作( 所以你...
-- Load `editable-table` directly from Pika's CDN --> < script type =" module " src =" https://cdn.pika.dev/editable-table " > </ script > <!-- put a normal <table> tag inside ...
Bootstrap Editable Table是一款基于Bootstrap框架的插件,用于创建可编辑的表格。这个插件使得在网页上直接编辑表格数据变得简单易行,提升了用户体验,同时也简化了开发者的工作。在这个项目中,主要包含一个名为...
Bootstrap Table Editable JS 是一个基于Bootstrap框架的动态表格插件,它允许用户在表格的单元格内进行编辑,提供了一种交互式的数据展示和管理方式。这个压缩包包含以下三个核心文件: 1. **bootstrap-editable....
在本话题中,我们聚焦于Bootstrap Table的一个扩展功能——可编辑下拉框,这得益于`bootstrap-table-editable`插件的使用。这个插件允许用户在表格的单元格内直接进行编辑,包括选择下拉框中的值。 `bootstrap-...
在 Bootstrap Table 中,`bootstrap-table-editable.js` 是一个非常重要的组件,它为表格提供了行内编辑的功能,极大地提升了用户体验。通过集成 x-editable 库,用户可以直接在表格中修改单元格内容,而无需跳转到...
Bootstrap+BootstrapTable+x-editable(css和js)整合包 内含 jq,js Bootstrap+BootstrapTable+x-editable内部的js 和css文件,一个整合包满足你的所有需求
压缩包里面有: 1.bootstrap-table-editable.js 2.bootstrap-editable.js 3.bootstrap-editable.css 用于bootstrap table实现x-editable的行单元格编辑
压缩包里面有: 1.bootstrap-table-editable.js 2.bootstrap-editable.js 3.bootstrap-editable.css 用于bootstrap table实现x-editable的行单元格编辑
X-Editable 是一个与Bootstrap Table集成的插件,用于实现表格内单元格的编辑功能。在本文中,我们将深入探讨如何使用Bootstrap Table的X-Editable特性来实现单元格的编辑,以及如何处理数据为空和自定义样式的场景...
"django-editable-table"就是这样一个应用,它为Django提供了具有动态列功能的简单可编辑表格。 动态列的概念允许用户根据需求自定义显示的列,这在处理大量数据或不同用户群体有不同需求时特别有用。它减少了代码...
Bootstrap Table实现X-Editable的行单元格编辑及解决数据Empty和支持多样式问题 本文着重解决X-Editable编辑的数据动态添加和显示数据为Empty的问题,还有给表格单元格的内容设置多样式,使得显示多样化。 一、...
bootstrap table合集(bootstrap-table bootstrap-table-editable bootstrap-editable) bootstrap table js插件,bootstrap table使用比较方便,有点easyui datagrid的风格,具体使用方法可见相关官网 ...
Разработкарасширенноооконструкторатаблицы Conjoint.ly的Заданиедляфронт-ендразработчика Результатомданногоза...
在本案例中,我们关注的是`x-editable`与`bootstraptable`的集成,这是一种流行的JavaScript组件,用于创建响应式、功能丰富的表格。 `BootstrapTable`是基于Twitter Bootstrap框架的插件,它为HTML表格提供了丰富...