0

I want to render a table in message dialog in which the table should be scroll able. I'm able to achieve most of my requirements but the scrollbar is not visible.

Also I want the size of the message dialog box to be fixed and if the table content doesn't fit the vertical scrollbar should appear.

import javax.swing.*;

public class SystemTrayDemo
{
    public static void main(String[] args)
    {
        String[][] data = {
                {"1","2"},{"2","2"}
        };

        String[] lg = {"x","y"};
        JTable table = new JTable(data,lg);
        JScrollPane jScrollPane = new JScrollPane();
        jScrollPane.getViewport().add(table);
        JOptionPane.showMessageDialog(null,jScrollPane);
        System.exit(0);
    }
}

I'm getting this from the code above:

Note the empty area and no scrollbar

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • I think it's not. While looking to resolve the issue, i visited the question that is mentioned, but the scenario mentioned doesn't match the issue i'm having, since I'm using a JOptionPane.showMessageDialog() . – cold.coffee. Oct 26 '17 at 13:49
  • 1
    Yes it is the same issue. The issue is the default size of a JTable which is hardcoded in the Java classes. Whether you add the table to a JOptionPane or a JFrame you will see the same result. The implementation will make the height match the preferred height based on the data added to the model. If you want the height to be less (so scrollbars will appear) then you need to modify the logic. – camickr Oct 26 '17 at 14:57

0 Answers0