Klitzekleines Problem :-)

Shizzl_chrizzl

Erfahrenes Mitglied
Also hab folgenes Problem:

Code:
PCombo_EQ testnarf = new PCombo_EQ();

 testnarf.comboEqui.addItemListener( new ItemListener() {
         public void itemStateChanged( ItemEvent e ) {
           if(e.getStateChange()==ItemEvent.SELECTED) 

        combo = (String)testnarf.comboEqui.getSelectedItem(); 
        System.out.println(combo);

wie ihr seht hol ich mir ne ComboBox aus ner anderen Klasse und möchte einfach das aktuelle selektierte item ausgeben. Nur das net , was mach ich falsch :suspekt:
 
Zuletzt bearbeitet:
Hallo,

was genau geht denn nicht? Kriegst Du Compile Fehler oder eine Exception beim ausführen oder der String combo enthält keinen Wert?

Bitte genauer spezifizieren.
 
der string combo erhält keinen wert

Code:
 comboEqui.addItemListener( new ItemListener() {
 public void itemStateChanged( ItemEvent e ) {
           if(e.getStateChange()==ItemEvent.SELECTED)      
             {
         //    combos = (String)comboEqui.getSelectedItem();   
          //   System.out.println(combos);
         //  stringEqui = new String(comboEqui.getSelectedItem().toString());
              if(comboProc.getItemCount() != 0){
              comboProc.removeAllItems();
            }
         try{
              comboProc.addItem(" ");
              FileInputStream fis = new FileInputStream("process.properties");
              Properties properties = new Properties();
              properties.load(fis);
              String temp = ""+comboEqui.getSelectedItem();
              temp = temp.substring(0,5);

so sieht n teil der anderen klasse aus da hängt an dem comboEqui schon ein Item Listener aber das dürfte normalerweise nichts ausmachen oder
 
Zuletzt bearbeitet:
Ich würde dafür statt ItemListener einen ActionListener verwenden. Der benachrichtigt Dich immer, wenn in der ComboBox ein anderer Eintrag ausgewählt wird.
 
also ich hab jetzt in der klasse wo ich das ausgewählte Item ausgeben weill alles in einen ActionListener umgewandelt. Doch er gibt mir immer noch nichts aus. Mach ich bei der Übergabe was falsch
 
Zuletzt bearbeitet:
Sorry, ich blicke in Deinem Code nicht durch.

Hier ist ein kleines Beispiel:
Code:
public class ComboBoxTest {
    
    public static void main(String[] args) {
        JFrame frame = new JFrame("ComboBoxTest");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        final JComboBox combo = new JComboBox(new String[] {"eins", "zwei", "drei"});
        
        Container c = frame.getContentPane();
        c.setLayout(new BorderLayout());
        frame.getContentPane().add(combo, BorderLayout.NORTH);
        
        final JTextArea text = new JTextArea();
        c.add(new JScrollPane(text), BorderLayout.CENTER);
        
        combo.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String item = (String) combo.getSelectedItem();
                text.append(item + "\n");
            } 
        });
        
        frame.setSize(300, 200);
        frame.setVisible(true);
    }
}
Vielleich kannst Du das auf Dein Programm irgendwie übertragen.

Gruß
Vincent
 
nein hilft mir nicht weiter ich hau jetzt mal beide source codes komplett rein


das ist das jtable wo das ausgewählte item angezeigt werden soll.
Code:
public class FI_WaferTable extends JPanel 
{

      public JTable tblMeasurement = new JTable();
      private JScrollPane scrollPane = new JScrollPane(tblMeasurement);
       
      private TableModel tableModel;
 
      public Vector vSearch;
      public Vector columnNames;

      String combo;
       PCombo_EQ testnarf = new PCombo_EQ();

    
public FI_WaferTable(Vector vSearch, Vector columnNames)
  {
    
    this.vSearch = vSearch;

  jbInit();
 }
  private void jbInit()  {

        columnNames = new Vector();
             
          columnNames.add("Index");
          columnNames.add("Datum");
          columnNames.add("Wert");
          columnNames.add("Sigma");
          columnNames.add("Equipment");
          columnNames.add("Prozess");
                   
        tblMeasurement.repaint();


          
        this.setLayout(new BorderLayout());
        this.add(scrollPane, BorderLayout.CENTER);
        this.add(tryBox, BorderLayout.NORTH);

        tblMeasurement.setPreferredScrollableViewportSize(new Dimension(240,300));
        tblMeasurement.setModel(getTableModelMeasure());
     //   TableModel.fireTableDataChanged();
              tblMeasurement.tableChanged(new TableModelEvent(tableModel));


      this.show();      

      ListSelectionModel rowSM = tblMeasurement.getSelectionModel();
        rowSM.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        rowSM.addListSelectionListener(new ListSelectionListener() {
                public void valueChanged(ListSelectionEvent e) {
                    ListSelectionModel lsm = (ListSelectionModel) e.getSource();

                    if (lsm.isSelectionEmpty()) {
                        System.out.println("no row selected");
                    } else {
                        System.out.println(tblMeasurement.getSelectedRow());

                    }
                }
            });         

}  

  /*******************************************************************************
     * setValueAt....
     * @return tabelModelLot
     * @exception no exception
  ***************************************************************************** */
  
private TableModel getTableModelMeasure() {
        if (tableModel != null) {
            return tableModel;
        } else {
            tableModel = new AbstractTableModel() {

      public int getRowCount() 
      {
      if (vSearch == null)
      {
       return 0;
      }
      else 
      {
       return vSearch.size();
      }
    }

    public int getColumnCount() 
    {
      return columnNames.size();
    }

    public String getColumnName(int columnIndex)
    {
      String colName = "";

      if (columnIndex <= getColumnCount())
          colName = (String)columnNames.elementAt(columnIndex);

          return colName;
    }

    public boolean isCellEditable(int rowIndex, int columnIndex)
    {
      return true;
    }

    public Object getValueAt(int rowIndex, int columnIndex) 
    {

         OMeasurementRS30 o = (OMeasurementRS30)vSearch.elementAt(rowIndex);

          switch (columnIndex) {
           case 0: return o.test;
           case 1: return o.process;
           case 2: return new Float(o.mean);
           case 3: return new Float(o.sigma);
           case 4: return o.equipment_ID;
           case 5: return o.processpr;
           
          }
          return null;

        }

       public void setValueAt(Object sValue, int row, int col) {

          try{

                        final OMeasurementRS30 om = (OMeasurementRS30)vSearch.elementAt(row);

                            switch (col) {
                             case 0: 

                                return;

                             case 1: 
                                return;

                             case 2:
                             return;
                             
                              case 3: 
                                return;
                                
                             case 4: 

                                 testnarf.comboEqui.addActionListener( new ActionListener() {
         public void actionPerformed( ActionEvent e ) {

        combo = (String)testnarf.comboEqui.getSelectedItem(); 
        System.out.println(combo);


         }
  });

  
                     return;
                               
                           

                                 case 5: 
                             om.processpr = sValue.toString();
                                return;
                              
                   }
                   fireTableDataChanged(); 
               }
                           catch (ClassCastException ex)
                           {
                           System.out.println("FEHLER: ClassCastException...");
                             //ex.printStackTrace();
                           }
                           
                        }
                
           
};
    return tableModel;
   }
}

}


und das hier ist die Klasse in dem die ComboBox drinnen ist

Code:
package fab6.integration.measure.FI_RS55;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.io.*;

public class PCombo_EQ extends JPanel 
{

    final String iniFileEqui = "runcard.properties";
    
    String Equipment1;
    String Equipment2;

    JPanel tabPanel1       = new JPanel();
    JPanel tabPanel2       = new JPanel();
    JPanel tabPanel3       = new JPanel();
    JPanel tabMain         = new JPanel();

    JLabel labelEqui       = new JLabel("Equipment", SwingConstants.CENTER);
    JLabel labelProc       = new JLabel("Prozesse", SwingConstants.CENTER);
    JLabel labelWafe       = new JLabel("Wafer", SwingConstants.CENTER);

    public JComboBox comboEqui = new JComboBox();
    public JComboBox comboProc = new JComboBox();
    public JComboBox comboWafe = new JComboBox();

    Vector vEquipment = new Vector();
    Vector vProcesses = new Vector();
    public String combos;
    ;
    
  public PCombo_EQ()
  { 
      jbInit();
  }
  public void jbInit(){

    iniFileReader();

        setLayout(new BorderLayout());

        comboEqui.addItem(" ");
        StringTokenizer st = new StringTokenizer(Equipment1, ",");
        int nTokens = st.countTokens();
        for(int i = 0; i < nTokens; i++){
         comboEqui.addItem(st.nextToken());
        }
        StringTokenizer ts = new StringTokenizer(Equipment2, ",");
        int nTokense = ts.countTokens();
        for(int i = 0; i < nTokense; i++){
         comboEqui.addItem(ts.nextToken());
        }
 
        comboEqui.addItemListener( new ItemListener() {
         public void itemStateChanged( ItemEvent e ) {
           if(e.getStateChange()==ItemEvent.SELECTED)      
             {
         
              if(comboProc.getItemCount() != 0){
              comboProc.removeAllItems();
            }
         try{
              comboProc.addItem(" ");
              FileInputStream fis = new FileInputStream("process.properties");
              Properties properties = new Properties();
              properties.load(fis);
              String temp = ""+comboEqui.getSelectedItem();
              temp = temp.substring(0,5);
      
             String proc = properties.getProperty(temp);
             StringTokenizer st = new StringTokenizer(proc,",");
             int count = st.countTokens();
             for(int i = 0; i < count; i++){
             comboProc.addItem(st.nextToken());
           }

      } catch (Exception eDocPfad) {
        System.out.println("Exception:  nicht korrekt!");
       }
    }
                          
          }
        });

        labelEqui.setForeground(Color.blue);
        labelProc.setForeground(Color.blue);
        labelWafe.setForeground(Color.blue);

        tabPanel1.add(comboEqui);
        tabPanel2.add(comboProc);
        tabPanel3.add(comboWafe);

        tabMain.add(labelEqui);
        tabMain.add(tabPanel1);
        tabMain.add(labelProc);
        tabMain.add(tabPanel2);
        tabMain.add(labelWafe);
        tabMain.add(tabPanel3);
        

        tabMain.setLayout( new GridLayout(6, 0));
        tabMain.setBorder(BorderFactory.createLineBorder(Color.black)); 
        this.add(tabMain, BorderLayout.CENTER);

  }
 private void iniFileReader() {
        try {
             StringTokenizer st;
         
             FileInputStream fis = new FileInputStream(iniFileEqui);
             Properties properties = new Properties();
             properties.load(fis);

             Equipment1 = properties.getProperty("EquipmentFD");
             Equipment2 = properties.getProperty("EquipmentFV");
             
            } catch (Exception e1) {
            e1.printStackTrace();
        }
    }
  
}

ich hoffe das es jetzt ein wenig verständlicher ist , wo mein Problem liegt.
 
Hi,
ich steig da auch nicht durch. Vincentius Code ist korrekt. Also liegt der Fehler bei Dir woanders.
- public Variablen sind prinzipiell schlechter Stil
- Benutze eine IDE und debugge einfach durch den Code, dann wirst Du sehen, wo es klemmt.
 
Deshalb schrieb ich auch "prinzipiell". ;)
Ich bin aber dennoch "Sauberkeitsfanatiker" und würde auch für X und Y die entsprechenden getter und setter anlegen (lassen ;)).
 

Neue Beiträge

Zurück