如何跟踪我的内部框架?

时间:2012-02-09 22:01:57

标签: java swing jtable jinternalframe

此示例代码是我实际程序的简短版本:

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.*;


public class Example extends JFrame {

private JPanel contentPane;
private JTable table;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Example frame = new Example();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public Example() {
    //what to do @ close
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 792, 585);

    //content pane
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    contentPane.setLayout(new BorderLayout(0, 0));
    setContentPane(contentPane);

    //create desktop pane add it to content pane
    final JDesktopPane desktopPane = new JDesktopPane();
    contentPane.add(desktopPane, BorderLayout.CENTER);

    //create Int Frame with table
    JInternalFrame tableIntFrame = new JInternalFrame("TableIntFrame");
    tableIntFrame.setBounds(31, 29, 300, 205);
    desktopPane.add(tableIntFrame);

    //create the table
    table = new JTable();

    table.setFillsViewportHeight(true);
    table.setModel(new DefaultTableModel(
        new Object[][] {
            {"Row 0 (click for more info)"},
            {"Row 1 (click for more info)"},
        },
        new String[] {
            "Collumn 0"
        }
    ));

    //add the table to a ScrollPane
    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setViewportView(table);
    //add Scrollpane to table       
    tableIntFrame.getContentPane().add(scrollPane, BorderLayout.CENTER);
    tableIntFrame.setVisible(true);

    //Listen for events on selection
    table.getSelectionModel().addListSelectionListener(new
            ListSelectionListener() {
        public void valueChanged(ListSelectionEvent e) {

            // only fires ones
            if (! e.getValueIsAdjusting())
            { 
                //create info frame with title set to selectedrow index
                createFrame(desktopPane, table.getSelectedRow()+"");

            }

        }
    });
    // Allow only one row to be selected.
    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);



}

//Creates an int frame with the title set to the row
private void createFrame(JDesktopPane desktopPane, String selectedRow){

    JInternalFrame InfoIntFrame = new JInternalFrame("Info Row "+selectedRow);
    InfoIntFrame.setBounds(425, 44, 183, 72);
    //add to desktop
    desktopPane.add(InfoIntFrame);
    //set visible
    InfoIntFrame.setVisible(true);
    }
}

如果相应的infoIntFrame已经打开,如何点击一行,如何创建另一个infoIntframe实例? (注意:必须在运行时创建infoIntFrame)

1 个答案:

答案 0 :(得分:1)

有两个方面:

1)JInternalFrame tableIntFrame = new JInternalFrame("TableIntFrame");失去焦点

2)你必须检查在创建新的desktopPane.getAllFrames()之前是否存在返回JInternalFrame的数组中,添加InternalFrameListener,因为(AFAIK)方法desktopPane.getAllFrames()仅返回visible { {1}}