0

我正在搜索,但我找不到问题。我有一个带有自定义 JComboBox 的 JXTreeTable,当我更改所选项目时,我希望 JXTreeTable 的该行中的其他内容也发生更改,但是当它失去焦点时它只会更改。我已经尝试过使用不同的侦听器,包括 FocusListener。没有任何成功。

首先是 JXTree-Table 中调用方法的部分。

((BestellCustomComboCellEditor)getColumnModel().getColumn(4).getCellEditor()).getJComboBox().addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent g) {   
                System.out.println("TreeTableBestelllisteErstellung - addActionListener");          
            final int rowIndex = getSelectedRow();
            if (rowIndex < 0) 
                return;
            final Node selectedNode = getSelectedNode();
            if (selectedNode instanceof BestelllistenpositionErstellungNode) {
                BestelllistenpositionErstellungNode BestelllistenpositionErstellungNode1 = (BestelllistenpositionErstellungNode)selectedNode;
                int Kundennummer = ((ComboBoxItem)(((CustomComboCellEditor)getColumnModel().getColumn(1)
                        .getCellEditor()).getJComboBox()).getSelectedItem()).id;
                setProduktComboBox(Kundennummer);

                ((BestellCustomComboCellEditor)getColumnModel().getColumn(4)
                        .getCellEditor()).setJComboBox(produktComboBox); 
                erstelleOderAendereBestelllistenpositionErstellungNode(
                        BestelllistenpositionErstellungNode1,
                        BestelllisteTree, "Produkt");
            }
            try {
                setRowSelectionInterval(getSelectedRow(), getSelectedRow()); 
            }
            catch (Exception e) {

            }   
        }
    });

这是我的 JComboBoxCellEditor

public class BestellCustomComboCellEditor extends AbstractCellEditor
implements TableCellEditor,
ActionListener {

private static final long serialVersionUID = 1L;
NumberFormat integerFormat;
private WiderDropDownCombo jComboBox1;
private Object  [] comboBoxValues;
private TreeMap<Integer, TreeMap<Integer, ProduktTree>> treeProdukteVonAlleKunden;
private TreeMap<Integer, Verpackung> treeVerpackung;
private String LEERGUT;
private String PALETTEN;

public BestellCustomComboCellEditor(TreeMap<Integer, TreeMap<Integer, ProduktTree>> treeProdukteVonAlleKunden,
        TreeMap<Integer, Verpackung> treeVerpackung, String LEERGUT, String PALETTEN) {
    this.treeProdukteVonAlleKunden=treeProdukteVonAlleKunden;
    this.treeVerpackung=treeVerpackung;
    this.LEERGUT=LEERGUT;
    this.PALETTEN=PALETTEN;

    System.out.println("BestellCustomComboCellEditor - comboBoxValues");    
    comboBoxValues=new SortTreeMapValues().getSortedProduktLeergutArray (treeProdukteVonAlleKunden.get(
            treeProdukteVonAlleKunden.firstKey()),
            treeVerpackung,LEERGUT,PALETTEN);
    jComboBox1 = new WiderDropDownCombo(comboBoxValues);
    jComboBox1.setBackground(Color.WHITE);
    jComboBox1.setBorder(new EmptyBorder(0, 0,0,0));
}

public Component getTableCellEditorComponent(JTable table,
        Object value, boolean isSelected,
        int row, int column) {      

    if (table instanceof JXTreeTable) {
        JXTreeTable jxtable = (JXTreeTable) table;

        System.out.println("BestellCustomComboCellEditor - getTableCellEditorComponent");
        TreePath path = jxtable.getPathForRow(row);
        Object node = path.getLastPathComponent();

        if((column==4)&&isSelected&&row!=-1) {

            System.out.println("BestellCustomComboCellEditor - column==4)&&isSelected&&row!=-1");
            if (node instanceof BestelllistenpositionErstellungNode) {
                BestelllistenpositionErstellungNode 
                BestelllistenpositionErstellungNode1 = ((BestelllistenpositionErstellungNode)node);
                if(treeProdukteVonAlleKunden.get(BestelllistenpositionErstellungNode1.getKundennummer()).size()>0) {
                    comboBoxValues=new SortTreeMapValues().getSortedProduktLeergutArray (treeProdukteVonAlleKunden.get(
                            BestelllistenpositionErstellungNode1.getKundennummer()),
                            treeVerpackung,LEERGUT,PALETTEN);

                    jComboBox1 = new WiderDropDownCombo(comboBoxValues);
                    jComboBox1.setBackground(Color.WHITE);
                    jComboBox1.setBorder(new EmptyBorder(0, 0,0,0));

                    for(int i=0; i<jComboBox1.getItemCount(); i++) {
                        if(((ComboBoxBigItem)jComboBox1.getItemAt(i)).getProduktnummer()==BestelllistenpositionErstellungNode1.getProduktnummer()) {
                            jComboBox1.setSelectedIndex(i);
                        }
                    } 
                    jComboBox1.setMaximumRowCount(7);
                    jComboBox1.updateUI();
                    return jComboBox1;
                }
            }

        }

    }         
    return null;
}

public Object getCellEditorValue() {

   return jComboBox1.getSelectedItem();
}

public void actionPerformed(ActionEvent arg0) {

}

public WiderDropDownCombo getJComboBox() {
   return jComboBox1;
}

public void setJComboBox(WiderDropDownCombo WiderDropDownCombo ) {
        jComboBox1 = WiderDropDownCombo ;
    }}

