1

I am making a random app and I want it to make tables for spreadsheets. I made some code so far but my JTable doesn't seem to show up. What have I done wrong? Here is my code:

import java.awt.EventQueue;
import java.awt.*;
import javax.swing.*;

public class Screen {

private static JFrame frame;
public JFileChooser choicer = new JFileChooser();
public Font f = new Font("Candara", Font.PLAIN, 16);
public TableMethods tm = new TableMethods();
String dir;
JTable Table;
String[] columns;
String[][] data;
JScrollPane jsp;
/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Screen window = new Screen();
                window.frame.setVisible(true);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public Screen() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setTitle("Spreadr");
    frame.setBounds(100, 100, 1000, 800);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


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

    JMenu FileMenu = new JMenu("File");
    FileMenu.setFont(f);
    menuBar.add(FileMenu);

    JMenuItem New_Doc = new JMenuItem("New Document");
    New_Doc.setFont(f);
    New_Doc.addActionListener(e -> {
        choicer.setCurrentDirectory(new java.io.File("."));
        choicer.setDialogTitle("New Document");
        choicer.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        choicer.setAcceptAllFileFilterUsed(false);

        if (choicer.showOpenDialog(new JPanel()) == JFileChooser.APPROVE_OPTION) {
            dir = String.valueOf(choicer.getCurrentDirectory());
        }
    });
    FileMenu.add(New_Doc);

    JMenuItem Save_Doc = new JMenuItem("Save Document");
    Save_Doc.setFont(f);
    FileMenu.add(Save_Doc);

    JMenuItem Open_Doc = new JMenuItem("Open Document");
    Open_Doc.setFont(f);
    FileMenu.add(Open_Doc);

    JMenu EditMenu = new JMenu("Edit");
    EditMenu.setFont(f);
    menuBar.add(EditMenu);

    JMenuItem Copy = new JMenuItem("Copy");
    Copy.setFont(f);
    EditMenu.add(Copy);

    JMenuItem Cut = new JMenuItem("Cut");
    Cut.setFont(f);
    EditMenu.add(Cut);

    JMenuItem Paste = new JMenuItem("Paste");
    Paste.setFont(f);
    EditMenu.add(Paste);
    EditMenu.addSeparator();

    JMenuItem EditRows = new JMenuItem("Edit Rows...");
    EditRows.setFont(f);
    JMenuItem EditColumns = new JMenuItem("Edit Columns...");
    EditColumns.setFont(f);

    EditMenu.add(EditColumns);
    EditMenu.add(EditRows);
    frame.getContentPane().setLayout(new BorderLayout(0, 0));

    Table = new JTable();
    Table = tm.newTable(Table);
    jsp = new JScrollPane(Table);
    frame.add(jsp);
}

class TableMethods {
    public JTable newTable(JTable table) {
        int i = 1;
        columns = new String[10];
        while (i <= 10) {
            columns[i - 1] = String.valueOf(i);
        }

        i = 0;
        int j = 0;
        while (i < 10) {
            while (j < 2) {
                data[i][j] = String.valueOf(i + j);
                j++;
            }
            i++;
        }
        table = new JTable(data, columns);
        table.setFillsViewportHeight(false);
        table.setDragEnabled(false);
        frame.getContentPane().add(table);
        return table;
    }
}
}

I feel there is something wrong with my layout. I did some changes as requested by @ClarkKent but now the Application is completely white.

The White App

1 Answers1

1

Found a couple of issues. The screen wouldn't show up because you never incremented i, and never initialized data in your newTable method. This was giving an endless loop which would cause your screen not to become visible yet. After the endless loop was fixed, you would get a null pointer exception.

import java.awt.EventQueue;
import java.awt.*;
import javax.swing.*;

public class Screen {

    private JFrame frame;
    public JFileChooser choicer = new JFileChooser();
    public Font f = new Font("Candara", Font.PLAIN, 16);
    public TableMethods tm = new TableMethods();
    String dir;
    JTable Table;
    String[] columns;
    String[][] data;
    JScrollPane jsp;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Screen window = new Screen();
                    //window.frame.setVisible(true);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public Screen() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setTitle("Spreadr");
        frame.setBounds(100, 100, 1000, 800);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

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

        JMenu FileMenu = new JMenu("File");
        FileMenu.setFont(f);
        menuBar.add(FileMenu);

