2010-11-21 7 views
0

An einem Punkt in meinem Programm öffnet es einen JDialog, der Informationen über die Vorgänge anzeigt, während das Programm ausgeführt wird. Es hat mehrere Bezeichnungen und einen Fortschrittsbalken, aber wenn das Dialogfenster geöffnet wird, zeigt es nichts an.mein benutzerdefinierter JDialog wird angezeigt, ist aber leer

Hier ist der individuelle Dialog und der Konstruktor:

public class DataMiner implements ActionListener 
{ 
    private Hashtable<Integer, GISNode> nodeTable; 
    private Hashtable<Integer, GISLink> linkTable; 
    private int numLinesIgnored; 
    private int numLinesProcessed; 
    private int numNodes; 
    private int numLinks; 
    private int numDuplicates; 
    private int numFailedGeoCodingRequests; 
    private boolean cancelled; 

// window objects 
    private JDialog window; 
    private JLabel LinesIgnored; 
    private JLabel LinesProcessed; 
    private JLabel Nodes; 
    private JLabel Links; 
    private JLabel Duplicates; // tracks the number of equivalent data entries found. 
    private JLabel FailedGeoCodingRequests; 
    private JProgressBar progressBar; 
    private JButton cancelButton; 

    public DataMiner(JFrame parentWindow) 
    { 
     nodeTable = new Hashtable<Integer, GISNode>(1000); 
     linkTable = new Hashtable<Integer, GISLink>(1000); 

     numLinesIgnored = 0; 
     numLinesProcessed = 0; 
     numNodes = 0; 
     numLinks = 0; 
     numDuplicates = 0; 
     numFailedGeoCodingRequests = 0; 
     cancelled = false; 

     LinesIgnored = new JLabel(); 
     LinesProcessed = new JLabel(); 
     Nodes = new JLabel(); 
     Links = new JLabel(); 
     Duplicates = new JLabel(); 
     FailedGeoCodingRequests = new JLabel(); 
     cancelButton = new JButton("Cancel"); 
     progressBar = new JProgressBar(); 

     updateLabels(); // assigns a formatted string to each JLabel 
     cancelButton.addActionListener(this); 

    // initialize window 
     window = new JDialog(parentWindow); 
     window.setResizable(false); 
     window.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 

     Container content = window.getContentPane(); 
     content.setLayout(new GridLayout(7,1)); 
     content.add(LinesProcessed); 
     content.add(Nodes); 
     content.add(Links); 
     content.add(Duplicates); 
     content.add(LinesIgnored); 
     content.add(FailedGeoCodingRequests); 
     content.add(progressBar); 
     JPanel p1 = new JPanel(); 
     p1.add(new JLabel("")); // takes up space 
     p1.add(cancelButton); 
     content.add(p1); 
     window.pack(); 
     window.setLocationRelativeTo(parentWindow); 
     window.setVisible(true); 
    } 

    (rest of the class...) 
} 

Sobald dieses Fenster öffnet, hält der Rest des Programms als normal ausgeführt wird, nur dieses Fenster leer ist. Fehle ich etwas?

+0

Wird der Dialog überhaupt angezeigt? Wenn nicht, versuchen Sie zuerst setSize() des JDialogs. –

+0

@pouncep: Es zeigt sich, nur mit nichts drin. – Max

+0

