Results 1 to 10 of 10
  1. #1
    VirusParadox is offline Member
    Join Date
    Dec 2013
    Posts
    7
    Rep Power
    0

    Question Why won't my JTable show it's columns?

    Hi, I'm new to swing, and I've been trying to make a JTable, but the column names don't show up! Here's my code: (It doesn't show a lot of unnecessary stuff)

    Java Code:
    	static Object[] titles = {"No",
    		"Item",
    		"Priority",
    		"Date",
    		"Time",
    		"Done?"};
    	static Object[][] data = {
    		{new Integer(1),"","","","",""},
    		{new Integer(2),"","","","",""},
    		{new Integer(3),"","","","",""},
    		{new Integer(4),"","","","",""},
    		{new Integer(5),"","","","",""},
    		{new Integer(6),"","","","",""},
    		{new Integer(7),"","","","",""},
    		{new Integer(8),"","","","",""},
    		{new Integer(9),"","","","",""},
    		{new Integer(10),"","","","",""},
    		{new Integer(11),"","","","",""},
    		{new Integer(12),"","","","",""},
    		{new Integer(13),"","","","",""},
    		{new Integer(14),"","","","",""},
    		{new Integer(15),"","","","",""}};
    	static JTable todo;
    	static JScrollPane todoScroll = new JScrollPane(todo);
    Java Code:
    		todo = new JTable(data, titles);
    		todoScroll.setPreferredSize(new Dimension(700,500));
    Java Code:
    		
    		c2.gridx = 0;
    		c2.gridy = 0;
    		c2.gridwidth = 6;
    		panel2.add(clearTable, c2);
    		c2.gridy = 1;
    		c2.gridwidth = 1;
    		panel2.add(todoScroll, c2);

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Why won't my JTable show it's columns?

    Please make a small, complete program that compiles, executes and shows the problem. It's good to leave out the unnecessary stuff, but it needs to compile and execute.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    VirusParadox is offline Member
    Join Date
    Dec 2013
    Posts
    7
    Rep Power
    0

    Default Re: Why won't my JTable show it's columns?

    Ok, here's the new version:
    Java Code:
    import javax.swing.*;
    
    static Object[] titles = {"No",
    		"Item",
    		"Priority",
    		"Date",
    		"Time",
    		"Done?"};
    	static Object[][] data = {
    		{new Integer(1),"","","","",""},
    		{new Integer(2),"","","","",""},
    		{new Integer(3),"","","","",""},
    		{new Integer(4),"","","","",""},
    		{new Integer(5),"","","","",""},
    		{new Integer(6),"","","","",""},
    		{new Integer(7),"","","","",""},
    		{new Integer(8),"","","","",""},
    		{new Integer(9),"","","","",""},
    		{new Integer(10),"","","","",""},
    		{new Integer(11),"","","","",""},
    		{new Integer(12),"","","","",""},
    		{new Integer(13),"","","","",""},
    		{new Integer(14),"","","","",""},
    		{new Integer(15),"","","","",""}};
    	static JTable todo;
    	static JScrollPane todoScroll = new JScrollPane(todo);
    	static JButton clearTable = new JButton("Clear");
    public class IndexWindow {
    	public static void GUI() {
    		GridBagLayout gbl = new GridBagLayout();
    		GridBagConstraints c1,c2,c3;
    		c1 = new GridBagConstraints();
    		c2 = new GridBagConstraints();
    		c3 = new GridBagConstraints();
    		windowContent.setLayout(cl);
    		index.setLayout(new BoxLayout(index, BoxLayout.PAGE_AXIS));
    		panel1.setLayout(gbl);
    		panel2.setLayout(gbl);
    		panel3.setLayout(gbl);
    		
    		panel1.setBackground(Color.RED);
    		panel2.setBackground(Color.BLUE);
    		panel3.setBackground(Color.GREEN);
    		index.setBackground(Color.ORANGE);
    		
    		windowContent.add(index, "Index");
    		windowContent.add(panel1, "Panel 1");
    		windowContent.add(panel2, "Panel 2");
    		windowContent.add(panel3, "Panel 3");
    		
    		frame.add(windowContent);
                    todo = new JTable(data, titles);
    		todoScroll.setPreferredSize(new Dimension(700,500));
    		c2.gridx = 0;
    		c2.gridy = 0;
    		c2.gridwidth = 6;
    		panel2.add(clearTable, c2);
    		c2.gridy = 1;
    		c2.gridwidth = 1;
    		panel2.add(todoScroll, c2);
    	public static void main(String[] args) {
    		new IndexWindow();
    		GUI();
    	}
    
    }
    Last edited by VirusParadox; 12-21-2013 at 10:11 PM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Why won't my JTable show it's columns?

    Can you make a small, complete program that compiles, executes and shows the problem? The code in post#3 is incomplete.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    VirusParadox is offline Member
    Join Date
    Dec 2013
    Posts
    7
    Rep Power
    0

    Default Re: Why won't my JTable show it's columns?

    Sorry, I forgot the class name. I edited the post, does it work now?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Why won't my JTable show it's columns?

    I edited the post, does it work now
    Sorry, I don't see the changes that will make that code compile and execute. It still needs to be fixed.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    VirusParadox is offline Member
    Join Date
    Dec 2013
    Posts
    7
    Rep Power
    0

    Default Re: Why won't my JTable show it's columns?

    I give up , here's the whole program:
    Java Code:
    package WindowAndEngines;
    
    import javax.swing.*;
    import javax.swing.table.*;
    
    
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.KeyEvent;
    
    public class IndexWindow {
    	
    	static JFrame frame = new JFrame("Utilities");
    	static JPanel windowContent = new JPanel();
    	static JPanel index = new JPanel();
    	static JPanel panel1 = new JPanel();
    	static JPanel panel2 = new JPanel();
    	static JPanel panel3 = new JPanel();
    	static JButton panel1Button = new JButton("Panel 1");
    	static JButton panel2Button = new JButton("Panel 2");
    	static JButton panel3Button = new JButton("Panel 3");
    	static CardLayout cl = new CardLayout();
    	static JTextField calcAnswer = new JTextField();
    	static JButton button0 = new JButton("0");
    	static JButton button1 = new JButton("1");
    	static JButton button2 = new JButton("2");
    	static JButton button3 = new JButton("3");
    	static JButton button4 = new JButton("4");
    	static JButton button5 = new JButton("5");
    	static JButton button6 = new JButton("6");
    	static JButton button7 = new JButton("7");
    	static JButton button8 = new JButton("8");
    	static JButton button9 = new JButton("9");
    	static JButton decimal = new JButton(".");
    	static JButton equals = new JButton("=");
    	static JButton addition = new JButton("+");
    	static JButton subtraction = new JButton("-");
    	static JButton multiplication = new JButton("*");
    	static JButton division = new JButton("/");
    	static JButton clear = new JButton("CLEAR");
            //Titles for the table
    	static Object[] titles = {"No",
    		"Item",
    		"Priority",
    		"Date",
    		"Time",
    		"Done?"};
            //The 2D array for the table
    	static Object[][] data = {
    		{new Integer(1),"","","","",""},
    		{new Integer(2),"","","","",""},
    		{new Integer(3),"","","","",""},
    		{new Integer(4),"","","","",""},
    		{new Integer(5),"","","","",""},
    		{new Integer(6),"","","","",""},
    		{new Integer(7),"","","","",""},
    		{new Integer(8),"","","","",""},
    		{new Integer(9),"","","","",""},
    		{new Integer(10),"","","","",""},
    		{new Integer(11),"","","","",""},
    		{new Integer(12),"","","","",""},
    		{new Integer(13),"","","","",""},
    		{new Integer(14),"","","","",""},
    		{new Integer(15),"","","","",""}};
            //The table variables
    	static JTable todo;
    	static JScrollPane todoScroll = new JScrollPane(todo);
    	static JButton clearTable = new JButton("Clear");
            //Other stuff
    	static Font buttonFont = new Font("Comic Sans MS", Font.BOLD, 20);
    	static JCheckBox checkbox = new JCheckBox();
    	
    	public static void GUI() {
    		GridBagLayout gbl = new GridBagLayout();
    		GridBagConstraints c1,c2,c3;
    		c1 = new GridBagConstraints();
    		c2 = new GridBagConstraints();
    		c3 = new GridBagConstraints();
    		windowContent.setLayout(cl);
    		index.setLayout(new BoxLayout(index, BoxLayout.PAGE_AXIS));
    		panel1.setLayout(gbl);
    		panel2.setLayout(gbl);
    		panel3.setLayout(gbl);
    		
    		panel1.setBackground(Color.RED);
    		panel2.setBackground(Color.BLUE);
    		panel3.setBackground(Color.GREEN);
    		index.setBackground(Color.ORANGE);
    		
    		windowContent.add(index, "Index");
    		windowContent.add(panel1, "Panel 1");
    		windowContent.add(panel2, "Panel 2");
    		windowContent.add(panel3, "Panel 3");
    		
    		frame.add(windowContent);
    		
    		panel1.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0), "forward");
            panel1.getActionMap().put("forward", new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                	cl.show(windowContent, "Index");
                }
            });
    		
            panel2.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0), "forward");
            panel2.getActionMap().put("forward", new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                	cl.show(windowContent, "Index");
                }
            });
            
            panel3.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0), "forward");
            panel3.getActionMap().put("forward", new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    cl.show(windowContent, "Index");
                }
            });
            
    		index.add(panel1Button);
    		index.add(panel2Button);
    		index.add(panel3Button);
    		index.add(checkbox);
    		checkbox.setSelected(true);
    		
    		IndexEngine IEngine = new IndexEngine();
    		
    		panel1Button.addActionListener(IEngine);
    		panel2Button.addActionListener(IEngine);
    		panel3Button.addActionListener(IEngine);
    		
    		calcAnswer.setEditable(false);
    		addition.setFont(buttonFont);
    		addition.setBackground(Color.BLUE);
    		subtraction.setFont(buttonFont);
    		subtraction.setBackground(Color.BLUE);
    		multiplication.setBackground(Color.BLUE);
    		multiplication.setFont(buttonFont);
    		division.setBackground(Color.BLUE);
    		division.setFont(buttonFont);
    		clear.setBackground(Color.CYAN);
    		clear.setFont(buttonFont);
    		equals.setBackground(Color.GREEN);
                    //Table creation
    		todo = new JTable(data, titles);
    		todoScroll.setPreferredSize(new Dimension(700,500));
                    //Other stuff
    		frame.setSize(500, 500);
    		frame.setDefaultCloseOperation(1);
    		frame.setVisible(true);
    		
    		cl.show(windowContent, "Index");
    
    		c1.ipadx = 4;
    		c1.ipady = 4;
    		c1.fill = GridBagConstraints.BOTH;
    		c1.gridx = 0;
    		c1.gridy = 0;
    		c1.gridwidth = 5;
    		c1.gridheight = 1;
    		panel1.add(calcAnswer, c1);
    		c1.gridy = 1;
    		c1.gridwidth = 1;
    		panel1.add(button9, c1);
    		c1.gridx = 1;
    		panel1.add(button8, c1);
    		c1.gridx = 2;
    		panel1.add(button7, c1);
    		c1.gridx = 3;
    		c1.gridwidth = 2;
    		panel1.add(addition, c1);
    		c1.gridy = 2;
    		c1.gridx = 0;
    		c1.gridwidth = 1;
    		panel1.add(button6, c1);
    		c1.gridx = 1;
    		panel1.add(button5, c1);
    		c1.gridx = 2;
    		panel1.add(button4, c1);
    		c1.gridx = 3;
    		c1.gridwidth = 2;
    		panel1.add(subtraction, c1);
    		c1.gridy = 3;
    		c1.gridx = 0;
    		c1.gridwidth = 1;
    		panel1.add(button3, c1);
    		c1.gridx = 1;
    		panel1.add(button2, c1);
    		c1.gridx = 2;
    		panel1.add(button1, c1);
    		c1.gridx = 3;
    		c1.gridwidth = 2;
    		panel1.add(multiplication, c1);
    		c1.gridy = 4;
    		c1.gridx = 0;
    		c1.gridwidth = 1;
    		panel1.add(button0, c1);
    		c1.gridx = 1;
    		panel1.add(decimal, c1);
    		c1.gridx = 2;
    		c1.gridwidth = 1;
    		panel1.add(equals, c1);
    		c1.gridwidth = 2;
    		c1.gridx = 3;
    		panel1.add(division, c1);
    		c1.gridy = 5;
    		c1.gridx = 0;
    		c1.gridwidth = 5;
    		panel1.add(clear, c1);
    		
    		CalculatorEngine calcEngine = new CalculatorEngine();
    		button0.addActionListener(calcEngine);
    		button1.addActionListener(calcEngine);
    		button2.addActionListener(calcEngine);
    		button3.addActionListener(calcEngine);
    		button4.addActionListener(calcEngine);
    		button5.addActionListener(calcEngine);
    		button6.addActionListener(calcEngine);
    		button7.addActionListener(calcEngine);
    		button8.addActionListener(calcEngine);
    		button9.addActionListener(calcEngine);
    		addition.addActionListener(calcEngine);
    		subtraction.addActionListener(calcEngine);
    		multiplication.addActionListener(calcEngine);
    		division.addActionListener(calcEngine);
    		equals.addActionListener(calcEngine);
    		clear.addActionListener(calcEngine);
    		
    		//The code that adds the table and the clear button to the 2nd panel
    		c2.gridx = 0;
    		c2.gridy = 0;
    		c2.gridwidth = 6;
    		panel2.add(clearTable, c2);
    		c2.gridy = 1;
    		c2.gridwidth = 1;
    		panel2.add(todoScroll, c2);
                    //The rest of the code
    		
    		TodoEngine TDEngine = new TodoEngine();
    		
    		clearTable.addActionListener(TDEngine);
    		
    	}
    	public static void main(String[] args) {
    		new IndexWindow();
    		GUI();
    	}
    
    }
    By the way, this won't show the table without the engine classes too. Unless you or me edit this code a little.
    Last edited by VirusParadox; 12-21-2013 at 10:42 PM. Reason: Extra info

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Why won't my JTable show it's columns?

    Does the posted code now compile, execute and show the problem? Not much use for testing if it doen't.

    Try again to make the small simple program for testing. This last post is almost too big now.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default Re: Why won't my JTable show it's columns?

    You're adding null to the scroll pane, i.e. you haven't created the table yet.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Why won't my JTable show it's columns?

    Quote Originally Posted by JosAH View Post
    You're adding null to the scroll pane, i.e. you haven't created the table yet.

    kind regards,

    Jos
    To expand on what you said:
    Java Code:
    static JTable todo; //You need to initialize this variable
        static JScrollPane todoScroll = new JScrollPane(todo);

Similar Threads

  1. Fill jtable columns
    By victoryo in forum AWT / Swing
    Replies: 2
    Last Post: 01-11-2012, 05:28 PM
  2. Redisplay New JTable and Columns
    By jcman01 in forum AWT / Swing
    Replies: 2
    Last Post: 08-19-2011, 06:40 PM
  3. Sorting and hiding columns in JTable
    By ProgrammingPup in forum Advanced Java
    Replies: 1
    Last Post: 11-17-2009, 09:59 PM
  4. Right Align columns in JTable
    By Laura Warren in forum New To Java
    Replies: 2
    Last Post: 12-18-2008, 09:01 PM
  5. sort columns in jtable
    By Alan in forum AWT / Swing
    Replies: 2
    Last Post: 05-14-2007, 05:18 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •