简体   繁体   中英

Multiple documents with the same JTable

I am developing a Java Application that uses a JTable for capturing user input. The user will have to print data captured in the table. I would want the user to open multiple documents to work on . This I have implemented it this way: I have my JFrame as my main window; on to this JFrame, I have added JTabbedPane so that the user can switch between File, Settings and Tools.Upon clicking new File, the user is taken to a JTabbedPane( place at the center of the JFrame ) with a JTable for input. I would want that the next time the user click new File, a new JPanel would be added to the JTabbledPane; but this new JPanel should also contain the JTable for input.This behavior should continue everytime the user creates a new File. This is shown in the images that I have uploaded.(Please forgive for poor drawing).

I have achied this by this code:

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());
    }}}}

The challenge I am facing here is that I cannot add my JTable to every panel created at run time.I have tried this: tablePane.add("New Quote",myCreatedBeforePanleWithTable) but it is overriding the previous tab.

Here are images of things that I want.I read about JDeskTopPane and JInternalFrame, but can't figure out how to make it work the way I want.

在此处输入图片说明

How do I make it work the way I want as shown in the image?

Unfortunately, a Swing Component can only have a single parent . If you add Component A to JPanel X and then to JPanel Y, it will end up in Y.

You will need to have three separate JTables, all sharing a single TableModel. This should be fairly straightforward to implement the basic functionality. It might get tricky if you allow sorting and column reordering and you want that to affect all 3 tables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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