关闭和重新打开后,JDialog不显示面板

时间:2015-06-12 11:41:01

标签: java swing model-view-controller jdialog

关闭对话框后JDialog没有显示JPanel,然后如果用户立即重复该操作,它根本没有显示面板,而是一个纯黑色的对话框,左上角有一个小白框角。我尝试过创建一个SSCCE但是无法开发一个。如果我在MainControl类中删除窗口适配器,它将在关闭并重复打开对话框后继续向对话框添加另一个面板。下面是我尝试创建的SSCCE的代码,但它仍然表现相同。下面的代码是运行对话框所需的最小量。

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;
  }
}

1 个答案:

答案 0 :(得分:1)

你应该使用

dialog.getContentPane().add(panel);

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

你也不能放置对话框,