我想要一个始终可见的JTable
和JComboBox
es。单击“我的代码未更新”框中的“我的代码未更新”。
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
public class trial extends JFrame {
private ArrayList<JComboBox> comboslist= new ArrayList <JComboBox>();/*list of my
comboboxes*/
public static void main(String[] args) {
trial tr= new trial();
}
public trial(){
this.setSize(new Dimension(600,100));
String[] ColData={"Index","Field Name","Value","Range"};
Object[][] TrowData={ };
DefaultTableModel model = new DefaultTableModel(TrowData, ColData);
final List<TableCellEditor> editors = new ArrayList<TableCellEditor>();
JComboBox combo1= new JComboBox();
combo1.addItem("don");
combo1.addItem("mump");
combo1.setEditable(true);
combo1.setBackground(Color.WHITE);
Object[] rowdata = {"cars","books","chicken","soup"};
model.addRow(rowdata);
DefaultCellEditor dce1 = new DefaultCellEditor(combo1);
editors.add(dce1);
JComboBox combo2= new JComboBox();
combo2.addItem("sop");
combo2.addItem("act");
combo2.setEditable(true);
combo2.setBackground(Color.WHITE);
Object[] rowdata2 = {"goats","cats","salads","dogs"};
model.addRow(rowdata2);
DefaultCellEditor dce2= new DefaultCellEditor(combo2);
editors.add(dce2);
comboslist.add(combo1);
comboslist.add(combo2);
JTable table = new JTable(model)
{
// Determine editor to be used by row
public TableCellEditor getCellEditor(int row, int column)
{
int modelColumn = convertColumnIndexToModel( column );
if (modelColumn == 2)
return editors.get(row);
else
return super.getCellEditor(row, column);
}
};
table.setRowHeight(0, 30);
table.setRowHeight(1, 30);
table.setPreferredScrollableViewportSize(new Dimension(800, 150));
table.setFillsViewportHeight(true);
TableColumn valCol= table.getColumnModel().getColumn(2);
int x=0;
for(JComboBox cb: comboslist ){
valCol.setCellRenderer(new ComboBoxCellRenderer(cb,x++));
}
this.add(table);
this.setVisible(true);
}
private class ComboBoxCellRenderer extends JComboBox implements TableCellRenderer {
private JComboBox combo;
private int idx=0;
private ComboBoxCellRenderer comboRenderer;
public ComboBoxCellRenderer(JComboBox com,int ind)
{
for (int i = 0; i < com.getItemCount(); i++)
{
addItem(com.getItemAt(i));
}
comboRenderer=this;
}
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
if(comboslist.get(row).getSelectedItem()!=null)
{
if(isSelected && row==idx)
{
setSelectedItem(comboslist.get(row).getSelectedItem().toString());
comboRenderer.setSelectedItem(comboslist.get(row).getSelectedItem().toString());
}
}else
{
setSelectedItem(value);
}
return this;
}
}
}
添加这段代码解决了一切。
column.setCellRenderer(new TableCellRenderer() {
JComboBox box = new JComboBox();
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
box.removeAllItems();
box.addItem(value.toString());
return box;
}
});
您已经有点向后进入并扭曲了。与其尝试拥有许多组合框编辑器,不如拥有一个组合框编辑器,然后根据行进行配置。
基于这个想法,例如,您可以让一个类同时充当渲染器和编辑器
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.AbstractCellEditor;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
public class Trial extends JFrame {
public static void main(String[] args) {
Trial tr = new Trial();
}
public Trial() {
this.setSize(new Dimension(600, 100));
String[] ColData = {"Index", "Field Name", "Value", "Range"};
Object[][] TrowData = {};
DefaultTableModel model = new DefaultTableModel(TrowData, ColData);
final List<TableCellEditor> editors = new ArrayList<TableCellEditor>();
model.addRow(new String[] {"cars", "books", "chicken", "soup"});
model.addRow(new String[] {"goats", "cats", "salads", "dogs"});
DefaultComboBoxModel model1 = new DefaultComboBoxModel();
model1.addElement("don");
model1.addElement("mump");
DefaultComboBoxModel model2 = new DefaultComboBoxModel();
model2.addElement("sop");
model2.addElement("act");
Map<Integer, ComboBoxModel<String>> models = new HashMap<>(2);
models.put(0, model1);
models.put(1, model2);
JTable table = new JTable(model);
table.setRowHeight(0, 30);
table.setRowHeight(1, 30);
table.setPreferredScrollableViewportSize(new Dimension(800, 150));
table.setFillsViewportHeight(true);
ComboBoxCellRendererEditor rendererEditor = new ComboBoxCellRendererEditor(models);
table.getColumnModel().getColumn(1).setCellRenderer(rendererEditor);
table.getColumnModel().getColumn(1).setCellEditor(rendererEditor);
this.add(table);
this.setVisible(true);
}
private static class ComboBoxCellRendererEditor extends AbstractCellEditor implements TableCellRenderer, TableCellEditor {
private JComboBox<String> comboBox;
private Map<Integer, ComboBoxModel<String>> models;
public ComboBoxCellRendererEditor(Map<Integer, ComboBoxModel<String>> models) {
comboBox = new JComboBox<>();
comboBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
stopCellEditing();
}
});
this.models = models;
}
protected void configure(Object value, int row) {
ComboBoxModel<String> model = models.get(row);
if (model == null) {
model = new DefaultComboBoxModel<>();
}
comboBox.setModel(model);
comboBox.setSelectedItem(value);
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
configure(value, row);
return comboBox;
}
@Override
public Object getCellEditorValue() {
return comboBox.getSelectedItem();
}
@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
configure(value, row);
return comboBox;
}
}
}
我试图传入一个JTable对象,它有DiscountID和Name列。我基本上是在尝试获取JTable a(折扣ID)中选择的第一列的一行,以及在循环中与JTable B中选择的记录相关的这组数据。 我现在的问题是,因为最初我必须将其设置为0,以避免出现但当我运行程序时,JTable B将显示与JTable A中的第一条记录相关的信息,而不需要我选择任何数据。我不想在JTTable B中显示任何信
我有一个jtable,它有一个编辑按钮,当我选择一行并点击编辑按钮和编辑字段,然后点击保存按钮,那一行没有更新,我必须刷新我的表来改变那一行! 我的代码: 我的NewUserFrame_Edit班: 我的模型类的 setValueAt() 和 updateFile() 方法: 当我选择一行并单击“编辑”按钮时,会打开一个新jframe,其文本字段填充了旧数据,我更新了数据并单击“保存”按钮。因此,
我有一个带有组合框的xaml。由于某种原因,我在那里有一些项目,我不能选择它们,当我选择一个项目时,它通常不允许我更改我的选择,如果它更改了,它仅适用于某些值。这是代码: 我希望我写的很清楚,因为我把它从电脑复制到了手机上。基本上,我有所有的用户,并按部门对他们进行分类。用户只能看到他所在部门的文档,并且可以选择绑定词典中可用的文档。如果不可用,组合框中的项目将为红色且不可检查。这个管用。唯一没有
我一直在试图找到一种通过直接输入来更新我的细胞的方法。从我的研究来看,每个人似乎都说你必须使用 尽管如此,它还是让我投myJTable-- 以下是我当前的表代码,如果这有帮助的话: 我怎么可能从用户直接输入更新的单元格中保存数据?谢谢!
问题内容: 我创建了以下示例,以便您可以确切了解正在发生的事情:http : //jsfiddle.net/8t2Ln/101/ 如果我使用ng-options,也会发生同样的事情。我这样做有不同的原因,但由于简化了示例,所以省略了这一部分。 如您所见,默认情况下它具有两个选项。我将在选择旁边显示ng- model的选择值,以便您可以看到它的含义。当您在顶部添加第三个选项时,它会将值设置为该新选项
我现在迷路了。