1

Is it possible to create a JTable like the one below from Internet Download Manager?

As you can see, it shows that the last column in the picture is the Date Added and the right side of it is like an empty column, those grid of rows on the right side is not rows and the column of it does not belong to the real columns with titles because of the selected row is ended at the Data Added column, when I tried the table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); I get this result.

So now I want the table to fills ups the empty spaces like in the first picture and still will create a horizontal scrollbar when the columns of the table reached the maximum width like below.

EDIT: Marco13 said - "Did you consider adding an empty column?"

Yes, look take a look at this

The empty column is part of all the columns, and has a horizontal scrollbar at the bottom because it is expanded over the frame, what I want to do is...

  1. All the columns are not auto resizing except for the empty column when resizing the frame.
  2. The empty column is fixed in the right side of the frame so when resizing, it will not leave blank pane.
  3. The horizontal scrollbar should not appear when all columns with title are visible in the whole frame.
  4. The empty column and rows in it cannot be selected.
Mang Kanor
  • 11
  • 3
  • Of course. You need JTable + JScrollBar – Blasanka Aug 04 '17 at 13:57
  • Is the core point of the question how to create the grid of rows on the right side (instead of just leaving the space empty)? – Marco13 Aug 05 '17 at 00:46
  • @Marco13 - yes, exactly. As you can see, those grid of rows on the right side is not rows and the column of it does not belong to the real columns with titles because of the selected row is ended at the Data Added column. – Mang Kanor Aug 05 '17 at 01:51
  • OK, I don't see why this could be "important" enough to invest time and effort in this, but ... maybe it's just the geeky wish to "get something done". Did you consider adding an empty column (i.e. a column without a header)? That's how I'd try this if I had to... – Marco13 Aug 05 '17 at 03:31
  • Already tried it, but when I select rows, that column will be selected too which is not like in the Internet Download Manager, also when I expand the frame size of the Internet Download Manager, the last column is expanding too, but not the columns with Titles. – Mang Kanor Aug 05 '17 at 03:45
  • @MangKanor I have provided you an answer. Then you have updated and given multiple requirements to us to get done for you. Please remember we are not your employee or you are not our client to get done your requirements for you. You can follow along with my answer and links that I provided. And if it is not fits to your requierements instead of editig the question, do reasearch and try to achieve it. If you then encounter specific problem ask another question(I think my updated answer is fit to your question before your edit). – Blasanka Aug 05 '17 at 09:20

1 Answers1

1

You need to use below code for vertical and horizontal scrollbar:

scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane .HORIZONTAL_SCROLLBAR_AS_NEEDED);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

To resize table when JFrame resizes. You need to override the getScrollableTracksViewportWidth().

Complete code:

import java.awt.CardLayout;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableColumn;
public class TableExample {
    JFrame frame;
    JTable table;
    JScrollPane scrollPane;

    public TableExample(){
        initComp();
    }

    public void initComp(){
         frame = new JFrame();
         frame.setSize(500, 400);
         frame.setLayout(new CardLayout());

         table = new JTable(100, 5){
                public boolean getScrollableTracksViewportWidth(){

                    return getPreferredSize().width < getParent().getWidth();

                }
            };

         table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
         //table.setFillsViewportHeight(true);
         //table.setPreferredScrollableViewportSize(new Dimension(300, 200)); 

         scrollPane = new JScrollPane(table);
         scrollPane.setOpaque(true);
         scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
         scrollPane.setHorizontalScrollBarPolicy(JScrollPane .HORIZONTAL_SCROLLBAR_AS_NEEDED);


         frame.add(scrollPane);

         //-----this part from doc
         TableColumn column = null;
         for (int i = 0; i < 5; i++) {
             column = table.getColumnModel().getColumn(i);
             if (i == 4) {
                 column.setPreferredWidth(200); //third column is bigger
             } else {
                 column.setPreferredWidth(100);
             }
         }
         //----

         frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
         frame.setVisible(true);
    }

    public static void main(String[] args) {
        new TableExample();
    }
}

Output:

enter image description here

You can get some idea from: How to make JTable both AutoResize and horizontall scrollable? and also this question @camickr answer may help.

Lastly, if you want to adjest column size to its content width(length of the value), you need to use Table Column Adjuster

Blasanka
  • 21,001
  • 12
  • 102
  • 104