How to make JTable both AutoResize and horizontall scrollable

0 votes

I am putting a JTable into a JScrollPane

But When I set JTable Auto Resizeable, then it won't have horizontal scroll bar.

if I set AUTO_RESIZE_OFF, then the Jtable won't fill the width of its container when the column width is not big enough.

So how can I do this:

  1. when the table is not wide enough, expand to fill its container width
  2. when the table is wide enough, make it scrollable.
Apr 21, 2020 in Java by kartik
• 37,520 points
1,932 views

1 answer to this question.

0 votes

Hello @kartik,

If for some reason customising JTable is not an option (e.g. it might be created in third-party code), you can achieve the same result by setting it to toggle between two different JTable AUTO_RESIZE modes whenever the containing viewport is resized, e.g.:

jTable.getParent().addComponentListener(new ComponentAdapter() {
    @Override
    public void componentResized(final ComponentEvent e) {
        if (jTable.getPreferredSize().width < jTable.getParent().getWidth()) {
            jTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
        } else {
            jTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        }
    }
});
answered Apr 21, 2020 by Niroj
• 82,840 points

Related Questions In Java

0 votes
2 answers

How do I read and convert an InputStream object to string type?

You can also use Java Standard Library ...READ MORE

answered Jul 17, 2018 in Java by Sushmita
• 6,920 points
17,218 views
0 votes
2 answers

How to convert byte array to String and STring to byte array?

The best possible way of conversion between byte[] and String is to ...READ MORE

answered Aug 22, 2019 in Java by Sirajul
• 59,230 points
3,596 views
0 votes
1 answer

How to read and write on an excel files?

Follow these steps: Write: public class User { ...READ MORE

answered May 15, 2018 in Java by Rishabh
• 3,620 points
1,536 views
0 votes
1 answer

How to download and save a file from Internet using Java?

public void saveUrl(final String filename, final String ...READ MORE

answered May 25, 2018 in Java by Rishabh
• 3,620 points
910 views
0 votes
1 answer

What are the prerequisites to learn Hadoop in java perspective?

In my day job, I've just spent ...READ MORE

answered Oct 11, 2018 in Big Data Hadoop by Frankie
• 9,830 points
775 views
0 votes
1 answer

How is inheritance in C++ different than that in Java?

The purpose of inheritance is same for ...READ MORE

answered Feb 6, 2019 in Java by Priyaj
• 58,020 points
973 views
0 votes
1 answer

What is the reason for no ConcurrentHashSet against ConcurrentHashMap?

Hello, There's no built in type for ConcurrentHashSet because you ...READ MORE

answered Apr 8, 2020 in Java by Niroj
• 82,840 points
1,468 views
0 votes
1 answer

How to pass an object from one activity to another on Android?

Hello @kartik, Implement your class with Serializable. Let's ...READ MORE

answered Apr 8, 2020 in Java by Niroj
• 82,840 points
769 views
0 votes
1 answer

How to make an Android device vibrate?

Hello @kartik, Try: import android.os.Vibrator; Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); // ...READ MORE

answered May 25, 2020 in Java by Niroj
• 82,840 points
635 views
0 votes
1 answer

How to import android project as library and NOT compile it as apk?

Hello, In projLib's build.gradle file, you'll see a statement ...READ MORE

answered May 26, 2020 in Java by Niroj
• 82,840 points
1,618 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP