0

Hello I am a very new to JAVA and powering out a GUI while I learn. I have created a JFrame with a ScrollPane and a JTable. When I increase the columns above 2 the data below row one does not show up.

Also when my JFram comes up the screen is blank until I Re-Size the window.

import javax.swing.*;
import javax.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.Border;
import javax.swing.JTable;
import javax.swing.JScrollPane;


public class Swing {

public static void main (String[] args){
//ADD FRAME 
    Color LBlue = new Color(83,141,213);//RGB Light BLUE

    JFrame frame = new JFrame("COLA");
    frame.setSize(950,350);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //CLOSE
    frame.getContentPane().setBackground(LBlue); //COLOR
    frame.setLocationRelativeTo(null); //Center JFrame in the middle of screen

//MENU Work
    JMenuBar menubar = new JMenuBar();
    frame.setJMenuBar(menubar);
    JMenu file = new JMenu ("File");
    menubar.add(file);
    JMenuItem exit = new JMenuItem("Exit");
    file.add(exit);

    class exitaction implements ActionListener{ //Action to call menu bar to close
        public void actionPerformed (ActionEvent o){
            System.exit(0);
        }
    }
    exit.addActionListener(new exitaction());
//GRID WORK

    JPanel panel = new JPanel(new GridBagLayout());
    frame.getContentPane().add(panel, BorderLayout.NORTH);
    GridBagConstraints c = new GridBagConstraints();
    panel.setBackground(LBlue); //Set Background of Panel
    panel.setVisible(true);


    String data[][] = {{"HEADER 1", "ABC"}, {"HEADER 1", "HEADER 1"}, {"HEADER     1", "GHI" },     {"HEADER 1", "GHI" }};
    String col[] = {"", "NAMES", "NAMES", };
    JTable table = new JTable(data, col);
    table.setBackground(Color.GRAY);
    table.setFont(new Font("Arial", Font.BOLD, 12));
    table.setVisible(true);

    JScrollPane scroll =new JScrollPane(table);
    c.gridx = 1; 
    c.gridy = 1;    
    scroll.setPreferredSize(new Dimension( 900,115));
    panel.add(scroll);

    table.revalidate();
    table.validate();
    table.repaint();
    table.setVisible(true);

    scroll.invalidate();
    scroll.validate();
    scroll.repaint();
    table.setVisible(true);

    frame.setVisible(true);

}

thank you for any help!

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Dinnin
  • 3
  • 2
  • unrelated: [don't use setXXSize, ever](http://stackoverflow.com/a/7229519/203657) http://stackoverflow.com/a/7229519/203657 – kleopatra Oct 22 '13 at 08:29

1 Answers1

1

Your code produces a stacktrace

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 2
    at javax.swing.JTable$1.getValueAt(JTable.java:689)
    at javax.swing.JTable.getValueAt(JTable.java:2720)

which is a good indicator of the problem. It means that the number of data columns needs to match the number of column identifiers. Add the missing data:

String data[][] = { 
    { "NEWDATA", "HEADER 1", "ABC" }, { "NEWDATA", "HEADER 1", "HEADER 1" },
    { "NEWDATA", "HEADER     1", "GHI" }, { "NEWDATA", "HEADER 1", "GHI" }, 
};

Read: How to Use Tables

Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • Thank you, it looks like that did it. Is there a way to just set the Titles while leaving the other columns blank, until I need them populated? or do I have to pre populate them with "" at minimum? – Dinnin Oct 21 '13 at 17:52
  • No need to pre-populate. It sounds like you will be dynamically adding items to the table. Have a look at using a `TableModel` from the link added – Reimeus Oct 21 '13 at 18:00