JDialog / ComboBox Anzeigefehler

daniel_sun

Erfahrenes Mitglied
Hallo,

ich habe einen interessanten Dialog fehler. Ich nutze ein und das selbe Formular um Daten in einer DB hinzuzufügen und zu bearbeiten.

Wenn ich Daten hinzufügen will, nutze ich diesesn Konstuktor:

Code:
public class InputForm extends JDialog{
	
	DBTable table = null;
	JLabel  labelName = new JLabel("Name:");
	JTextField textName = new JTextField();
	JLabel  labelSize = new JLabel("Größe:");
	JTextField textSize = new JTextField();
	JLabel  labelRelease = new JLabel("Release:");
	JTextField textRelease = new JTextField();
	JLabel  labelQuality = new JLabel("Qualität:");
	JComboBox<String> comboQuality = new JComboBox<String>(getQuality());
	JLabel  labelLocation = new JLabel("Speicherort:");
	JComboBox<String> comboLocation = new JComboBox<String>(getLoc());
	JButton buttonOk = new JButton("Hinzufügen");
	JButton buttonClose = new JButton("Abbrechen");
	ActionListener addData = new AddFormActionListener(this);
	ActionListener updateData = new UpdateFormActionListener(this);
	int row;
	
	public InputForm(DBTable table, MainWindow owner, String title){
		super(owner);
		this.table = table;
		this.setTitle(title);
		this.setSize(280,400);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
		this.setLayout(null);
		labelName.setLocation(10, 10);
		labelName.setSize(40,10);
		textName.setLocation(10, 30);
		textName.setSize(250, 25);
		labelSize.setLocation(10, 70);
		labelSize.setSize(40,10);
		textSize.setLocation(10, 90);
		textSize.setSize(250, 25);
		labelRelease.setLocation(10, 130);
		labelRelease.setSize(80,10);
		textRelease.setLocation(10, 150);
		textRelease.setSize(250, 25);
		labelQuality.setLocation(10, 190);
		labelQuality.setSize(60,10);
		comboQuality.setLocation(10, 210);
		comboQuality.setSize(90,25);
		labelLocation.setLocation(10, 250);
		labelLocation.setSize(80,10);
		comboLocation.setLocation(10, 270);
		comboLocation.setSize(80,25);
		buttonOk.setLocation(50, 330);
		buttonOk.setSize(100,25);
		buttonOk.addActionListener(addData);
		buttonClose.setLocation(160, 330);
		buttonClose.setSize(100,25);
		buttonClose.addActionListener(new ActionListener(){
					 
				public void actionPerformed(ActionEvent e) {
					closeWindow();
				}
			});
		
		this.add(labelName);
		this.add(textName);
		this.add(labelSize);
		this.add(textSize);
		this.add(labelRelease);
		this.add(textRelease);
		this.add(labelQuality);
		this.add(comboQuality);
		this.add(labelLocation);
		this.add(comboLocation);
		this.add(buttonOk);
		this.add(buttonClose);
	}

Er generiert mit dieses Formular:

falsch.png

Wie man sieht, sind die ComboBoxen nicht komplett dargestellt (der Pfeil nach unten gehlt). Die Größe des Objekts ist korrekt und auch der Inhalt (der aus der DB gelesen wird) ist vollständig.

Wenn ich Daten updaten will nutze ich die Überladung von Konstruktoren

Code:
	public InputForm(DBTable table, MainWindow owner, String title, int row){
		this(table, owner, title);
		this.row = row;
		buttonOk.setText("Bearbeiten");
		buttonOk.removeActionListener(addData);
		buttonOk.addActionListener(updateData);
		getData();
	}

Im Grunde wird nur der erste Konstruktr ausgeführt, eine zusätzliche Klassenvariable gefüllt und okButton geändert. Und die Fehler in der funktion getData(); mit den zu bearbeitenden Werten aus der DB befüllt. (bei den ComboBoxen wird das richtige Element selektiert)

Das Formular was ich dort ausgegeben bekomme ist dieses:

richtig.png

Hier werden die Boxen richtig angezeit. Leider hab ich keine Ahnung warum. Denn im grunde wird der selbe Konstruktor genutzt. Ich weis nicht mehr weiterhoffentlich einer von euch.

Gruß
daniel_sun

EDIT: Fehler weiter eingegrenzt. Es liegt an der Funktion: buttonOk.setText("Bearbeiten"); Löscht man diese wird auch beim bearbeitetn die Boxen nicht richtig angezeit. Anders herum füht man im anderen Konstruktor buttonOk.setText("Hinzufügen"); nach this.add(buttonClose); ein wedren auch hier die boxen richtig angezeigt. Allerdingt muss setText nach den adds kommen. Jemand ne erklärung?
 
Zuletzt bearbeitet:
Wollte es mal testen aber du benutzt da ja paar funktionen die du nicht angegeben hast ...
naja egal, ich würde jetz spontan erstmal sagen es liegt nicht am button (macht kein sinn) auch wenn ich imch da nicht festlegen will, aber als erstes, tu mal setVisible(true) GANZ ANS ENDE******
uns wurde in der uni gesagt es ist sehr wichtig, dass das immer ans ende kommt, weil sons halt fehler in der gui auftauchen können ^^

ich mach deshalb immer sowas um ne gute reihenfolge zu haben
Java:
public Konstructor()
{
   init();
   //andres wichtiges zeug
   setVisible(true);
}

public void init()
{
   //Fenster aufbau, listener aufnehmen etc etc 
}


wenn das mit dem setVisible verschieben nix bringt könntes du ja mal den ganzen code geben damit ich rumtesten kann ^^


Edit::
Hab mal alles womit ich nix anfangen konnte auskommentiert / ersetzt,bei mir funktionierts super mit dem visible am ende:
Java:
import java.awt.event.*;
import javax.swing.*;

public class InputForm extends JDialog{
    
