Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement selectable JComboBox in JTable cells

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article is to share with you about how to achieve the optional JComboBox in the JTable cell, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Implement optional JComboBox in the JTable cell. The classic source code (such as setting gender) [@ more@] gender example is used as follows:

TableColumnModel tcm = table.getColumnModel ()

TableColumn tc = tcm.getColumn (genderColumn); / / genderColumn is the serial number of your gender.

Tc.setCellRenderer (new GenderRenderer ())

Tc.setCellEditor (new GenderEditor ())

The restrictions are as follows, and the column type is boolean.

/ * *

* GenderEditor.java

, /

Import java.awt.Component

Import java.util.EventObject

Import java.awt.event.*

Import javax.swing.*

Import javax.swing.event.*

Import javax.swing.table.*

Public class GenderEditor extends JComboBox implements TableCellEditor {

Protected EventListenerList listenerList = new EventListenerList ()

Protected ChangeEvent changeEvent = new ChangeEvent (this)

Public GenderEditor () {

Super ()

AddItem ("Male")

AddItem ("Female")

AddActionListener (new ActionListener () {

Public void actionPerformed (ActionEvent event) {

FireEditingStopped ()

}

});

}

Public void addCellEditorListener (CellEditorListener listener) {

ListenerList.add (CellEditorListener.class, listener)

}

Public void removeCellEditorListener (CellEditorListener listener) {

ListenerList.remove (CellEditorListener.class, listener)

}

Protected void fireEditingStopped () {

CellEditorListener listener

Object [] listeners = listenerList.getListenerList ()

For (int I = 0; I < listeners.length; iTunes +) {

If (listeners [I] = = CellEditorListener.class) {

Listener = (CellEditorListener) listeners [I + 1]

Listener.editingStopped (changeEvent)

}

}

}

Protected void fireEditingCanceled () {

CellEditorListener listener

Object [] listeners = listenerList.getListenerList ()

For (int I = 0; I < listeners.length; iTunes +) {

If (listeners [I] = = CellEditorListener.class) {

Listener = (CellEditorListener) listeners [I + 1]

Listener.editingCanceled (changeEvent)

}

}

}

Public void cancelCellEditing () {

FireEditingCanceled ()

}

Public boolean stopCellEditing () {

FireEditingStopped ()

Return true

}

Public boolean isCellEditable (EventObject event) {

Return true

}

Public boolean shouldSelectCell (EventObject event) {

Return true

}

Public Object getCellEditorValue () {

Return new Boolean (getSelectedIndex ()) = = 0? True: false)

}

Public Component getTableCellEditorComponent (JTable table

Object value, boolean isSelected, int row, int column) {

Boolean isMale = (Boolean) value) .booleanValue ()

SetSelectedIndex (isMale? 0: 1)

Return this

}

}

/ * *

* GenderRender.java

, /

Import java.awt.Component

Import javax.swing.JComboBox

Import javax.swing.JTable

Import javax.swing.table.TableCellRenderer

Public class GenderRenderer extends JComboBox implements TableCellRenderer {

Public GenderRenderer () {

Super ()

AddItem ("Male")

AddItem ("Female")

}

Public Component getTableCellRendererComponent (JTable table

Object value, boolean isSelected, boolean hasFocus

Int row, int column) {

If (isSelected) {

SetForeground (table.getSelectionForeground ())

Super.setBackground (table.getSelectionBackground ())

} else {

SetForeground (table.getForeground ())

SetBackground (table.getBackground ())

}

Boolean isMale = (Boolean) value) .booleanValue ()

SetSelectedIndex (isMale? 0: 1)

Return this

}

}

The above is how to achieve the optional JComboBox in the JTable cell. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report