ComboBoxen

Status
Nicht offen für weitere Antworten.

assault

Aktives Mitglied
Hi ich bräucht emal wieder eure hilfe.
ich habe jetzt soweit meine Gui fertig und müsste nun wissen ob es möglich ist die Java app zu stoppen bis ich eine auswahl in der ersten combobox getroffen habe ...

konkret sieht das so aus

Code:
// //////////////////////////////////////////
	// ComboBox
	// //////////////////////////////////////////
	
	/////////////////////////////////////////////
	// Inhalt ComboBox Host
	DBHS[] DB_HN1 = new DBHS[999];
	

	DB_HN1 = Read.read("", 0);

	do {

	    anrX = anrX + 1;

	} while (DB_HN1[anrX].hsname != "");

	anrFix = anrX;

	DBHS[] DB_HN2 = new DBHS[anrFix];
	DB_HN2 = Read.read("", anrFix);
	DB_HN1 = null;

	String[] dateStrings = { pos1, pos2, pos3, pos4, pos5 };
	String s[] = new String[anrFix];
	anrX = 0;
	
	do {

	     s[anrX] = DB_HN2[anrX].hsname;
	    
	   
	    anrX = anrX + 1;
	    
	} while (anrX < anrFix);
	
	////////////////////////////////////////////////////////////////
	
	final JComboBox Host = new JComboBox(s);
	Host.setSelectedIndex(0);
	Host.setBounds(10, 45, 90, 20);
	Host.setForeground(Color.BLACK);
	f.add(Host);
	
	// Host.addActionListener(a2);

	
	
	
	/////////////////////////////////////////////
	// Inhalt ComboBox SID
	
	
	i = Host.getSelectedIndex();
	zwi = DB_HN2[i].hsname;
	
	DBHS[] DB_SN1 = new DBHS[999];
	

	DB_HN1 = Read.read(zwi, 0);

	do {

	    anrX = anrX + 1;

	} while (DB_SN1[anrX].hsname != "");

	anrFix = anrX;

	DBHS[] DB_SN2 = new DBHS[anrFix];
	DB_HN2 = Read.read(zwi, anrFix);
	DB_HN1 = null;

	
	String s2[] = new String[anrFix];
	anrX = 0;
	
	do {

	    zwi = DB_SN2[anrX].hsname;
	    System.out.println(DB_SN2[anrX].hsname);
	    s2[anrX] = zwi;
	    anrX = anrX + 1;
	    
	} while (anrX < anrFix);
		
	
	
	final JComboBox SID = new JComboBox(dateStrings);
	SID.setSelectedIndex(4);
	SID.setBounds(110, 45, 100, 20);
	SID.setForeground(Color.BLACK);
	f.add(SID);
das problen ist halt das er die 2. combobox füllen will aber noch dicht den inex der 1. hat ->

Exception in thread "main" java.lang.NullPointerException
at Gui_v4.main(Gui_v4.java:198)
 

Niki

Top Contributor
Aus deinem Post ist mir die Frage nicht ganz ersichtlich. Du willst die 2. ComboBox erst füllen, wenn in der 1. ComboBox ein Item selektiert wird? Wenn ja brauchst du dafür einen ItemListener

Code:
JComboBox box1 = new JComboBox();
final JComboBox box2 = new JComboBox();

box1.addItemListener(new ItemListener(){
  public void itemChanged(ItemEvent e){
    if(e.getStateChange() == ItemEvent.SELECTED){
      box2.removeAllItems();
      //hier box2 mit den neuen Items befüllen
    }
  }
});
 

assault

Aktives Mitglied
jo genau am besten alle bis auf die 1. combobox werden grau, dann wählt man aus der ersten ein item und die 2. combobox wird anwählbar so soll man sich dann bis zum ende durchklicken
 

assault

Aktives Mitglied
hi bins nochmal wegen demitem listener ich weiß jetzt nicht ab das jetzt geht mit dem Listener weil
der teil des codes


