-2

I have the following panel in NetBeans (only around the first third is my work, the rest is auto-generated and unmodifiable):

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                   
}

My problem is that the panel's sole table is invisible and doesn't show up even after setVisible(true) is called on it. The table's data model does have the contents of the table and said contents can be read and written normally - they're just not visible.

Drawing from my recent similar problems, can it be caused by the panel layout or look-and-feel?

Alexis C.
  • 91,686
  • 21
  • 171
  • 177
amitakartok
  • 115
  • 1
  • 11
  • 1
    For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve). Note that when you add a component to another visible component, the default is 'visible'.. – Andrew Thompson Jan 18 '14 at 23:09
  • 1
    `menuButton.setBounds(250, 410, 137, 23);` Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Jan 18 '14 at 23:10
  • This code is already self-contained and I can't make it any shorter without leaving out critical details. Also, the indicated part of the code was auto-generated by NetBeans and I'm not allowed to modify it. – amitakartok Jan 18 '14 at 23:13
  • 1
    You don't need to call `setVisible(...)` on components. You've got a bug in your program, but I don't see how your posted code helps us. I agree that you should create and post an MCVE. You also should avoid using Swing code generators until you understand the basics of Swing. And no, your code is not "already self-contained". Please read Andrew's link before posting nonsense like this. – Hovercraft Full Of Eels Jan 18 '14 at 23:13
  • *"I'm not allowed to modify it."* "The tail wagging the dog." Either learn how to use the powerful tools at your disposal, or don't use them. It is perfectly possible to use layouts with Netbeans. – Andrew Thompson Jan 18 '14 at 23:15

1 Answers1

-2

I did as you guys asked: took the panel, copied it to a blank project, deleted everything but the constructor and the auto-generated part. Here's what's left:

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

There is literally nothing else on the panel now but the table and the scrollpane it's in. Setting autoCreateColumnsFromModel(true) made the table visible in NetBeans but it's still not visible at runtime, not even after calling revalidate() and cycling through layouts.

amitakartok
  • 115
  • 1
  • 11
  • See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) – Andrew Thompson Jan 18 '14 at 23:59
  • ...which doesn't really help with my problem. On the other hand, I don't really intend on using them anyway; they're auto-generated. – amitakartok Jan 19 '14 at 00:02
  • Which is why I did not enter it as an answer. But *that* problem I was referring to will probably crop up on the next computer that fragile code runs on. BTW - The down-votes and comment were a warning for *others* that this is not the way to go. We already have enough fragile code on the net to confuse people, and I'd already given up hope that you would listen to reason. – Andrew Thompson Jan 19 '14 at 00:06
  • I see. One more reason to learn enough Java to scratch-write all my code, I guess... though it's kinda strange: why is NetBeans hardwired to use unsafe/buggy/suboptimal code in the first place? Not that I'm nitpicking or anything; I'm just curious. BTW, this doesn't seem to be a layout issue; switching layouts doesn't solve the problem and besides, using anything but a null layout on the frame this panel is originally displayed on produces a blank panel, no matter what layout the frame itself uses. – amitakartok Jan 19 '14 at 00:10
  • Netbeans is **not** hard wired to do that. The code you see in [this answer](http://stackoverflow.com/a/21142687/418556) was written using Netbeans, though not using the GUI designer. And even then, the GUI designer can use layouts. It is just a matter of knowing how to use it (or at least the right choices to make when prompted) in order to use layouts in the Drag-n-Drop designer. The only reason I *don't* use the GUI designer in NB is simply because it tends to force you to `extend` components, and that is generally sub-optimal (OO) design. – Andrew Thompson Jan 19 '14 at 00:15
  • 1
    Note to self, don't use GUI designer if I can help it. – amitakartok Jan 19 '14 at 00:18
  • *"BTW, this doesn't seem to be a layout issue;"* If you want to find out how to lay the GUI out, take it up on a separate question, but provide ASCII art (or an image with a simple drawing) of the GUI as it should appear in smallest size and (if resizable) with extra width/height. – Andrew Thompson Jan 19 '14 at 00:18
  • @amitakartok I too am a Java newbie and I've also been using Netbeans to produce my GUI. But from what I saw on my search on internet is that Netbeans GUI designer is considered the best tool available, and it is being used by pros. So, you don't have to learn to hard code everything yourself. I've been using GUI designer and I am really happy with how easy and fast it is. – cbt Jan 19 '14 at 01:00
  • The problem seems to be with the scrollpane; dragging the table out of the pane and onto the panel displays it normally but creating a new pane and dragging the table into it hides the table again. So, I've got the table to show up but of course now there's no table header; the instructions here: http://stackoverflow.com/questions/6047090/jtable-column-header-not-visible didn't work. Not that it's much of a problem but as a stopgap measure, it'll do. – amitakartok Jan 19 '14 at 01:28