        JMenuItem New_Doc = new JMenuItem("New Document");
        New_Doc.setFont(f);
        New_Doc.addActionListener(e -> {
            choicer.setCurrentDirectory(new java.io.File("."));
            choicer.setDialogTitle("New Document");
            choicer.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            choicer.setAcceptAllFileFilterUsed(false);

            if (choicer.showOpenDialog(new JPanel()) == JFileChooser.APPROVE_OPTION) {
                dir = String.valueOf(choicer.getCurrentDirectory());
            }
        });
        FileMenu.add(New_Doc);

        JMenuItem Save_Doc = new JMenuItem("Save Document");
        Save_Doc.setFont(f);
        FileMenu.add(Save_Doc);

        JMenuItem Open_Doc = new JMenuItem("Open Document");
        Open_Doc.setFont(f);
        FileMenu.add(Open_Doc);

        JMenu EditMenu = new JMenu("Edit");
        EditMenu.setFont(f);
        menuBar.add(EditMenu);

        JMenuItem Copy = new JMenuItem("Copy");
        Copy.setFont(f);
        EditMenu.add(Copy);

        JMenuItem Cut = new JMenuItem("Cut");
        Cut.setFont(f);
        EditMenu.add(Cut);

        JMenuItem Paste = new JMenuItem("Paste");
        Paste.setFont(f);
        EditMenu.add(Paste);
        EditMenu.addSeparator();

        JMenuItem EditRows = new JMenuItem("Edit Rows...");
        EditRows.setFont(f);
        JMenuItem EditColumns = new JMenuItem("Edit Columns...");
        EditColumns.setFont(f);

        EditMenu.add(EditColumns);
        EditMenu.add(EditRows);
        frame.getContentPane().setLayout(new BorderLayout(0, 0));

        Table = new JTable();
        Table = tm.newTable(Table);
        jsp = new JScrollPane(Table);
        frame.add(jsp);
        frame.pack();
        frame.setVisible(true);
    }

    class TableMethods {
        public JTable newTable(JTable table) {
            int i = 1;
            columns = new String[10];
            while (i <= 10) {
                columns[i++ - 1] = String.valueOf(i); //Increment i
            }
            data = new String[10][10]; //Initialize data.
            i = 0;
            int j = 0;
            while (i < 10) {
                j=0;  //Set j to 0.
                while (j < 2) {
                    data[i][j] = String.valueOf(i + j);
                    j++;
                }
                i++;
            }
            table = new JTable(data, columns);
            table.setFillsViewportHeight(false);
            table.setDragEnabled(false);
            frame.getContentPane().add(table);
            return table;
        }
    }
}

enter image description here

Clark Kent
  • 1,178
  • 1
  • 12
  • 26
  • But when I declared j as 0, why did this happen in the first place? – Bomba Bazooka May 31 '16 at 17:31
  • And also, if I want to prevent people from dragging the columns everywhere, how would I do that? – Bomba Bazooka May 31 '16 at 17:32
  • Do you mean, why did you still get an endless loop when you already declared `j = 0` ? – Clark Kent May 31 '16 at 17:33
  • Yes. As the value is set to 0, shouldn't it work fine? – Bomba Bazooka May 31 '16 at 17:34
  • To answer your second question, you can prevent reordering of columns using the following line: `table.getTableHeader().setReorderingAllowed(false);` courtesy of http://stackoverflow.com/questions/17641123/jtable-disable-user-column-dragging – Clark Kent May 31 '16 at 17:35
  • To answer your first question regarding the endless loop, I was mistaken. It wasn't an endless loop, it just made your inner loop execute only once. – Clark Kent May 31 '16 at 17:37
  • Sorry for the million requests but can I also create and label rows? – Bomba Bazooka May 31 '16 at 17:37
  • 1
    @ThatFailureGuy It's possible. There are plenty of resources on how to do that: http://stackoverflow.com/questions/8187639/jtable-with-titled-rows-and-columns/8187799#8187799 http://www.chka.de/swing/table/row-headers/JTable.html https://tips4java.wordpress.com/2008/11/18/row-number-table/ – Clark Kent May 31 '16 at 17:40
  • Actually forget that I found the answer http://http://stackoverflow.com/questions/8187639/jtable-with-titled-rows-and-columns – Bomba Bazooka May 31 '16 at 17:40