Code:
i = Host.getSelectedIndex(); 
   zwi = DB_HN2[i].hsname; 
    
   DBHS[] DB_SN1 = new DBHS[999]; 
    

   DB_HN1 = Read.read(zwi, 0); 

   do { 

       anrX = anrX + 1; 

   } while (DB_SN1[anrX].hsname != ""); 

   anrFix = anrX; 

   DBHS[] DB_SN2 = new DBHS[anrFix]; 
   DB_HN2 = Read.read(zwi, anrFix); 
   DB_HN1 = null; 

    
   String s2[] = new String[anrFix]; 
   anrX = 0; 
    
   do { 

       zwi = DB_SN2[anrX].hsname; 
       System.out.println(DB_SN2[anrX].hsname); 
       s2[anrX] = zwi; 
       anrX = anrX + 1; 
       
   } while (anrX < anrFix);
soll ja die combobox füllen das Problemist aber das ich den STring s2 doch eignetlich erstellen muss vor der combobox oder nicht???
 

Niki

Top Contributor
Na dann füge doch bei jeder ComboBox einen ItemListener hinzu, setz alle, bis auf die erste auf setEnabled(false). Im ItemListener musst du dann prüfen ob etwas sinnvolles selektiert wurde. Wenn ja kannst du die nächste ComboBox aktivieren bzw. auch bei Bedarf mit anderen Werten füllen. Beim Befüllen muss man glaub ich aufpassen dass die Events nicht geworfen/behandelt werden. Entweder mit flags steuern, oder einfacher ist den Listener vorher deregistrieren und dann wieder registrieren.
 

Niki

Top Contributor
Vielleicht hilft dir ja dieses Beispiel weiter:
Code:
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.WindowConstants;

public class JFComboBoxDemo extends JFrame {

	private JComboBox cb1 = null;
	private JComboBox cb3 = null;
	private JComboBox cb2 = null;

	private ItemListener il1 = null;

	private ItemListener il2 = null;

	public JFComboBoxDemo() {
		super("ComboBox Demo");
		guiInit();
		pack();
		setLocationRelativeTo(null);
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
	}

	private void guiInit() {
		Insets i = new Insets(3, 3, 3, 3);
		GridBagConstraints c = new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0,
				GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, i,
				0, 0);
		Container cont = getContentPane();
		cont.setLayout(new GridBagLayout());

		cb1 = new JComboBox();
		cb1.addItem("");
		cb1.addItem("1");
		cb1.addItem("2");

		cb2 = new JComboBox();		

		cb3 = new JComboBox();
		

		cont.add(cb1, c);
		c.gridy++;
		cont.add(cb2, c);
		c.gridy++;
		cont.add(cb3, c);

		cb2.setEnabled(false);
		cb3.setEnabled(false);

		il1 = new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				if (ItemEvent.SELECTED == e.getStateChange()) {
					String item = (String)e.getItem();					
					
					cb2.removeAllItems();
					if(item.length() > 0){					
						if("1".equals(item)){
							cb2.addItem("");
							cb2.addItem("3");
							cb2.addItem("4");
						} else {
							cb2.addItem("");
							cb2.addItem("5");
							cb2.addItem("6");
						}
						
						cb2.setEnabled(true);
					} else {						
						cb2.setEnabled(false);
						cb3.setEnabled(false);
						cb3.removeAllItems();
					}
					
				
				}
			}
		};

		il2 = new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				if (ItemEvent.SELECTED == e.getStateChange()) {
					if (ItemEvent.SELECTED == e.getStateChange()) {
						String item = (String)e.getItem();							
						cb3.removeAllItems();
						if(item.length() > 0){					
							if("3".equals(item)){
								cb3.addItem("");
								cb3.addItem("7");
								cb3.addItem("8");
							} else if("4".equals(item)){
								cb3.addItem("");
								cb3.addItem("9");
								cb3.addItem("a");
							}else if("5".equals(item)){
								cb3.addItem("");
								cb3.addItem("b");
								cb3.addItem("c");
							}else if("6".equals(item)){
								cb3.addItem("");
								cb3.addItem("d");
								cb3.addItem("e");
							}
							
							cb3.setEnabled(true);
						} else {						
							cb3.setEnabled(false);
						}						
					}
				}
			}
		};

		
		cb1.addItemListener(il1);
		cb2.addItemListener(il2);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new JFComboBoxDemo().setVisible(true);

	}

}
 

assault

