Domanda

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!

È stato utile?

Soluzione

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top