简体   繁体   English

如何使用 ComboBox.getSelectedIndex() 在一帧中更改 jPanels

[英]How to change jPanels in one frame with using ComboBox.getSelectedIndex()

I want to switch panels with using ComboBox.我想使用 ComboBox 切换面板。 For this, I defined a listener like:为此,我定义了一个监听器,如:

cmbChooseAction.addPropertyChangeListener(new PropertyChangeListener() {
        public void propertyChange(PropertyChangeEvent evt) {
            
            String whichPanel = cmbChooseAction.getSelectedItem().toString();
            
            getCurrentPanel(whichPanel);
            
        }
});

I am sending the String value this section as a parameter to the sql query: "from Database where name = ?"我将此部分的字符串值作为参数发送到 sql 查询:“from Database where name = ?” And I want to return WhichPanel value of the row that provides this query.我想返回提供此查询的行的 whichPanel 值。 So I wrote the following function:所以我写了以下函数:

public List<ComboBoxItem> getComboBoxItem(String where) {
    
    List<ComboBoxItem> cmbActions = new ArrayList<ComboBoxItem>();
    
    try {
        
        session.beginTransaction();
        
        cmbActions = session.createQuery("from ComboBoxItem where name = '" + where + "'").getResultList(); 
//name == action column
        
        session.getTransaction().commit();
        
    } finally {
        factory.close();
    }
    
    return cmbActions;
    
}

数据库图像

public void getCurrentPanel(String where) {
    
    List<ComboBoxItem> resultList = comboBoxItemDal.getComboBoxItem(where);
    
    for(ComboBoxItem comboBoxItem: resultList) {
        switchPanel(comboBoxItem.getPanelName()); //It's invalid!
    }

}

------------------------------------------------------------------------------------- -------------------------------------------------- ---------------------

My problem is I can't convert String to JPanel and looking for alternative solutions like this: (My English is a bit poor so I couldn't explain my problem completely)我的问题是我无法将 String 转换为 JPanel 并寻找这样的替代解决方案:(我的英语有点差,所以我无法完全解释我的问题)

public void switchPanel(JPanel panel) {
    
    //How should I change here?
    layeredPane.removeAll();
    layeredPane.add(panel);
    layeredPane.repaint();
    layeredPane.revalidate();
    
}

Can anybody help?有人可以帮忙吗?

If you are curious, it can be solved CardLayout like @camickr said.如果你好奇,它可以像@camickr 所说的那样解决 CardLayout。

public void switchPanel(String panel) {

   CardLayout cardLayout = (CardLayout) (layeredPane.getLayout());
   cardLayout.show(layeredPane, panel)

}

However, when saving these panels, Eclipse IDE set them to layeredPane with a random name.但是,在保存这些面板时,Eclipse IDE 将它们设置为具有随机名称的 layeredPane。 Also need to pay attention here.这里也需要注意。 The show method will recognize the String value here, you can change it as you like. show 方法会识别这里的 String 值,您可以随意更改它。

pnlSearch = new JPanel();
pnlSearch.setBackground(Color.WHITE);
layeredPane.add(pnlSearch, "name_89127391238sd");
// layeredPane.add(pnlSearch, "pnlSearch");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM