Java 具有相同JTable的多个文档
我正在开发一个Java应用程序,它使用JTable捕获用户输入。用户必须打印表中捕获的数据。我希望用户打开多个文档进行处理。我是这样实现的:我将JFrame作为我的主窗口;在此JFrame上,我添加了JTabbedPane,以便用户可以在文件、设置和工具之间切换。单击“新建文件”后,用户将被带到JFrame中心的JTabbedPane位置,并使用JTable进行输入。我希望下次用户单击new File时,会向JTabblePane添加一个新的JPanel;但是这个新的JPanel还应该包含用于输入的JTable。每次用户创建新文件时,这种行为都应该继续。这是显示在我上传的图片。请原谅拙劣的绘图 我通过以下代码实现了这一点:Java 具有相同JTable的多个文档,java,swing,jtable,mdi,Java,Swing,Jtable,Mdi,我正在开发一个Java应用程序,它使用JTable捕获用户输入。用户必须打印表中捕获的数据。我希望用户打开多个文档进行处理。我是这样实现的:我将JFrame作为我的主窗口;在此JFrame上,我添加了JTabbedPane,以便用户可以在文件、设置和工具之间切换。单击“新建文件”后,用户将被带到JFrame中心的JTabbedPane位置,并使用JTable进行输入。我希望下次用户单击new File时,会向JTabblePane添加一个新的JPanel;但是这个新的JPanel还应该包含用于输
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.addNew Quote、myCreatedBeforePanleWithTable,但它正在覆盖上一个选项卡
这里是我想要的东西的图片。我读了关于JDeskTopPane和JInternalFrame的文章,但不知道如何让它以我想要的方式工作
如何使其按图中所示的方式工作?不幸的是,Swing组件只能有一个。如果将组件A添加到JPanel X,然后再添加到JPanel Y,它将以Y结尾
您需要有三个单独的JTables,它们共享一个TableModel。这对于实现基本功能来说应该相当简单。如果您允许排序和列重新排序,并且您希望这会影响所有3个表,那么这可能会变得很棘手。我无法将我的JTable添加到运行时创建的每个面板中,它听起来真的很像应用程序。应该有一个表,没有选项卡,3个按钮用于3个操作。否则,请在任意数量的JTable组件中使用相同的表模型。@AndrewThompson如何为运行时创建的每个面板使用一个表模型?一个示例代码和链接会很有帮助。@AndrewThompson但我发布了当用户点击“新建文件”按钮时执行的必要代码。“我想那会有帮助的。”安德烈·霍姆普森好吧,那么让我加上它。让我们。