Results 1 to 18 of 18
Like Tree3Likes
  • 1 Post By Fubarable
  • 1 Post By KevinWorkman
  • 1 Post By KevinWorkman

Thread: JTable not displaying

  1. #1
    fam2315 is offline Member
    Join Date
    Feb 2011
    Posts
    78
    Rep Power
    0

    Default JTable not displaying

    I am trying to add a simple JTable to my GUI

    I have an array that contains the column headers:

    Java Code:
    String[] columnNames = {"Col1","Col2","Col3"}
    The data array is a 1x3 (1 row, 3 col)

    Java Code:
    String[][] data = {{"orange","black","blue"}}
    The data array is not hardcoded, but everytime I add to it, I have a println statement and it shows as follows:

    Java Code:
    data[0][0] = orange
    data[0][1] = black
    data[0][2] = blue
    So at the point where I want to show the jTable, I have:
    Java Code:
    JTable tbl = new JTable(data,columnNames);
    JScrollPane jsp = new JScrollPane(tbl);
    myPanel is a JPanel

    Java Code:
    myPanel.setLayout(new BorderLayout());
    myPanel.add(jsp, BorderLayout.CENTER);
    The jscrollpane/table dont appear to be added to the JPanel. I've tried to manually toggle the visibility by doing a setvisibility false/true, but still no luck
    Last edited by fam2315; 08-02-2011 at 04:47 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,874
    Rep Power
    8

    Default

    If you want help, you'll have to provide an SSCCE that demonstrates the problem. Otherwise we're just guessing.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    fam2315 is offline Member
    Join Date
    Feb 2011
    Posts
    78
    Rep Power
    0

    Default

    which of the 's','sc','c','e' elements am i missing?

    I tried to break down the sections of my code to show what I was doing.
    Here it is as one code block

    Java Code:
    String[][] data = {{"orange","black","blue"}}
    String[] columnNames = {"Col1","Col2","Col3"}
    
    JTable tbl = new JTable(data,columnNames);
    JScrollPane jsp = new JScrollPane(tbl);
    
    myPanel.setLayout(new BorderLayout());
    myPanel.add(jsp, BorderLayout.CENTER);
    myPanel is a jPanel that I'm trying to display the JTable in, and it is not displaying at all.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by fam2315 View Post
    which of the 's','sc','c','e' elements am i missing?
    Pretty much all of them. Please read the link provided by the crazy cat lady above -- if you are in need our help quickly that is. Otherwise we can fumble along, and fumble is what we'll do since none of the code you are posting is helpful in allowing us to solve your problem.
    Last edited by Fubarable; 08-02-2011 at 05:28 PM.
    KevinWorkman likes this.

  5. #5
    fam2315 is offline Member
    Join Date
    Feb 2011
    Posts
    78
    Rep Power
    0

    Default

    Well that at least let's me know that it's not something glaringly obvious, so here is an example of what I'm trying to do. The project is a Netbean'ized' version of a swing project, so theres the app and the view. So my view consists of a JPanel(the main class panel if you will), and my JPanel(myPanel), inside of it, thats it. After I do initComponents() I run a method called setupTable(). Inside that method there is:

    Java Code:
    public void setupTable(){
    String[][] data = {{"orange","black","blue"}}
    String[] columnNames = {"Col1","Col2","Col3"}
    
    JTable tbl = new JTable(data,columnNames);
    JScrollPane jsp = new JScrollPane(tbl);
    
    myPanel.setLayout(new BorderLayout());
    myPanel.add(jsp, BorderLayout.CENTER);
    }
    This code is certainly being run and I've proved that to my self by putting in System.out.println statements, and I'm not seeing any changes to myPanel.

    As far as making it compilable, that would be a little tough, unless you would be willing to create a project in Netbeans(Swing Project), and drag and drop a panel called myPanel onto the screen, and add the code that I have layed out.
    Last edited by fam2315; 08-02-2011 at 05:55 PM.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by fam2315 View Post
    Well that at least let's me know that it's not something glaringly obvious, so here is a compilable example of what I'm trying to do. The project is a Netbean'ized' version of a swing project, so theres the app and the view. So my view consists of a JPanel(the main class panel if you will), and my JPanel(myPanel), inside of it, thats it. After I do initComponents() I run a method called setupTable(). Inside that method there is:

    Java Code:
    public void setupTable(){
    String[][] data = {{"orange","black","blue"}}
    String[] columnNames = {"Col1","Col2","Col3"}
    
    JTable tbl = new JTable(data,columnNames);
    JScrollPane jsp = new JScrollPane(tbl);
    
    myPanel.setLayout(new BorderLayout());
    myPanel.add(jsp, BorderLayout.CENTER);
    }
    This code is certainly being run and I've proved that to my self by putting in System.out.println statements, and I'm not seeing any changes to myPanel.
    You're continuing to post small segments of code that have nothing to do with your problem. Have you read the SSCCE link? Sorry to sound frustrated, but your behavior suggests to me that you've either not read the link, or don't understand it or for some reason have decided to not abide by its recommendations. If the latter please tell us why. If you've read it and don't understand it, then please ask. If you simply decide not to follow the recommendations contained, then I guess you really don't need our help because I don't see how we can help you.

  7. #7
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default

    so here is an example of what I'm trying to do.
    That is still not a SSCCE.

    As far as making it compilable, that would be a little tough, unless you would be willing to create a project in Netbeans(Swing Project), and drag and drop a panel called myPanel onto the screen, and add the code that I have layed out.
    We are not interested in the IDE you are using. In fact we recommend you don't use and IDE so you learn how to code Java instead of learning how to use an IDE.

    Post code that anybody can compile if you want help.

  8. #8
    fam2315 is offline Member
    Join Date
    Feb 2011
    Posts
    78
    Rep Power
    0

    Default

    I see where you are coming from but I'm not understanding the conflict here.

    The "Ensure everything is included, ready to go." portion seems to suggest that you need something with a main method that can be put into a txt file, renamed to .java and executed.

    If my problem is as simple as,

    "My for loop is not incrementing as I think it should" and the code is

    Java Code:
    for(int i=0; i<5){
    //Do Something
    }
    then obviously no compilable code is necessary. So where is the divide between "we need more code" and "don't just post your whole" program come?(because I have seen people yelled at for that)

    My project is in Swing, created by netbeans. If you have dealt with netbeans, then you know that when you create a fresh Swing desktop application, the ide generates ALOT of code, and most of it i'm sure is not relevant to this problem. I can reasonably assume that the code for the progress bar that gets generated automatically won't help out in this situation.

    So I did my best to strip out everything thats 'ide generated', and since I only wrote the lines of code that I outlined in my last post, then I assumed that was the problem. I honestly don't know what else to do to help clarify what's going on. I have the ide generated main panel, im dragging a panel from the palet onto that panel, and executing some code in the constructor.
    Last edited by fam2315; 08-02-2011 at 06:12 PM.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by fam2315 View Post
    I see where you are coming from but I'm not understanding the conflict here.

    The "Ensure everything is included, ready to go." portion seems to suggest that you need something with a main method that can be put into a txt file, renamed to .java and executed.
    Exactly.

    then obviously no compilable code is necessary.
    correct.


    So where is the divide between "we need more code" and "don't just post your whole" program come?
    It comes when we can't answer your question based on the small snippets of code that you're providing. The code you post has nothing to do with your problem (but don't know how to tell you this so you'll believe me). This means that the problem is elsewhere in your code. Where, no one knows. Which means that we'll need to see more code, but not so much code as to drown us in your code. You need to put the effort in to create a small compilable program that shows the problem.


    My project is in Swing, created by netbeans. If you have dealt with netbeans, then you know that when you create a fresh Swing desktop application, the ide generates ALOT of code, and most of it i'm sure is not relevant to this project. I can reasonably assume that the code for the progress bar that gets generated automatically won't help out in this situation.
    Which is a strong argument to not use NetBeans to generate your code when you are learning Swing.

    So I did my best to strip out everything thats 'ide generated', and since I only wrote the lines of code that I outlined in my last post, then I assumed that was the problem. I honestly don't know what else to do to help clarify what's going on.
    And we honestly can't help you if we can't see the problem. Surely this makes sense to you.

    For all we know your myPanel JPanel has not been added to a Container that is ultimately held by the JFrame, or is added in such a way that it is very small or not visualized at all, but again, who knows, and most of us don't like guessing because when we do that we're usually wrong.

    My suggestion: drop the NetBeans and learn to code by hand. Then once you understand Swing, go on back to using NetBeans code generation if you so desire.
    Last edited by Fubarable; 08-02-2011 at 06:17 PM.

  10. #10
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,874
    Rep Power
    8

    Default

    In addition to everything already said above, I'd just like to point out that if you had just created the SSCCE instead of arguing against it in the first place, your answer would probably be answered already- heck, most of the time, you'll find your own error during the process of boiling your problem down to the SSCCE.

    And yeah, ditch the GUI builder.
    Fubarable likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #11
    fam2315 is offline Member
    Join Date
    Feb 2011
    Posts
    78
    Rep Power
    0

    Default

    My suggestion: drop the NetBeans and learn to code by hand. Then once you understand Swing, go on back to using NetBeans code generation if you so desire.
    So since I have a question about swing, that implies that I don't understand swing?
    Since I'm using NetBeans, I don't know how to code by hand?

    Neither of these are true statements. I'll get it figured out, but making assertions like that, not good.

    I respect your opinion, but when you bring up things like "thats an argument to not use NetBeans", and 'learn swing", i feel like those are irrelevant and a little condescending(mainly the 2nd one, you can have your opinion on IDE/coding practices, but the bottom line is NetBeans works well with swing, and what I'm trying to do is feasible).

    Saying the code I posted has "nothing to do with your problem" is a little misleading.

    If I posted no code relating to me trying to create the jtable, add it to the jscrollpane, and add it to the jPanel, im sure someone would say, "I don't see where you even created the jTable". So the code is a part of the problem, but in your opinion, the 'problem' is not with that particular section of code, understood.

    Do you think it could have something to do with a property on the JPanel that i'm adding the jscrollpane/table to?
    Last edited by fam2315; 08-02-2011 at 07:04 PM.

  12. #12
    fam2315 is offline Member
    Join Date
    Feb 2011
    Posts
    78
    Rep Power
    0

    Default

    I guess(?)I can mark this one as solved. Just a weird swing quirk I guess. When I ran the application, it did not show the table. I hit the minimize button on the application, re-opened it, and boom, table shows up perfectly. I don't know what that means yet.
    Last edited by fam2315; 08-02-2011 at 07:17 PM.

  13. #13
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,874
    Rep Power
    8

    Default

    I don't think many people here are interested in debating how you should post code, or whether you should use a GUI builder. You've received our advice, it's up to you whether you want to follow it or not. The code you posted doesn't help us figure out what you're actually doing. Using GUI builders almost always leads to posts like this one (where is my problem, here is a small snippet of my code). So cut out both of those problems by creating an SSCCE. Or don't. Up to you.

    I'll be willing to help as soon as you provide the SSCCE. Everything else is irrelevant.
    DarrylBurke likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  14. #14
    fam2315 is offline Member
    Join Date
    Feb 2011
    Posts
    78
    Rep Power
    0

    Default

    I seriously doubt a SSCCE would bring anyone to the conclusion that 'maybe you should just wait 30 seconds and it'll finally show up'. I'm happy with where it's at, I've used the debugger and put in breakpoints as it steps through the code. I'm not doing anything more than i've been pointing out.

    And I'm sure there are a lot of people here who would debate whether I should use a GUI builder or not, because they are certainly quick to suggest that I ditch it. Well, say to ditch it, more than suggest.

    At any rate, thanks for the help.

  15. #15
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,874
    Rep Power
    8

    Default

    I'd be willing to bet the problem has to do with how/when you're adding the components to the JFrame - it's not a "swing quirk", it's expected behavior. That's the kind of stuff you pick up on by not using a GUI builder. I'd also bet that this is a pretty obvious problem that will probably come back to bite you later. But oh well, we won't know for sure without an SSCCE.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by fam2315 View Post
    So since I have a question about swing, that implies that I don't understand swing?
    Since I'm using NetBeans, I don't know how to code by hand?

    Neither of these are true statements. I'll get it figured out, but making assertions like that, not good.

    I respect your opinion, but when you bring up things like "thats an argument to not use NetBeans", and 'learn swing", i feel like those are irrelevant and a little condescending(mainly the 2nd one, you can have your opinion on IDE/coding practices, but the bottom line is NetBeans works well with swing, and what I'm trying to do is feasible).

    Sorry, but if you knew Swing very well, you'd know that the code you've posted is not the cause of the error you're encountering. And besides, there's nothing wrong with stating that you're still learning Swing; heck, I'm still learning Swing, and for that reason don't use an IDE to create my code. Besides I've seen too many problems from folks in similar situations where the IDE-generated code gets in the way of their solving a problem. If that bothers your sensabilities, again, sorry.

    Saying the code I posted has "nothing to do with your problem" is a little misleading.

    If I posted no code relating to me trying to create the jtable, add it to the jscrollpane, and add it to the jPanel, im sure someone would say, "I don't see where you even created the jTable". So the code is a part of the problem, but in your opinion, the 'problem' is not with that particular section of code, understood.
    There is nothing misleading at all about i. I took your code and created an SSCCE from it:

    Java Code:
    import java.awt.BorderLayout;
    
    import javax.swing.*;
    
    public class FooSwing {
       private static void createAndShowGui() {
          JPanel myPanel = new JPanel();
          
          // semicolons have been added below to make code compilable.
          String[][] data = {{"orange","black","blue"}};
          String[] columnNames = {"Col1","Col2","Col3"};
    
          JTable tbl = new JTable(data,columnNames);
          JScrollPane jsp = new JScrollPane(tbl);
    
          myPanel.setLayout(new BorderLayout());
    
          
          JFrame frame = new JFrame("FooSwing");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(jsp);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    And I can state categorically that this code has nothing to do with your problem. It lies elsewhere.



    Quote Originally Posted by fam2315 View Post
    I guess(?)I can mark this one as solved. Just a weird swing quirk I guess. When I ran the application, it did not show the table. I hit the minimize button on the application, re-opened it, and boom, table shows up perfectly. I don't know what that means yet.
    It could mean that you are adding components after you've rendered your GUI (usually after calling setVisible(true) on the JFrame).

    But again, I don't know as I can't see your code, and we can state that it's not a Swing quirk but rather a bug somewhere in your code, possibly due to a misunderstanding in how Swing works.
    Last edited by Fubarable; 08-02-2011 at 10:16 PM.

  17. #17
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    Quote Originally Posted by fam2315 View Post
    i feel like those are irrelevant and a little condescending
    Oh, it's entirely relevant. And you get what you deserve.

    Quote Originally Posted by fam2315 View Post
    Saying the code I posted has "nothing to do with your problem" is a little misleading.
    It's entirely accurate.

    Quote Originally Posted by fam2315 View Post
    Just a weird swing quirk I guess.
    Ever heard the proverb about the bad carpenter?

    Quote Originally Posted by fam2315 View Post
    I hit the minimize button on the application, re-opened it, and boom, table shows up perfectly. I don't know what that means yet.
    I do, but if you want me to tell you then you'll have to post that SSCCE. But if you don't want to learn, I advise you to stay away from forums like these.

    And yes, that' condescending. Because you deserve it.

    db

  18. #18
    fam2315 is offline Member
    Join Date
    Feb 2011
    Posts
    78
    Rep Power
    0

    Default

    thanks again...
    Last edited by fam2315; 08-03-2011 at 02:57 PM.

Similar Threads

  1. Replies: 1
    Last Post: 03-24-2011, 12:51 PM
  2. Having problem displaying JTable
    By rmartinsaraujo in forum AWT / Swing
    Replies: 1
    Last Post: 05-20-2010, 03:34 PM
  3. Adding New JTable in JTable
    By anilkumar_vist in forum New To Java
    Replies: 0
    Last Post: 01-27-2010, 08:27 AM
  4. Displaying Data
    By skiing in forum New To Java
    Replies: 4
    Last Post: 11-15-2008, 02:56 AM
  5. Replies: 2
    Last Post: 05-15-2008, 10:15 AM

Posting Permissions

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