Versuchen Sie, ['invokeLater'] (http://download.oracle.com/javase/tutorial/uiswing/concurrency/initial.html) zu verwenden, um' window.setVisible (true) 'aus dem Event-Dispatch-Thread aufzurufen. – casablanca

Antwort

1

Wahrscheinlich, weil Sie die EventDispatchThread blockieren, auf die GUI kann sich nicht neu streichen. Lesen Sie den Abschnitt aus dem Swing-Lernprogramm unter Concurrency, um weitere Informationen zu erhalten.

Da Sie keine ordnungsgemäße SSCCE bieten, können wir nicht mehr als raten.

+0

Danke. Das sieht aus wie ich brauche. – Max

+0

Beim Debuggen ist es hilfreich, SwingUtilities.isEventDispatchThread() Anrufe in Ihrem Code zu streuen. –

0

Das ist wirklich seltsam. Dieser Code funktioniert ganz gut für mich, dh er zeigt ein leeres Hauptfenster (was richtig ist) und "Pop-ups" Kinderrahmenfenster (was du sagst, funktioniert nicht für dich). Ich habe NetBeans 6.8 und Java 1.6 unter Ubuntu verwendet:

package javaapplication2; 

import java.awt.Container; 
import java.awt.GridLayout; 
import javax.swing.JButton; 
import javax.swing.JDialog; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JProgressBar; 

public class MainFrame extends javax.swing.JFrame { 

    private int numLinesIgnored; 
    private int numLinesProcessed; 
    private int numNodes; 
    private int numLinks; 
    private int numDuplicates; 
    private int numFailedGeoCodingRequests; 
    private boolean cancelled; 

// window objects 
    private JDialog window; 
    private JLabel LinesIgnored; 
    private JLabel LinesProcessed; 
    private JLabel Nodes; 
    private JLabel Links; 
    private JLabel Duplicates; // tracks the number of equivalent data entries found. 
    private JLabel FailedGeoCodingRequests; 
    private JProgressBar progressBar; 
    private JButton cancelButton; 

    /** Creates new form MainFrame */ 
    public MainFrame() { 
     initComponents(); 

     JFrame parentWindow = this; 


     numLinesIgnored = 0; 
     numLinesProcessed = 0; 
     numNodes = 0; 
     numLinks = 0; 
     numDuplicates = 0; 
     numFailedGeoCodingRequests = 0; 
     cancelled = false; 

     LinesIgnored = new JLabel(); 
     LinesProcessed = new JLabel(); 
     Nodes = new JLabel(); 
     Links = new JLabel(); 
     Duplicates = new JLabel(); 
     FailedGeoCodingRequests = new JLabel(); 
     cancelButton = new JButton("Cancel"); 
     progressBar = new JProgressBar(); 

    // initialize window 
     window = new JDialog(parentWindow); 
     window.setResizable(false); 
     window.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 

     Container content = window.getContentPane(); 
     content.setLayout(new GridLayout(7,1)); 
     content.add(LinesProcessed); 
     content.add(Nodes); 
     content.add(Links); 
     content.add(Duplicates); 
     content.add(LinesIgnored); 
     content.add(FailedGeoCodingRequests); 
     content.add(progressBar); 
     JPanel p1 = new JPanel(); 
     p1.add(new JLabel("")); // takes up space 
     p1.add(cancelButton); 
     content.add(p1); 
     window.pack(); 
     window.setLocationRelativeTo(parentWindow); 
     window.setVisible(true); 
    } 

    /** This method is called from within the constructor to 
    * initialize the form. 
    * WARNING: Do NOT modify this code. The content of this method is 
    * always regenerated by the Form Editor. 
    */ 
    @SuppressWarnings("unchecked") 
    // <editor-fold defaultstate="collapsed" desc="Generated Code"> 
    private void initComponents() { 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
     getContentPane().setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 400, Short.MAX_VALUE) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 300, Short.MAX_VALUE) 
     ); 

     pack(); 
    }// </editor-fold> 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String args[]) { 
     java.awt.EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       new MainFrame().setVisible(true); 
      } 
     }); 
    } 

    // Variables declaration - do not modify 
    // End of variables declaration 

} 
+0

Kurz gesagt, ich habe den Rest des Programms, das nach der Anzeige des Dialogfelds lief, auskommentiert und es funktionierte korrekt. Irgendein Grund, warum das passieren würde? – Max

+0

Erste Idee, die mir in den Sinn kommt, ist, dass Sie irgendwie "Inhalt" oder "Fenster" -Objekte verwenden – shybovycha