将侦听器添加到将仅称为onload的JPanel中 [英] Add listener to JPanel that will be only called onload

查看:108
本文介绍了将侦听器添加到将仅称为onload的JPanel中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题很简单:

在C#WinForms中,您在Winform上有一个名为onload> 加载winforms

这也是我还要在JPanels中创建的内容,因为我想用数据库中的数据填充JList。

In C# WinForms you got a listener on the Winform called onload > onload winforms.
This is what I also want to create in my JPanels, because I want to fill my JList with data from the database.


我尝试使用MouseMotionListener,但是随后我需要一个Timer来控制对数据库的select语句,否则他将成为垃圾邮件

I tried using the MouseMotionListener but then I need a Timer which controls the select statement to the database, otherwise he would spam the database with the select statement.

我得到了以下代码来切换面板:

I got this code to switch panels:

public void changePanel(String panelRef) {
    ((CardLayout) (getContentPane().getLayout())).show(getContentPane(), panelRef);
}

实例化面板并将其添加到JFrame中。

The panels are instantiated and added in the JFrame.

推荐答案

我看到您正在使用CardLayout,并且您想知道何时可以看到卡片。您可以在面板上使用HierarchyListener。

I see you are using a CardLayout and you want to know when a card is made visible. You can use a HierarchyListener on your panel.

监听面板可见的基本代码为:

The basic code to listen for the panel becoming visible would be:

@Override
public void hierarchyChanged(HierarchyEvent e)
{
    JComponent component = (JComponent)e.getSource();

    if ((HierarchyEvent.SHOWING_CHANGED & e.getChangeFlags()) != 0
    &&  component.isShowing())
    {
        // add code here
    }
}

FWIW,此方法用于< a href = http://tips4java.wordpress.com/2008/10/31/card-layout-focus/ rel = nofollow>卡片布局重点,它是对 CardLayout ,可在激活卡片时提供其他功能。

FWIW, this approach is used in Card Layout Focus which is an extension to CardLayout that provides additional functionality when a card becomes active.

这篇关于将侦听器添加到将仅称为onload的JPanel中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