Aktives Mitglied
ja die sache ist ja nich der listener, die sache ist die das der code weiter läuft, wenn ich den code der die box füllen soll in den
listener packe kann er nicht mehr auf die variablen und arrays zugreifen die ich zum "berechnen" der liste benöte. wenn ich versuche die ausserhalb des listeners zu halten bekomme ich denn fehler das ihm die werte fehlen weil eben halt zu dem zeitpunkt noch nichts ausgewählt werden konnte (soll ich mal zu hilfe den kompletten source posten???)
 

Niki

Top Contributor
Wieso kannst du nicht auf die Variablen zugreifen? Deklarier sie als Attribute deiner Klasse oder setz sie final, dann kannst du sie auch innerhalb des Listeners verwenden. Poste halt mal Code und beschreib woran es genau liegt und was du genau erreichen möchtest
 

assault

Aktives Mitglied
Code:
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Gui_v4 {
    public static void main(String[] args) {
	int anrX = 0;
	int anrFix = 0;
	String zwi = "";
	int i;

	// //////////////////////////////////////////////////////////////////
	// GUI
	// //////////////////////////////////////////////////////////////////

	JFrame f = new JFrame("ID");
	f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	f.setLayout(null); // new GridLayout(3, 3));
	f.setSize(560, 200);

	java.awt.Dimension mD = java.awt.Toolkit.getDefaultToolkit()
		.getScreenSize();
	java.awt.Dimension fD = f.getSize();
	f.setLocation((int) ((mD.getWidth() - fD.getWidth()) / 2), (int) ((mD
		.getHeight() - fD.getHeight()) / 2));

	// //////////////////////////////////////////
	// LABEL
	// //////////////////////////////////////////
	JLabel l1 = new JLabel("Host");
	l1.setForeground(Color.BLUE);
	l1.setBounds(10, 20, 90, 20);

	f.add(l1);

	JLabel l2 = new JLabel("SID");
	l2.setForeground(Color.BLUE);
	l2.setBounds(110, 20, 90, 20);

	f.add(l2);

	JLabel l3 = new JLabel("Mandant");
	l3.setForeground(Color.BLUE);
	l3.setBounds(220, 20, 90, 20);

	f.add(l3);

	JLabel l4 = new JLabel("Anwender-NR.");
	l4.setForeground(Color.BLUE);
	l4.setBounds(330, 20, 90, 20);

	f.add(l4);

	JLabel l5 = new JLabel("Jahr");
	l5.setForeground(Color.BLUE);
	l5.setBounds(440, 20, 90, 20);

	f.add(l5);

	JLabel la1 = new JLabel("");
	la1.setForeground(Color.BLUE);
	la1.setBounds(10, 70, 90, 20);

	f.add(la1);

	JLabel la2 = new JLabel("");
	la2.setForeground(Color.BLUE);
	la2.setBounds(110, 70, 90, 20);

	f.add(la2);

	JLabel la3 = new JLabel("");
	la3.setForeground(Color.BLUE);
	la3.setBounds(220, 70, 90, 20);

	f.add(la3);

	JLabel la4 = new JLabel("");
	la4.setForeground(Color.BLUE);
	la4.setBounds(330, 70, 90, 20);

	f.add(la4);

	JLabel la5 = new JLabel("");
	la5.setForeground(Color.BLUE);
	la5.setBounds(440, 70, 90, 20);

	f.add(la5);

	// //////////////////////////////////////////
	// Button
	// //////////////////////////////////////////
	final JButton EXIT = new JButton("Exit");
	f.getContentPane().add(EXIT /* BorderLayout.SOUTH */);
	EXIT.setPreferredSize(new java.awt.Dimension(65, 35));
	f.add(EXIT);
	EXIT.setBounds(275, 105 /* 75 */, 155, 20);

	final JButton RESET = new JButton("Reset");
	f.getContentPane().add(RESET /* BorderLayout.SOUTH */);
	RESET.setPreferredSize(new java.awt.Dimension(65, 35));
	f.add(RESET);
	RESET.setBounds(110, 105/* 75 */, 155, 20);

	// //////////////////////////////////////////
	// Date
	// //////////////////////////////////////////
	Date dt = new Date();
	SimpleDateFormat df = new SimpleDateFormat("yyyy");
	df.setTimeZone(TimeZone.getDefault());

	String n = df.format(dt);
	double zahl = Double.valueOf(n).doubleValue();
	zahl = zahl - 2;
	String pos1 = (int) zahl + "";
	zahl = zahl + 1;
	String pos2 = (int) zahl + "";
	zahl = zahl + 1;
	String pos3 = (int) zahl + "";
	zahl = zahl + 1;
	String pos4 = (int) zahl + "";
	zahl = zahl + 1;
	String pos5 = (int) zahl + "";

	// //////////////////////////////////////////
	// ComboBox
	// //////////////////////////////////////////

	// ///////////////////////////////////////////
	// Inhalt ComboBox Host
	final DBHS[] DB_HN1 = new DBHS[999];

	DB_HN1 = Read.read("", 0);

	do {

	    anrX = anrX + 1;

	} while (DB_HN1[anrX].hsname != "");

	anrFix = anrX;

	final DBHS[] DB_HN2 = new DBHS[anrFix];
	DB_HN2 = Read.read("", anrFix);
	//DB_HN1 = null;

	String[] dateStrings = { pos1, pos2, pos3, pos4, pos5 };
	String s[] = new String[anrFix];
	anrX = 0;

	do {

	    s[anrX] = DB_HN2[anrX].hsname;

	    anrX = anrX + 1;

	} while (anrX < anrFix);

	
	
	// //////////////////////////////////////////////////////////////

	final JComboBox Host = new JComboBox(s);
	Host.setSelectedIndex(0);
	Host.setBounds(10, 45, 90, 20);
	Host.setForeground(Color.BLACK);
	f.add(Host);

	// Host.addActionListener(a2);

	// ///////////////////////////////////////////
	// Inhalt ComboBox SID

// hier der original source (auskomentiert)
	
	
	
//	i = Host.getSelectedIndex();
//	zwi = DB_HN2[i].hsname;
//
//	DBHS[] DB_SN1 = new DBHS[999];
//
//	DB_HN1 = Read.read(zwi, 0);
//
//	do {
//
//	    anrX = anrX + 1;
//
//	} while (DB_SN1[anrX].hsname != "");
//
//	anrFix = anrX;
//
//	DBHS[] DB_SN2 = new DBHS[anrFix];
//	DB_HN2 = Read.read(zwi, anrFix);
//	DB_HN1 = null;
//
//	String s2[] = new String[anrFix];
//	anrX = 0;
//
//	do {
//
//	    zwi = DB_SN2[anrX].hsname;
//	    System.out.println(DB_SN2[anrX].hsname);
//	    s2[anrX] = zwi;
//	    anrX = anrX + 1;
//
//	} while (anrX < anrFix);

//////////////////////////////////////////////////////////7	
	
	final JComboBox SID = new JComboBox();
	SID.setSelectedIndex(0);
	SID.setBounds(110, 45, 100, 20);
	SID.setForeground(Color.BLACK);
	f.add(SID);

	// SID.addActionListener(a2);

	final JComboBox Mand = new JComboBox(dateStrings);
	Mand.setSelectedIndex(4);
	Mand.setBounds(220, 45, 100, 20);
	Mand.setForeground(Color.BLACK);
	f.add(Mand);

	// Mand.addActionListener(a2);

	final JComboBox anw = new JComboBox(dateStrings);
	anw.setSelectedIndex(4);
	anw.setBounds(330, 45, 100, 20);
	anw.setForeground(Color.BLACK);
	f.add(anw);

	// anw.addActionListener(a2);

	final JComboBox Jahr = new JComboBox(dateStrings);
	Jahr.setSelectedIndex(4);
	Jahr.setBounds(440, 45, 100, 20);
	Jahr.setForeground(Color.BLACK);
	f.add(Jahr);

	
// und hier der Listener	
	
	Host.addItemListener(new ItemListener(){ 
	    public void itemChanged(ItemEvent e, int i, String zwi, int anrX, int anrFix){ 
	      if(e.getStateChange() == ItemEvent.SELECTED){ 
	        SID.removeAllItems();
	        
	        
	        i = Host.getSelectedIndex();
		zwi = DB_HN2[i].hsname;

		DBHS[] DB_SN1 = new DBHS[999];

		DB_HN1 = Read.read(zwi, 0);

		do {

		    anrX = anrX + 1;

		} while (DB_SN1[anrX].hsname != "");

		anrFix = anrX;

		DBHS[] DB_SN2 = new DBHS[anrFix];
		DB_HN2 = Read.read(zwi, anrFix);
		DB_HN1 = null;

		String s2[] = new String[anrFix];
		anrX = 0;

		do {

		    zwi = DB_SN2[anrX].hsname;
		    System.out.println(DB_SN2[anrX].hsname);
		    s2[anrX] = zwi;
		    anrX = anrX + 1;

		} while (anrX < anrFix);
	        
	        
	         
	      } 
	    } 
	  });
	

////////////////////////////////////////////////////////////	
	
	// Jahr.addActionListener(a2);

	// //////////////////////////////////////////
	// ActionListener
	// //////////////////////////////////////////
	ActionListener a1 = new ActionListener() {
	    public void actionPerformed(ActionEvent e) {
		System.exit(0);
	    }
	};
	EXIT.addActionListener(a1);

	ActionListener a2 = new ActionListener() {
	    public void actionPerformed(ActionEvent e) {
		Host.setSelectedIndex(0);
		SID.setSelectedIndex(0);
		Mand.setSelectedIndex(0);
		anw.setSelectedIndex(0);
		Jahr.setSelectedIndex(0);
	    }
	};

	RESET.addActionListener(a2);

	f.setSize(560, 200);
	f.setVisible(true);

    }

}

