1

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

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 实例?(注意:必须在运行时创建 infoIntFrames)

4

1 回答 1

1

有两个领域:

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

2)在创建新的另一个JInternalFrame之前,您必须检查返回desktopPane.getAllFrames()的数组中是否存在,添加InternalFrameListener,因为(AFAIK)方法 desktopPane.getAllFrames() 仅返回可见JInternalFrame

于 2012-02-10T13:27:29.380 回答