    //DBTable table = null;
    JLabel  labelName = new JLabel("Name:");
    JTextField textName = new JTextField();
    JLabel  labelSize = new JLabel("Größe:");
    JTextField textSize = new JTextField();
    JLabel  labelRelease = new JLabel("Release:");
    JTextField textRelease = new JTextField();
    JLabel  labelQuality = new JLabel("Qualität:");
    JComboBox<String> comboQuality = new JComboBox<String>(new String[] {""+1,""+2,""+3});
    JLabel  labelLocation = new JLabel("Speicherort:");
    JComboBox<String> comboLocation = new JComboBox<String>(new String[] {""+4,""+5,""+6});
    JButton buttonOk = new JButton("Hinzufügen");
    JButton buttonClose = new JButton("Abbrechen");
    //ActionListener addData = new AddFormActionListener(this);
    //ActionListener updateData = new UpdateFormActionListener(this);
    int row;
    String title;
    
    public InputForm(String title){//public InputForm(DBTable table, MainWindow owner, String title){
        super();//super(owner);
        this.title=title;
        //this.table = table;
        init();
        this.setVisible(true);

    }
    
    public InputForm(String title, int row){//public InputForm(DBTable table, MainWindow owner, String title, int row){
        this(title);//this(table, owner, title);
        this.row = row;
        buttonOk.setText("Bearbeiten");
        //buttonOk.removeActionListener(addData);
        //buttonOk.addActionListener(updateData);
        //getData();
    }
    
    public void init()
    {
    	this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setTitle(title);
        this.setSize(280,400);
        this.setResizable(false);
        this.setLocationRelativeTo(null);
        this.setLayout(null);
        labelName.setLocation(10, 10);
        labelName.setSize(40,10);
        textName.setLocation(10, 30);
        textName.setSize(250, 25);
        labelSize.setLocation(10, 70);
        labelSize.setSize(40,10);
        textSize.setLocation(10, 90);
        textSize.setSize(250, 25);
        labelRelease.setLocation(10, 130);
        labelRelease.setSize(80,10);
        textRelease.setLocation(10, 150);
        textRelease.setSize(250, 25);
        labelQuality.setLocation(10, 190);
        labelQuality.setSize(60,10);
        comboQuality.setLocation(10, 210);
        comboQuality.setSize(90,25);
        labelLocation.setLocation(10, 250);
        labelLocation.setSize(80,10);
        comboLocation.setLocation(10, 270);
        comboLocation.setSize(80,25);
        buttonOk.setLocation(50, 330);
        buttonOk.setSize(100,25);
        //buttonOk.addActionListener(addData);
        buttonClose.setLocation(160, 330);
        buttonClose.setSize(100,25);
        buttonClose.addActionListener(new ActionListener(){
                     
                public void actionPerformed(ActionEvent e) {
                    //closeWindow();
                }
            });
        
        this.add(labelName);
        this.add(textName);
        this.add(labelSize);
        this.add(textSize);
        this.add(labelRelease);
        this.add(textRelease);
        this.add(labelQuality);
        this.add(comboQuality);
        this.add(labelLocation);
        this.add(comboLocation);
        this.add(buttonOk);
        this.add(buttonClose);
    }
    
    public static void main(String[] args)
    {
    	InputForm f = new InputForm("hahahahahahaha");
    }
}

dialog.jpg
 
Zuletzt bearbeitet:

Neue Beiträge

Zurück