so und er listener ist rot markiert mit dem fehler

The type new ItemListener(){} must implement the inherited abstract method ItemListener.itemStateChanged(ItemEvent)

hab auch geschaut das ich das wegen dem Fehler erweiter aber er schluckts nicht :(

hätte dann
Code:
	Host.addItemListener(new ItemListener()implements ItemListener.itemStateChanged(ItemEvent){ 
	    public void itemChanged(ItemEvent e, int i, String zwi, int anrX, int anrFix){ 
	      if(e.getStateChange() == ItemEvent.SELECTED){ 
	        SID.removeAllItems();
	        
	        
...


und hier noch die read class eber die wird wohl nicht weiter helfen ...

Code:
import java.io.*;
import java.sql.*;
import sun.jdbc.odbc.JdbcOdbcDriver;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.Date;
import java.text.*;

public class Read {

    public static DBHS[] read(String HSNAME, int anrX) {
	int c= 0 ;
	if (anrX == 0) {
	    anrX = 999; c = 1;
	}
	DBHS[] DB_HSN = new DBHS[anrX];
	
	int i = 0;
	anrX = 0;
	int anrfix = 0;

	try {
	    RandomAccessFile f;
	    if (HSNAME == "") {
		f = new RandomAccessFile("Input-TNSNames\\Host.tns", "r");
	    } else {
		f = new RandomAccessFile("Input-TNSNames\\DB\\" + HSNAME
			+ ".tns", "r");
	    }

	    for (String line; (line = f.readLine()) != null;) {

		i++;
		if (i == 1) {
		    HSNAME = line;
		    DB_HSN[anrX] = new DBHS(HSNAME);
		    anrX = anrX + 1;
		}

		if (i >= 2) {
		    i = 0;
		}
	    }
	    
	    if (c == 1){ 
	    
	    HSNAME = "";
	    DB_HSN[anrX] = new DBHS(HSNAME);
	    }
	    f.close();

	    anrX = 0;

	} catch (FileNotFoundException e) {
	    System.err.println("Datei gibt’s nicht!");
	} catch (IOException e) {
	    System.err.println("Schreib-/Leseprobleme!");
	}

	return DB_HSN;
    }
}
 

Niki

Top Contributor
Das wird so nichts.

Wenn du zum Befüllen der ComboBoxen mehrere Attribute brauchst, die sich auch zur Laufzeit ändern können, dann musst du dem Listener die Referenz mit geben. Du solltest als erstes mal deine Klassen sauber trennen und statt alles in die main Methode zu schreiben eine Klasse erstellen die von JFrame erbt:

Code:
public class MyFrame extends JFrame{

  private JComboBox cbHost = null;

  private JComboBox cbJahr = null;

  //und so weiter....

  public MyFrame(){
    super("Mein Titel");
    guiInit();
    pack();
    setLocationRelativeTo(null);
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  }

  private void guiInit(){
    //Hier Oberfläche aufbauen
  }

  //Methoden zum Abrufen der Attribute...

  

  public static void main(String[] args){
    new MyFrame().setVisible(true);
  }

}

dann brauchst du eine Klasse, die das Interface ItemListener implementiert und eine Referenz auf den Frame hat, damit die Attribute ausgelesen werden können:

Code:
public class HostItemListener implements ItemListener{

  private MyFrame frame = null;

  public HostItemListener(MyFrame frame){
    this.frame = frame;
  }

  public void itemChanged(ItemEvent e){
    //hier kann dann vom frame alles notwendige geholt werden und anhand der Werte
    //irgend etwas im frame geändert werden
  }
}
 

assault

Aktives Mitglied
o.0 wird wohl nicht einfacher gehen

Code:
public class MyFrame extends JFrame{ 

  private JComboBox cbHost = null; 

  private JComboBox cbJahr = null; 

  //und so weiter.... 

  public MyFrame(){ 
    super("Mein Titel"); 
    guiInit(); 
    pack(); 
    setLocationRelativeTo(null); 
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
  } 

  private void guiInit(){ 
    //Hier Oberfläche aufbauen 
  } 

  //Methoden zum Abrufen der Attribute... 

  

  public static void main(String[] args){ 
    new MyFrame().setVisible(true); 
  } 

}
soll ich hierauch das Fenster und die Labels reinpacken???
 

assault

Aktives Mitglied
Code:
import java.awt.Color;
import java.awt.event.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class MyFrame extends JFrame {

    private JComboBox cbHost = null;

    private JComboBox cbSID = null;

    private JComboBox cbMand = null;

    private JComboBox cbanw = null;

    private JComboBox cbJahr = null;

    public MyFrame() {
	JFrame f = new JFrame("ID");
	guiInit();
	pack();
	f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	f.setLayout(null); // new GridLayout(3, 3));
	f.setSize(560, 200);

	java.awt.Dimension mD = java.awt.Toolkit.getDefaultToolkit()
		.getScreenSize();
	java.awt.Dimension fD = f.getSize();
	f.setLocation((int) ((mD.getWidth() - fD.getWidth()) / 2), (int) ((mD
		.getHeight() - fD.getHeight()) / 2));
    }

    private void guiInit() {

	JLabel l1 = new JLabel("Host");
	l1.setForeground(Color.BLUE);
	l1.setBounds(10, 20, 90, 20);

	JLabel l2 = new JLabel("SID");
	l2.setForeground(Color.BLUE);
	l2.setBounds(110, 20, 90, 20);

	JLabel l3 = new JLabel("Mandant");
	l3.setForeground(Color.BLUE);
	l3.setBounds(220, 20, 90, 20);

	JLabel l4 = new JLabel("Anwender-NR.");
	l4.setForeground(Color.BLUE);
	l4.setBounds(330, 20, 90, 20);

	JLabel l5 = new JLabel("Jahr");
	l5.setForeground(Color.BLUE);
	l5.setBounds(440, 20, 90, 20);

	JLabel la1 = new JLabel("");
	la1.setForeground(Color.BLUE);
	la1.setBounds(10, 70, 90, 20);

	JLabel la2 = new JLabel("");
	la2.setForeground(Color.BLUE);
	la2.setBounds(110, 70, 90, 20);

	JLabel la3 = new JLabel("");
	la3.setForeground(Color.BLUE);
	la3.setBounds(220, 70, 90, 20);

	JLabel la4 = new JLabel("");
	la4.setForeground(Color.BLUE);
	la4.setBounds(330, 70, 90, 20);

	JLabel la5 = new JLabel("");
	la5.setForeground(Color.BLUE);
	la5.setBounds(440, 70, 90, 20);

	final JComboBox Host = new JComboBox();
	Host.setSelectedIndex(0);
	Host.setBounds(10, 45, 90, 20);
	Host.setForeground(Color.BLACK);

	final JComboBox SID = new JComboBox();
	SID.setSelectedIndex(0);
	SID.setBounds(110, 45, 100, 20);
	SID.setForeground(Color.BLACK);

	final JComboBox Mand = new JComboBox();
	Mand.setSelectedIndex(4);
	Mand.setBounds(220, 45, 100, 20);
	Mand.setForeground(Color.BLACK);

	final JComboBox anw = new JComboBox();
	anw.setSelectedIndex(4);
	anw.setBounds(330, 45, 100, 20);
	anw.setForeground(Color.BLACK);

	final JComboBox Jahr = new JComboBox();
	Jahr.setSelectedIndex(4);
	Jahr.setBounds(440, 45, 100, 20);
	Jahr.setForeground(Color.BLACK);

    }

    public void H1() {
	int anrX = 0;
	int anrFix = 0;
	String zwi = "";
	int i;

	DBHS[] DB_HN1 = new DBHS[999];

	DB_HN1 = Read.read("", 0);

	do {

	    anrX = anrX + 1;

	} while (DB_HN1[anrX].hsname != "");

	anrFix = anrX;

	DBHS[] DB_HN2 = new DBHS[anrFix];
	DB_HN2 = Read.read("", anrFix);
	//DB_HN1 = null;

	String[] dateStrings = { pos1, pos2, pos3, pos4, pos5 };
	String s[] = new String[anrFix];
	anrX = 0;

	do {

	    s[anrX] = DB_HN2[anrX].hsname;

	    anrX = anrX + 1;

	} while (anrX < anrFix);

    }

    public void S1() {
	int anrX = 0;
	int anrFix = 0;
	String zwi = "";
	int i;

	i = Host.getSelectedIndex();
	zwi = DB_HN2[i].hsname;

	DBHS[] DB_SN1 = new DBHS[999];

	DB_HN1 = Read.read(zwi, 0);

	do {

	    anrX = anrX + 1;

	} while (DB_SN1[anrX].hsname != "");

	anrFix = anrX;

	DBHS[] DB_SN2 = new DBHS[anrFix];
	DB_HN2 = Read.read(zwi, anrFix);
	DB_HN1 = null;

	String s2[] = new String[anrFix];
	anrX = 0;

	do {

	    zwi = DB_SN2[anrX].hsname;
	    System.out.println(DB_SN2[anrX].hsname);
	    s2[anrX] = zwi;
	    anrX = anrX + 1;

	} while (anrX < anrFix);

    }

    public static void main(String[] args) {
	new MyFrame().setVisible(true);

    }

}

so ungefähr???
die listener müssten doch mit in die main methode oder? und wie bekomm ich jetzt mein fenster zum laufen?
 

Niki

Top Contributor
Nein, du brauchst keinen neuen JFrame mehr erstellen. Deine Klasse erbt von JFrame und ist daher selber einer. Du kannst die Komponenten daher alle direkt zum JFrame hinzufügen:
Code:
public class MyFrame extends JFrame{

  private JComboBox cbHost = null;

  private JComboBox cbJahr = null;

  //und so weiter....

  public MyFrame(){
    super("Mein Titel");
    guiInit();
    pack();
    setLocationRelativeTo(null);
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  }

  private void guiInit(){
    Container cont = getContentPane();
    cont.setLayout(....);

    //Hier Oberfläche aufbauen
    cbHost = new JComboBox();
    cont.add(cbHost);
  }

  //Methoden zum Abrufen der Attribute...

 

  public static void main(String[] args){
    new MyFrame().setVisible(true);
  }

}
 

assault

Aktives Mitglied
Code:
WindowConstants.EXIT_ON_CLOSE
muss ich wegen dem teil noch etwas importieren? bekomme nämlich den fehler "WindowConstants cannot be resolved"

und wie binde ich dan meine labels und so mit ein zb. frame.add(label) geth ja jetzt nichtmehr oder?
 

Niki

Top Contributor
WindowConstants musst du importieren.

du brauchst nicht mehr frame.add schreiben sondern einfach nur mehr add, da deine Klasse ja bereits dein Frame ist.
 
Status
Nicht offen für weitere Antworten.

Ähnliche Java Themen

Neue Themen


Oben