首页 文章

具有相同JTable的多个文档

提问于
浏览
0

我正在开发一个使用JTable捕获用户输入的Java应用程序 . 用户必须打印表中捕获的数据 . I would want the user to open multiple documents to work on . 我这样实现了它:我将JFrame作为主窗口;在这个JFrame上,我添加了JTabbedPane,以便用户可以在文件,设置和工具之间切换 . 单击新文件,用户将被带到JTabbedPane(位于JFrame的中心),用于输入JTable . 我希望下次用户点击新文件时,新的JPanel会被添加到JTabbledPane;但是这个新的JPanel还应该包含用于输入的JTable . 每次用户创建新文件时,这种行为都应该继续 . 这在我上传的图像中显示 . (请原谅不良绘图) .

我通过这段代码得到了这个:

public final class QuotPane {

//components to be used
JFrame frame;
JTabbedPane tabbedPane,tablePane;
JPanel quotPane, topPane, pane1, pane2, pane3,tablePanel;
JSeparator sep;
JButton newFile;

QuotPane() {
    this.createQuotPane();
    this.createGUI();
    ButtonActionListener lits= new ButtonActionListener();
    newFile.addActionListener(lits);

}

public void createGUI() {
    tabbedPane = new JTabbedPane();
    tabbedPane.addTab("Create Quot", quotPane);

    frame = new JFrame("Qout Interface");
    frame.setSize(new Dimension(700, 650));
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new BorderLayout());
    //adding the tabbed pane to the frmae

    frame.add(tabbedPane, BorderLayout.NORTH);
}

public void createQuotPane() {

    quotPane = new JPanel();
    quotPane.setLayout(new BorderLayout());
    //creating the top pane
    topPane = new JPanel();
    topPane.setPreferredSize(new Dimension(650, 145));
    topPane.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED, Color.lightGray, Color.lightGray, Color.white, Color.orange));
    topPane.setLayout(new MigLayout());
    //add the top pane on the quot panel

    quotPane.add(topPane, BorderLayout.NORTH);
    //adding the panes on the top pane
    this.createPanels();
    topPane.add(pane1);
    topPane.add(pane2);
    topPane.add(pane3);

}

//a method to create panes for options
public void createPanels() {
    pane1 = new JPanel();
    pane1.setPreferredSize(new Dimension(200, 140));
    pane1.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED, Color.BLUE, Color.lightGray, Color.white, Color.orange));
    //lets set the icons then
    pane1.setLayout(new MigLayout());
    Icon fileIcon = new ImageIcon(this.getClass().getResource("/images/fil.jpg"));
    newFile = new JButton(fileIcon);
    pane1.add(new JLabel("New File"), "wrap");
    pane1.add(newFile,"width 20!");




    pane2 = new JPanel();
    pane2.setPreferredSize(new Dimension(200, 140));
    pane2.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED, Color.BLUE, Color.lightGray, Color.white, Color.orange));

    pane3 = new JPanel();
    pane3.setPreferredSize(new Dimension(200, 140));
    pane3.setMaximumSize(new Dimension(300, 140));
    pane3.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED, Color.BLUE, Color.lightGray, Color.white, Color.orange));

}

public static void main(String[] args) {

    QuotPane qp = new QuotPane();
}

public void createTablePane(){
tablePanel= new JPanel();

}

class ButtonActionListener implements ActionListener{
 protected int count=0;
@Override
public void actionPerformed(ActionEvent e) {
    if(count==0){
    if(e.getSource().equals(QuotPane.this.newFile)){
       tablePane=new JTabbedPane();
       QuotPane.this.createTablePane();
       tablePane.addTab("New Qout", tablePanel);
       frame.add(tablePane,BorderLayout.CENTER);
        count ++;
        }
    }else if(count>0)
    {
  tablePane.add("New Quote",new JPanel());
    }}}}

我在这里遇到的挑战是我无法将JTable添加到运行时创建的每个面板 . 我试过这个: tablePane.add("New Quote",myCreatedBeforePanleWithTable) 但它覆盖了前一个选项卡 .

这里是我想要的东西的图像 . 我读到了JDeskTopPane和JInternalFrame,但无法弄清楚如何让它按照我想要的方式工作 .

enter image description here

如何让它按照我想要的方式工作,如图所示?

1 回答

  • 0

    不幸的是,Swing组件只能有single parent . 如果将组件A添加到JPanel X然后再添加到JPanel Y,它将以Y结尾 .

    您需要有三个独立的JTable,它们共享一个TableModel . 这应该是相当简单的实现基本功能 . 如果您允许排序和列重新排序并且您希望它影响所有3个表,则可能会变得棘手 .

相关问题