这里是我的自定义 JComboBox

private static final long serialVersionUID = 1L;
private String type;
private boolean layingOut = false;
private int widestLengh = 0;
private boolean wide = false;


@SuppressWarnings("unchecked")
public WiderDropDownCombo( Object[] objs ) {
super( objs );
        Object popup = getUI().getAccessibleChild(this, 0);     
        Component c = ((Container) popup).getComponent(0);
        if (c instanceof JScrollPane)
          {
             JScrollPane scrollpane = (JScrollPane) c;
             JScrollBar scrollBar = scrollpane.getVerticalScrollBar();
             Dimension scrollBarDim = new Dimension(30, scrollBar
                   .getPreferredSize().height);
             scrollBar.setPreferredSize(scrollBarDim);
          } 
        setRenderer(new HighRowRenderer(this.getRenderer(),1));
        setMaximumRowCount(7);
        setBackground(Color.WHITE);
        setBorder(null);
}       // end of constructor WiderDropDownCombo( Object[] objs )

public boolean isWide() {
return wide;
}
//Setting the JComboBox wide
public void setWide(boolean wide) {
this.wide = wide;
widestLengh = getWidestItemWidth();

}
public Dimension getSize(){
Dimension dim = super.getSize();
if(!layingOut && isWide())
dim.width = Math.max( widestLengh, dim.width );
Object popup = WiderDropDownCombo.this.getUI().getAccessibleChild(this, 0);

Component c = ((Container) popup).getComponent(0);

   if (c instanceof JScrollPane)
  {
     JScrollPane scrollpane = (JScrollPane) c;
     JScrollBar scrollBar = scrollpane.getVerticalScrollBar();
     Dimension scrollBarDim = new Dimension(30, scrollBar
           .getPreferredSize().height);
     scrollBar.setPreferredSize(scrollBarDim);
  }
return dim;
}

public int getWidestItemWidth()
{

int numOfItems = this.getItemCount();
Font font = this.getFont();
FontMetrics metrics = this.getFontMetrics( font );
int widest = 0;
for ( int i = 0; i < numOfItems; i++ )
{
Object item = this.getItemAt( i );
int lineWidth = metrics.stringWidth( item.toString() );
widest = Math.max( widest, lineWidth );


}

return widest + 5;
}
public void doLayout(){
try{
layingOut = true;
super.doLayout();
}finally{
layingOut = false;
}
}
public String getType() {
return type;
}

public void setType( String t ) {
type = t;
}}
4

0 回答 0