尽管setVisible,JTable仍然不可见(true)

时间:2014-01-18 23:07:56

标签: java swing jtable

我在NetBeans中有以下面板(只有前三分之一是我的工作,其余的是自动生成和不可修改的):

public class ScorePanel extends javax.swing.JPanel {

private DefaultTableModel dtm;

public ScorePanel() {
    initComponents();
    dtm = (DefaultTableModel) scoreTable.getModel();
    for(int i = dtm.getRowCount() - 1; i >= 0; i--)
        dtm.removeRow(i);
    loadFile();
}

private void loadFile(){
    FileInputStream ins = null;
    try {
        ins = new FileInputStream("./src/Resources/score.txt");
        Scanner fScanner = new Scanner(ins, "UTF-8");
        String[] str;
        while(fScanner.hasNextLine()){
            str = fScanner.nextLine().split(";");
            dtm.addRow(new Object[]{str[0].trim(), Integer.parseInt(str[1].trim()), str[2]});
        }
        fScanner.close();
    } catch (FileNotFoundException ex) {
        Logger.getLogger(ScorePanel.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            if(ins != null)
                ins.close();
        } catch (IOException ex) {
            Logger.getLogger(ScorePanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

// The rest of the code was auto-generated by NetBeans and cannot be modified

private void initComponents() {

    titleLabel = new javax.swing.JLabel();
    jScrollPane1 = new javax.swing.JScrollPane();
    scoreTable = new javax.swing.JTable();
    menuButton = new javax.swing.JButton();
    nameField = new javax.swing.JTextField();
    scoreLabel = new javax.swing.JLabel();
    timeLabel = new javax.swing.JLabel();
    addButton = new javax.swing.JButton();
    saveButton = new javax.swing.JButton();

    setMaximumSize(new java.awt.Dimension(640, 480));
    setMinimumSize(new java.awt.Dimension(640, 480));
    setPreferredSize(new java.awt.Dimension(640, 480));
    setLayout(null);

    titleLabel.setFont(new java.awt.Font("Snap ITC", 2, 36)); // NOI18N
    titleLabel.setForeground(new java.awt.Color(51, 102, 0));
    titleLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    titleLabel.setText("Zaku Attack!");
    add(titleLabel);
    titleLabel.setBounds(170, 56, 300, 47);

    scoreTable.setAutoCreateColumnsFromModel(false);
    scoreTable.setAutoCreateRowSorter(true);
    scoreTable.setModel(new javax.swing.table.DefaultTableModel(
        new Object [][] {
            {null, null, null},
            {null, null, null},
            {null, null, null},
            {null, null, null}
        },
        new String [] {
            "Name", "Score", "Time"
        }
    ) {
        Class[] types = new Class [] {
            java.lang.String.class, java.lang.Integer.class, java.lang.String.class
        };
        boolean[] canEdit = new boolean [] {
            false, false, false
        };

        public Class getColumnClass(int columnIndex) {
            return types [columnIndex];
        }

        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return canEdit [columnIndex];
        }
    });
    scoreTable.setRowSelectionAllowed(false);
    scoreTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
    jScrollPane1.setViewportView(scoreTable);

    add(jScrollPane1);
    jScrollPane1.setBounds(160, 191, 320, 190);

    menuButton.setText("Back to Menu");
    menuButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            menuButtonActionPerformed(evt);
        }
    });
    add(menuButton);
    menuButton.setBounds(250, 410, 137, 23);
    add(nameField);
    nameField.setBounds(160, 170, 130, 20);
    add(scoreLabel);
    scoreLabel.setBounds(290, 170, 60, 20);
    add(timeLabel);
    timeLabel.setBounds(350, 170, 60, 20);

    addButton.setText("Add");
    addButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            addButtonActionPerformed(evt);
        }
    });
    add(addButton);
    addButton.setBounds(410, 170, 70, 23);

    saveButton.setText("Save to File");
    saveButton.setMaximumSize(menuButton.getMaximumSize());
    saveButton.setMinimumSize(menuButton.getMinimumSize());
    saveButton.setPreferredSize(menuButton.getPreferredSize());
    saveButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            saveButtonActionPerformed(evt);
        }
    });
    add(saveButton);
    saveButton.setBounds(250, 390, 140, 23);
}// </editor-fold>                        

private void menuButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
    parent.switchPanel(MainWindow.PanelName.MENU);
}                                          

private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {                                          
    if(!nameField.getText().isEmpty()){
        dtm.addRow(new Object[]{nameField.getText(), scoreLabel.getText(), timeLabel.getText()});
        nameField.setText("");
        scoreLabel.setText("");
        timeLabel.setText("");
        addButton.setEnabled(false);
        scoreTable.revalidate();
    }
}                                         

