Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to implement selectable JComboBox in JTable cells

Shulou Source: shulou.com Published: 2022-06-01 11:16:18 09月26日 Update

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.

Tags: Unit selection gender example more knowledge article utility usage that is work meeting serial number article method source code look knowledge point type classic industry Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Shulou Tech Info NVidia Microsoft Shulou Technology