首页 > 解决方案 > Java swing - 无法显示表格

问题描述

我有一个简单的 MVC 应用程序,我想显示表格。这是我的视图类

public class NaklView {

    public JFrame frame;
    private JTable table;                     


    /**
     * Create the application.
     */
    public NaklView(String[][] data, Client client) {
        initialize(data,client);
    }
        private class AreYouSure extends WindowAdapter { 
            Client client;
        public AreYouSure(Client client)
        {
                this.client = client;
        }
        public void windowClosing( WindowEvent e ) {  
            int option = JOptionPane.showOptionDialog(  
                    frame,  
                    "Are you sure you want to quit?",  
                    "Exit Dialog", JOptionPane.YES_NO_OPTION,  
                    JOptionPane.WARNING_MESSAGE, null, null,  
                    null );  
            if( option == JOptionPane.YES_OPTION ) { 
                try {
                    client.getClientBuffWriter().write("QUIT");
                    client.getClientBuffWriter().flush();
                    System.out.println("Disconnected.");
                    System.exit(0);
                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(frame, "Can not disconnect.");
                }
            }
        }
        }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize(String[][] data, Client client) {
        frame = new JFrame();
                frame.setTitle("Nakladatelství");
        frame.setBounds(100, 100, 800, 400);
                frame.setMinimumSize(new Dimension(500, 300));
                frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
                frame.addWindowListener(new AreYouSure(client));

        JMenuBar menuBar = new JMenuBar();
        frame.setJMenuBar(menuBar);

        JMenu fileMenu = new JMenu("File");
            JMenu importMenu = new JMenu("Add");

            JMenuItem addKniha = new JMenuItem("Přidat knihu");
            addKniha.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                    AddKniha agd = new AddKniha(client);
                    agd.setVisible(true);              
            }
            });
            JMenuItem addNosic = new JMenuItem("Přidat digitální nosič");
            addNosic.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                    AddDigNosic al = new AddDigNosic(client);
                    al.setVisible(true);
            }
            });
            JMenuItem addCas = new JMenuItem("Přidat časopis");
            addCas.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                    AddCasopis el = new AddCasopis(client);
                    el.setVisible(true);
            }
            });
            JMenuItem addKat = new JMenuItem("Přidat kategorii");
            addKat.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                    AddKat md = new AddKat(client);
                    md.setVisible(true);
            }
            });
            JMenuItem addNakl = new JMenuItem("Přidat nakladatelství");
            addNakl.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                    AddNakl id = new AddNakl(client);
                    id.setVisible(true);
            }
            });
            JMenuItem addAutor = new JMenuItem("Přidat autora");
            addAutor.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                    AddAutor ap = new AddAutor(client);
                    ap.setVisible(true);
            }
            });

            importMenu.add(addKniha);
            importMenu.add(addNosic);
            importMenu.add(addCas);
            importMenu.add(addKat);
            importMenu.add(addNakl);
            importMenu.add(addAutor);

            fileMenu.add(importMenu);


                menuBar.add(fileMenu);
                GridBagLayout gridBagLayout = new GridBagLayout();
                gridBagLayout.columnWidths = new int[]{0, 0, 0};
                gridBagLayout.rowHeights = new int[]{0, 0, 0, 0};
                gridBagLayout.columnWeights = new double[]{1.0, 0.0, Double.MIN_VALUE};
                gridBagLayout.rowWeights = new double[]{0.0, 0.0, 1.0, Double.MIN_VALUE};
                frame.getContentPane().setLayout(gridBagLayout);

                JButton btnBack = new JButton("Back");
                btnBack.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                            Menu menu = new Menu(client);
                            frame.dispose();
                            menu.frame.setVisible(true);
                        }
                });
                GridBagConstraints gbc_btnBack = new GridBagConstraints();
                gbc_btnBack.anchor = GridBagConstraints.EAST;
                gbc_btnBack.insets = new Insets(0, 0, 5, 5);
                gbc_btnBack.gridx = 0;
                gbc_btnBack.gridy = 0;
                frame.getContentPane().add(btnBack, gbc_btnBack);

                JButton btnRefresh = new JButton("Refresh");
                btnRefresh.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        Menu m = new Menu(client);
                        frame.dispose();
                        m.getKnihy(client);

                    }
                });
                GridBagConstraints gbc_btnRefresh = new GridBagConstraints();
                gbc_btnRefresh.anchor = GridBagConstraints.EAST;
                gbc_btnRefresh.insets = new Insets(0, 0, 5, 5);
                gbc_btnRefresh.gridx = 1;
                gbc_btnRefresh.gridy = 0;
                frame.getContentPane().add(btnRefresh, gbc_btnRefresh);


                JButton btnAddNakl = new JButton("Přidat nakladatelství");
                btnAddNakl.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        AddNakl dialog = new AddNakl(client);
            dialog.setVisible(true);
                        if(!dialog.isVisible())
                        {
                            Menu m = new Menu(client);
                            m.getNakladatelstvi(client);
                            frame.dispose();
                        }
                    }
                });
                GridBagConstraints gbc_btnAddEmployee = new GridBagConstraints();
                gbc_btnAddEmployee.anchor = GridBagConstraints.EAST;
                gbc_btnAddEmployee.insets = new Insets(0, 0, 5, 0);
                gbc_btnAddEmployee.gridx = 2;
                gbc_btnAddEmployee.gridy = 0;
                frame.getContentPane().add(btnAddNakl, gbc_btnAddEmployee);

                JSeparator separator = new JSeparator();
                GridBagConstraints gbc_separator = new GridBagConstraints();
                gbc_separator.fill = GridBagConstraints.HORIZONTAL;
                gbc_separator.gridwidth = 3;
                gbc_separator.insets = new Insets(0, 0, 5, 5);
                gbc_separator.gridx = 0;
                gbc_separator.gridy = 1;
                frame.getContentPane().add(separator, gbc_separator);

                JScrollPane scrollPane = new JScrollPane();
                GridBagConstraints gbc_scrollPane = new GridBagConstraints();
                gbc_scrollPane.gridwidth = 3;
                gbc_scrollPane.fill = GridBagConstraints.BOTH;
                gbc_scrollPane.gridx = 0;
                gbc_scrollPane.gridy = 2;
                frame.getContentPane().add(scrollPane, gbc_scrollPane);


                Object rowData[][] = data;
                Object columnNames[] = { "IČO", "Název"};

                    JTable table = new JTable(rowData, columnNames) {
                    public boolean editCellAt(int row, int column, java.util.EventObject e) {
                          return false;
                       }
                    };
                    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
                    scrollPane.setViewportView(table);

                    table.addMouseListener(new MouseAdapter() {
                        public void mouseClicked(MouseEvent e) {
                           if (e.getClickCount() == 2) {
                              JTable target = (JTable) e.getSource();
                                int row = target.getSelectedRow();
                                String[] data = new String[2];
                                data[0] = rowData[row][0].toString();
                                data[1] = rowData[row][1].toString();
                                EditNakl ee = new EditNakl(client,data);
                                ee.setVisible(true);
                           }
                        }
                     });

                scrollPane.setViewportView(table);

    }

这是我的菜单类(程序方向的默认菜单)

public void getNakladatelstvi(Client client)
            {
                String data ="";
                try {
                    client.getClientBuffWriter().write("READALL;NAKL\r\n");
                    client.getClientBuffWriter().flush();
                    data = client.getClientBuffReader().readLine();
                    Formater f = new Formater();
                    String[][] content = null;
                    if(data.contains(";"))
                    {
                        content = f.parseBySemicolon(f.parseByBackslash(data), 5);
                        NaklView iw = new NaklView(content,client);
                        frame.dispose();
                        iw.frame.setVisible(true);
                    }
                    else
                    {
                        throw new Exception();
                    }
                }
                catch (Exception ex)
                {
                    JOptionPane.showMessageDialog(frame, data);
                }               
            }

这是我的控制器类(只有 readall 方法)

@Override
    public String readall(String[] data) {
        String result = "";
        try
        {
            System.out.println(this.getEm().hashCode());
            this.getEm().clear();
            this.getEm().getTransaction().begin();
            Query query = this.getEm().createQuery("SELECT n FROM Nakladatelstvi n");
            List<Nakladatelstvi> ems = query.getResultList();
            this.getEm().getTransaction().commit();
            if(!ems.isEmpty())
            {
                for (Nakladatelstvi n : ems) 
                {
                    result += n.toString() + "/";       
                }
            }
            else
                result = "No nakladatestvi in database!";
        }
        catch (Exception ex)
        {
            result = "Can not proceed transaction.";
        }
        return result;        
    }

我只想从数据库中查看表。如果我运行程序并单击“Nakladatelstvi”按钮,则会显示以下内容 - https://drive.google.com/open?id=0B859nptdnI1Vbkd5UlFWVy1pckE

我会很高兴每一个建议!谢谢你。

标签: javaswingmodel-view-controller

解决方案


推荐阅读