Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: JScollPane JTable etc

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JScollPane JTable etc

    Guys

    I'm a complete newby to Java, trying desperately to convert from VB.NET. For days now I have been struggling to implement an interface that I consider to be extremely easy in VB Net.

    I have a JTable that has a checkbox -when it is clicked it will expand 2 hidden(size set to zero) columns that contain images.

    The JTable is within a JScrollPane. I can't get the scrollpane to resize to the wider Table, with scroll bars if needed. I also need the scrollpane to resize, if needed, when the form is resized. I can't get horizontal scroll bars.

    All the above is in a JPanel using GridbagLayout.

    Initially I don't think including my lines of code will help much. I'm hoping someone can point me to an example that services much the same requirements.

    I've looked at many code snippets but most are beyond my current limited Java knowledge.


  2. #2
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: JScollPane JTable etc

    Quote Originally Posted by Bullstreetboy View Post
    The JTable is within a JScrollPane. I can't get the scrollpane to resize to the wider Table, with scroll bars if needed. I also need the scrollpane to resize, if needed, when the form is resized. I can't get horizontal scroll bars.
    JTable must be inserted as a "view" into JScrollPane (generally it's sufficient new JScrollPane(yourTable) but there is also the setViewportView method to set the component to scroll). And scrollbars are automatically managed as necessary.
    Then it's JScrollPane that you must insert into another "container" .... not JTable.

    The sum of columns width, by default, not necessarily fit the width of JScrollPane "view". You can do this, if you want.
    For any other specific question, ask.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  3. The Following User Says Thank You to andbin For This Useful Post:

    GregBrannon (January 14th, 2014)

  4. #3
    Junior Member
    Join Date
    Jan 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JScollPane JTable etc

    Here is a precis of the code:-

     
          CoinTable = new JTable();
          // CoinTable.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
          final DefaultTableModel CoinTableModel = new DefaultTableModel(); 
          CoinTableModel.addColumn("Year");
          CoinTableModel.addColumn("Grade");
          CoinTableModel.addColumn("img");
          CoinTableModel.addColumn("Obverse");
          CoinTableModel.addColumn("Reverse");
          CoinTableModel.addColumn("Weight");
          CoinTableModel.addColumn("Note");
     
          etc
     
          final JScrollPane scrollPane = new JScrollPane();
          CoinTable.setFillsViewportHeight(true);
          scrollPane.setViewportView(CoinTable);
          scrollPane.setPreferredSize(new Dimension(CoinTable.getSize()));
     
         // if I take the above line out NOTHING appears!
     
           listCoinsBody.add(scrollPane);
     
           cons.weightx = cons.weighty = 1;
     
           cons.gridx=0;
           cons.gridy=1;
     
          listCoinsPNL.add(listCoinsBody,cons);
     
           frame.pack();
           frame.setSize(1130,633);
     
            scrollPane.setPreferredSize(new Dimension(CoinTable.getSize()));
     
           // again if I take this out nothing appears.

  5. #4
    Junior Member
    Join Date
    Jan 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JScollPane JTable etc

    Guys

    Lack of feedback:- I'm beginning to think this is not a Newbie trivial error. Since posting I've explored many postings for a possible explanation, tried many things, and I just cannot advance. The features I require are so simple for an experienced VB.Net programmer(and a programmer in many languages for over 40 years) - why so complicated in Java? I've wasted days and many MANY hours trying to get a simple GUI requirement working in Java. I don't want to give up, and will not.

    Someone out there must have done this before?!

    Extremely disappointed and frustrated!

  6. #5
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: JScollPane JTable etc

    Quote Originally Posted by Bullstreetboy View Post
    Lack of feedback:- I'm beginning to think this is not a Newbie trivial error. Since posting I've explored many postings for a possible explanation, tried many things, and I just cannot advance. The features I require are so simple for an experienced VB.Net programmer(and a programmer in many languages for over 40 years) - why so complicated in Java? I've wasted days and many MANY hours trying to get a simple GUI requirement working in Java. I don't want to give up, and will not.
    Extremely disappointed and frustrated!
    Wait ... calm.
    The first thing that is not really "good" is the CoinTable.getSize() for the scrollPane preferred size. At that time, the real size (getSize() ) of JTable is surely small (or even 0x0).

    Can you re-explain what are your requirements for dimensions? And also what/where is that listCoinsBody component.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  7. #6
    Junior Member
    Join Date
    Jan 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JScollPane JTable etc

    Just in case it makes a difference I'm using Netbeans!

  8. #7
    Junior Member
    Join Date
    Jan 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JScollPane JTable etc

    Thanks for the reply Andbin

    Lets see if a picture(S) is really worth a thousand words
    Here s the VB.NET GUI unchecked

    java1.jpg

    Here is the VB>NET GUI with a row checked

    java3.jpg

    Notice the scroll bars

    You may be right - I have be-dugged Cointable.getsize() returns oxo before the frame is packed and displayed even after dolayout.

    ListCoinsBody is a JPanel with a GridBoxLayout(Is this the problem? a Jscoll within a Janel? - probably not)

    I re-iterate that I am a complete novice to JAVA so have probably inadvertently broken a basic rule.

    Any hints/advice will be much appreciated as I am getting close to throwing the toys out of the pram!

    Maybe I should read the Java Tutorial from start to finish before trying a major re-write!

    I'm after the same as clearly displayed in the netbeans screen dump lower right hand side panel:-
    NetBeansDump.jpg

Similar Threads

  1. Help me please, Jtable
    By mazaoa in forum AWT / Java Swing
    Replies: 1
    Last Post: December 9th, 2012, 09:16 AM
  2. Replies: 21
    Last Post: November 27th, 2012, 10:58 PM
  3. Hello everyone! I need help with JTable
    By hektor in forum AWT / Java Swing
    Replies: 6
    Last Post: October 11th, 2010, 10:11 AM