2015-06-12 5 views
-1

JDialog не показывает JPanel после закрытия диалога, а затем, если пользователь сразу повторяет действие, он не отображает панель вообще, а диалог, который является сплошным черным с маленькая белая коробка в верхнем левом углу. Я попытался создать SSCCE, но не смог его разработать. Если я удалю оконный адаптер в классе MainControl, он просто добавит еще одну панель в диалог после закрытия и повторит открытие диалога. Ниже приведен код SSCCE, который я пытался создать, но он по-прежнему ведет себя одинаково. Код ниже - это минимальная сумма, необходимая для запуска диалога.JDialog не показывает панель после закрытия и повторного открытия

public class CreateAndShowUI { 
    public static void createUI() { 
    JFrame frame = new JFrame(); 
    MainPanel mainPanel = new MainPanel(); 
    Dialog dialog = new Dialog(); 
    MainControl mainControl = new MainControl(frame, mainPanel, 
      dialog); 

    frame.getContentPane().add(mainPanel.getMainPanel()); 
    frame.setUndecorated(true); 
    frame.setPreferredSize(new Dimension(1100, 550)); 
    frame.pack(); 
    frame.setLocationRelativeTo(null); 
    frame.setVisible(true); 

    } 
    public static void main(String[] args) { 
    createUI(); 
    } 
} 


public class MainControl { 
    private JFrame frame; 
    private MainPanel mainPanel; 
    private Dialog dialog; 

    public MainControl(JFrame frame, MainPanel panel, Dialog dialog) { 
     this.frame = frame; 
     this.mainPanel = panel; 
     this.dialog = dialog; 
     mainPanel.getTable().addMouseListener(new MouseListener()); 
     dialog.getDialog().addWindowListener(new DialogWindowListener()); 
    } 

    public class MouseListener extends MouseAdapter { 
     @Override 
     public void mousePressed(MouseEvent evt) { 
      if (evt.getButton() == MouseEvent.BUTTON3) { 
       dialog.showDialog(new KeywordPanel().getKeywordPanel(), 
         evt.getXOnScreen(), evt.getYOnScreen()); 
      } 
     } 
    } 

    public class DialogWindowListener extends WindowAdapter { 
     @Override 
     public void windowClosing(final WindowEvent event) { 
      dialog.getDialog().dispose(); 
      dialog.getDialog().removeAll(); 
     } 
    } 
} 


public class MainPanel { 
    private JPanel mainPanel; 
    private JScrollPane listScrollPane; 
    private JTable table; 

    public MainPanel() { 
     mainPanel = new JPanel(new MigLayout("", "", "[]13[]")); 
     table = new JTable(new ProductTableModel()); 
     listScrollPane = new JScrollPane(table, 
       JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, 
       JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 
     listScrollPane.setPreferredSize(new Dimension(850, 990)); 
     mainPanel.add(listScrollPane, "cell 0 1"); 
    } 

    public JPanel getMainPanel() { 
     return mainPanel; 
    } 

    public JTable getTable() { 
     return table; 
    } 
} 


public class KeywordPanel { 
    private JPanel keywordPanel; 
    private JLabel searchLbl; 

    public KeywordPanel() { 
    searchLbl = new JLabel("KeyWords"); 
    keywordPanel = new JPanel(); 
    keywordPanel.add(searchLbl); 
    } 

    public JPanel getKeywordPanel() { 
    return keywordPanel; 
    } 

} 


public class Dialog { 
    private JDialog dialog = new JDialog(); 

    public Dialog() { 
     dialog.setLayout(new MigLayout()); 
    } 

    public void showDialog(JPanel panel, int x, int y) { 
     dialog.add(panel); 
     dialog.pack(); 
     dialog.setVisible(true); 
    } 

    public JDialog getDialog() { 
     return dialog; 
    } 

} 


public class ProductTableModel extends AbstractTableModel { 
    private ArrayList<Object> list = new ArrayList<Object>(); 
    private String[] columnNames = { "ID", "Description", "Inventory", 
     "Minimum Quantity", "Cost", "Order Quantity" }; 

    public ProductTableModel() { 
    list.add(new Product("Sup", "Sup", "Sup", 10, 20, "null")); 
    } 

    @Override 
    public int getColumnCount() { 
    return columnNames.length; 
    } 

    @Override 
    public String getColumnName(int column) { 
    switch (column) { 
    case 0: 
     return "<html>ID<br></html>"; 
    case 1: 
     return "<html>Description<br></html>"; 
    case 2: 
     return "<html>Inventory<br></html>"; 
    case 3: 
     return "<html>Minimum<br>Quantity</html>"; 
    case 4: 
     return "<html>Cost<br></html>"; 
    case 5: 
     return "<html>Order<br>Quantity</html>"; 
    default: 
     return null; 
    } 
    } 

    @Override 
    public int getRowCount() { 
    return list.size(); 
    } 

    @Override 
    public boolean isCellEditable(int row, int column) { 
    return true; 
    } 

    @Override 
    public Object getValueAt(int row, int column) { 

    if (list.get(row) instanceof Product) { 

     Product product = (Product) list.get(row); 
     switch (column) { 
     case 0: 
      return product.getId(); 
     case 1: 
      return product.getProductDescription(); 
     case 2: 
      return product.getQtyOnHand(); 
     case 3: 
      return product.getMinQty(); 
     case 4: 
      return product.getCost(); 
     case 5: 
      return product.getOrderQty(); 
     default: 
      throw new IndexOutOfBoundsException(); 
     } 

    } else { 
     return null; 
    } 
    } 
} 



public class Product { 
    private int minQty; 
    private double cost; 
    private String productDescription, id, category, qtyOnHand, orderQty; 

    public Product(String id, String productDescription, String qtyOnHand, 
     int minQty, double cost, String orderQty) { 

    this.setQtyOnHand(qtyOnHand); 
    this.setOrderQty(orderQty); 
    this.setId(id); 
    this.setMinQty(minQty); 
    this.setCost(cost); 
    this.setProductDescription(productDescription); 
    } 

    public String getQtyOnHand() { 
    return qtyOnHand; 
    } 

    public void setQtyOnHand(String qtyOnHand) { 
    this.qtyOnHand = qtyOnHand; 
    } 

    public String getOrderQty() { 
    return orderQty; 
    } 

    public void setOrderQty(String orderQty) { 
    this.orderQty = orderQty; 
    } 

    public String getId() { 
    return id; 
    } 

    public void setId(String id) { 
    this.id = id; 
    } 

    public double getCost() { 
    return cost; 
    } 

    public void setCost(double cost) { 
    this.cost = cost; 
    } 

    public String getProductDescription() { 
    return productDescription; 
    } 

    public void setProductDescription(String productDescription) { 
    this.productDescription = productDescription; 
    } 

    public int getMinQty() { 
    return minQty; 
    } 

    public void setMinQty(int minQty) { 
    this.minQty = minQty; 
    } 

    public String getCategory() { 
    return category; 
    } 

    public void setCategory(String category) { 
    this.category = category; 
    } 
} 
+2

SSCCE ... вы разместили код 8 классов, и это даже не иллюстрирует проблему, поскольку она работает так, как ожидалось. Это не SSCCE – Robin

ответ

1

Вы должны использовать

dialog.getContentPane().add(panel); 

и

dialog.getDialog().getContentPane().removeAll(); 

И вы не должны утилизировать диалог,

+0

спасибо, полностью решил мою проблему. Я буду использовать setVisible вместо dispose. – Grim

+0

setVisible также не требуется: по умолчанию для закрытия диалогового окна по умолчанию установлено значение HIDE, поэтому в любом случае диалоговое окно будет невидимым. –