简体   繁体   English

每次在JFrame中单击任何按钮时,刷新/重新创建卡布局中的所有面板。

[英]Refresh/Recreate All Panel in a Card Layout each time any button is clicked in JFrame Holding these Panels

I have a jFrame witch contains two panel Right Panel and LEFT PANEL.( Both are part of Split Panel) 我有一个jFrame女巫,其中包含两个面板的右面板和左面板。(两者都是拆分面板的一部分)

Right Panel has a card layout. 右面板具有卡布局。

p1=new AddPanel();
p2=new DeletePanel();
c2=new CardLayout(); 
right.setLayout(c2);
right.add(p1,"Add");
right.add(p2,"Delete");

In the left panel, there are 2 buttons (ADD, DELETE). 在左侧面板中,有2个按钮(ADD,DELETE)。

left.add(new JButton("Add");
left.add(new JButton("DELETE");

When i press ADD button- a new panel (AddPanel) shows in the LeftPanel . 当我按下ADD按钮时,新面板(AddPanel)将显示在LeftPanel中。

 private void addActionPerformed(java.awt.event.ActionEvent evt) {                                    
   c2.show(right,"Add");

} 

AddPanel has various textfield and that writes the value to a database. AddPanel具有各种文本字段,该值将值写入数据库。

When i press DELETE button - a new panel (DeletePanel) shows in the LeftPanel. 当我按DELETE按钮时-一个新面板(DeletePanel)将显示在LeftPanel中。

private void deleteActionPerformed(java.awt.event.ActionEvent evt) {                                     
   c2.show(right,"Delete");
}                                    

DeletePanel also has various text fields that retireves all those values from database and there are three buttton :- next , previous ,remove. DeletePanel还具有各种文本字段,这些文本字段将从数据库中检索所有这些值,并且具有三个按钮:-next(上一个),previous(上一个),remove(删除)。

User can navigate through mysql records ,up and down,using next,previous button as well as delete records by pressing REMOVE button and at run time only it gets updated ie the record deleted is not shown again as i again call SELECT statement at REMOVE Button call in DELETEPanel. 用户可以使用下一个,上一个按钮在mysql记录中上下导航,也可以通过按REMOVE按钮删除记录,并且在运行时仅更新记录,即删除的记录不再显示,因为我再次在REMOVE按钮处调用SELECT语句呼叫DELETEPanel。 This all is working. 这一切都在起作用。

Problem arises when i again click on ADD button , submit new record and now try to view records by pressing DELETE Button in LEFT PANEL of JFRAME. 当我再次单击添加按钮,提交新记录并现在尝试通过按JFRAME的左面板中的DELETE按钮来查看记录时,就会出现问题。

How do i make it so each time i press 'DELETE' button (from left panel in jFrame), DeletePanel is created again so that it gets updated result from database. 我如何做到这一点,所以每当我按下“删除”按钮(从jFrame的左面板)时,都会再次创建DeletePanel,以便它从数据库中获取更新结果。

problem i am facing is since DELETE Button was called previously , therefore DELETE PANEL WAS created before and thus it maintain its OLD STATE. 我面临的问题是因为DELETE Button之前被调用过,因此DELETE PANEL是在之前创建的,因此它保持其OLD STATE。 How to RE INITIALIZE DELETE PANEL. 如何重新初始化删除面板。 I dont get new updated record from database. 我没有从数据库获取新的更新记录。 Only works if i again restart application. 仅在我再次重新启动应用程序时有效。
Please help. 请帮忙。

FInally found answer and posting Response here ANSWER IS TO USE COMPONENT LISTENER to re-initialize a panel each time it is viewed..... 最后找到答案并在此处发布响应每次查看该面板时,答案是使用COMPONENT LISTENER重新初始化面板.....

public class DeletePanel implements ComponentListener 公共类DeletePanel实现ComponentListener

and then inorder to add panel ie DELETEPANEL to a listnener, which i require in my question, use this line. 然后为了将面板(即DELETEPANEL)添加到侦听器,我需要在我的问题中使用此行。

this.addComponentListner(this); this.addComponentListner(this); // in deletepanel construtor. //在deletepanel构造函数中。

then define overridden Component Shown method to initialize things. 然后定义重写的Component Shown方法来初始化事物。 EX- like in my case select statement. EX-就像我的选择语句一样。

@Override
public void componentShown(ComponentEvent e) {
     try {
        Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/online", "root","");
        st = con.prepareStatement("select question,op1,op2,op3,op4 from java");
        rs = st.executeQuery();
    } catch (ClassNotFoundException | SQLException ex) {
  }
}

This Works 100% 这有效100%

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

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