private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
    if(dtm.getRowCount() != 0){
        File file = new File("./src/Resources/score.txt");
            try {
                if(!file.exists())
                    file.createNewFile();
                FileWriter fw = new FileWriter(file);
                int i;
                for(i = 0; i < dtm.getRowCount(); i++)
                    fw.write(dtm.getValueAt(i, 0) + ";" +
                                        dtm.getValueAt(i, 1)+ ";" +
                                        dtm.getValueAt(i, 2));
                fw.close();
                JOptionPane.showMessageDialog(this,"Done saving file.","Completed",JOptionPane.INFORMATION_MESSAGE);
            } catch (IOException ex1) {
                JOptionPane.showMessageDialog(null,"Error while saving file.","Error",JOptionPane.ERROR_MESSAGE);
            }
    }
}                                          

// Variables declaration - do not modify                     
private javax.swing.JButton addButton;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JButton menuButton;
private javax.swing.JTextField nameField;
private javax.swing.JButton saveButton;
private javax.swing.JLabel scoreLabel;
private javax.swing.JTable scoreTable;
private javax.swing.JLabel timeLabel;
private javax.swing.JLabel titleLabel;
// End of variables declaration                   
}

我的问题是,面板的唯一表格是不可见的,即使在调用setVisible(true)之后也不会显示。表的数据模型确实具有表的内容,并且所述内容可以正常读取和写入 - 它们只是不可见。

从我最近的类似问题中得出,是否可能是由面板布局或外观造成的?

1 个答案:

答案 0 :(得分:-2)

我按照你们的要求做了:拿起面板,将其复制到一个空白项目中,除了构造函数和自动生成的部分之外,删除了所有内容。这是剩下的:

public class ScorePanel extends javax.swing.JPanel {

private DefaultTableModel dtm;

private JScrollPane jScrollPane1;
private JTable scoreTable;           

public ScorePanel() {
    initComponents();
    dtm = (DefaultTableModel) scoreTable.getModel();
    for(int i = dtm.getRowCount() - 1; i >= 0; i--)
        dtm.removeRow(i);
    dtm.addRow (new Object[]{"a", "b", "c"});
    scoreTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    //scoreTable.getColumnModel().getColumn(0).setHeaderValue("Test");
    this.add(scoreTable);
}

private void initComponents() {

    jScrollPane1 = new javax.swing.JScrollPane();
    scoreTable = new javax.swing.JTable();

    setMaximumSize(new java.awt.Dimension(640, 480));
    setMinimumSize(new java.awt.Dimension(640, 480));
    setPreferredSize(new java.awt.Dimension(640, 480));

    scoreTable.setAutoCreateRowSorter(true);
    scoreTable.setModel(new javax.swing.table.DefaultTableModel(
        new Object [][] {
            {null, null, null},
            {null, null, null},
            {null, null, null}
        },
        new String [] {
            "Name", "Score", "Time"
        }
    ) {
        Class[] types = new Class [] {
            java.lang.String.class, java.lang.Integer.class, java.lang.String.class
        };
        boolean[] canEdit = new boolean [] {
            false, false, false
        };

        public Class getColumnClass(int columnIndex) {
            return types [columnIndex];
        }

        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return canEdit [columnIndex];
        }
    });
    scoreTable.setRowSelectionAllowed(false);
    scoreTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
    jScrollPane1.setViewportView(scoreTable);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(170, 170, 170)
            .addComponent(titleLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addGroup(layout.createSequentialGroup()
            .addGap(160, 160, 160)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(130, 130, 130)
                    .addComponent(scoreLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 0, 0)
                    .addComponent(timeLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 0, 0)
                    .addComponent(addButton, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addComponent(nameField, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 320, javax.swing.GroupLayout.PREFERRED_SIZE)))
        .addGroup(layout.createSequentialGroup()
            .addGap(250, 250, 250)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(menuButton, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(saveButton, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(56, 56, 56)
            .addComponent(titleLabel)
            .addGap(67, 67, 67)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(scoreLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(timeLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(addButton)
                .addComponent(nameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createSequentialGroup()
                    .addGap(21, 21, 21)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE)))
            .addGap(9, 9, 9)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(20, 20, 20)
                    .addComponent(menuButton))
                .addComponent(saveButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
    );
}
}

现在面板上没有任何其他东西,但是表格和它所在的滚动窗格。设置autoCreateColumnsFromModel(true)使得表格在NetBeans中可见,但它在运行时仍然不可见,即使在调用revalidate()之后也是如此并通过布局循环。