Search code examples
javaswinglayout-managergridbaglayoutindentation

Indentating of column name


enter image description here

I am making a GUI in which i tried to indent the column name.I am also pasting my source code. I am using the GridBagConstraints. This my code where i create a GridBagConstraints but nothing seems to happen

 private final static Dimension VOLUME_FIELD_DIMENSION = new Dimension(200, 25);
    private final static Dimension NAME_FIELD_DIMENSION = new Dimension(200, 25);
    private final static Dimension IMAGE_FIELD_DIMENSION = new Dimension(200, 25);
    private final static Dimension DATE_FIELD_DIMENSION = new Dimension(150, 25);
    private final static Dimension STATUS_FIELD_DIMENSION = new Dimension(100, 25);
    private final static Dimension IMAGE_PANEL_DIMENSION = new Dimension(1000,250);            
     private static void ImagePanel(ArrayList<ResourceListObject> arr, JPanel imagelist)
    {


        for(int i=0 ; i < arr.size();i++)
        {
            try {
                // column 1: name
                JTextField t1 = createTextField(arr.get(i).getName(), NAME_FIELD_DIMENSION);

                // column 2: Created At
                JTextField t2 = createTextField(arr.get(i).getCreatedDate(), DATE_FIELD_DIMENSION);


                // column 3: Status 
                JTextField t3 = createTextField(arr.get(i).getStatus(), STATUS_FIELD_DIMENSION);

                // column 4: Image Id
                JTextField t4 = createTextField(arr.get(i).getID(), IMAGE_FIELD_DIMENSION);

                //column 5:Volume Id
                JTextField t5 = createTextField(arr.get(i).getVolume_id(), VOLUME_FIELD_DIMENSION);
                // construct the row (panel), and then add it to the list panel
                JPanel rowPanel = new JPanel();
                rowPanel.setLayout(new BoxLayout(rowPanel, BoxLayout.X_AXIS));
                rowPanel.add(t1);
                rowPanel.add(t2);
                rowPanel.add(t3);
                rowPanel.add(t4);
                rowPanel.add(t5);
                imagelist.add(rowPanel);
            } catch (Exception e) {
                 }

        }


    }

    private static JTextField createTextField(String text, Dimension d) {
        JTextField tf = new JTextField(text);
        tf.setPreferredSize(d);
        tf.setBackground(Color.WHITE);
        tf.setEditable(false);
        return tf;
    }

    private static JPanel getColumnHeadings() {

        GridBagConstraints regularFieldConstraints = new GridBagConstraints();
        regularFieldConstraints.fill = GridBagConstraints.BOTH;
        String nameColumn = "Image Name";
        String secondColumn = "Created At";
        String thirdColumn = "Status";
        String fourthColumn = "Image Id";
        String fifthColumn = "Volume Id";

        JLabel name = new JLabel(nameColumn);
        JLabel created_at = new JLabel(secondColumn);
        JLabel status = new JLabel(thirdColumn);
        JLabel id = new JLabel(fourthColumn);
        JLabel volume_id = new JLabel(fifthColumn);

        name.setPreferredSize(NAME_FIELD_DIMENSION);
        created_at.setPreferredSize(DATE_FIELD_DIMENSION);
        status.setPreferredSize(STATUS_FIELD_DIMENSION);
        id.setPreferredSize(IMAGE_FIELD_DIMENSION);
        volume_id.setPreferredSize(VOLUME_FIELD_DIMENSION);

        JPanel headingsPanel = new JPanel(new GridBagLayout());
        headingsPanel.add(name, regularFieldConstraints);
        headingsPanel.add(created_at, regularFieldConstraints);
        headingsPanel.add(status, regularFieldConstraints);
        headingsPanel.add(id, regularFieldConstraints);
        headingsPanel.add(volume_id, regularFieldConstraints);

        return headingsPanel;
    }

Solution

  • The class sun.swing.table.DefaultTableCellHeaderRenderer is not part of the published API. If you are reluctant to depend on it, consider these alternative options:

    image

    import java.awt.BorderLayout;
    import java.awt.Component;
    import java.awt.Dimension;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.SwingUtilities;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.table.TableCellRenderer;
    
    public class Demo {
    
        /** @see https://stackoverflow.com/a/6644956/230513 */
        private static class HeaderRenderer implements TableCellRenderer {
    
            TableCellRenderer renderer;
    
            public HeaderRenderer(JTable table) {
                renderer = table.getTableHeader().getDefaultRenderer();
            }
    
            @Override
            public Component getTableCellRendererComponent(
                JTable table, Object value, boolean isSelected,
                boolean hasFocus, int row, int col) {
                // assumes JLabel
                JLabel label = (JLabel) renderer.getTableCellRendererComponent(
                           table, value, isSelected, hasFocus, row, col);
                label.setHorizontalAlignment(JLabel.LEADING);
                return label;
            }
        }
    
        private void initGUI() {
            Object[] columnNames = new Object[]{
                "Image Name", "Created At", "Status", "Image Id", "Volume Id"};
            DefaultTableModel dtm = new DefaultTableModel(columnNames, 0);
            dtm.addRow(new Object[]{"Some picture's name", "2013-10-05",
                "Status unknown", "0123456789", "9876543210"});
    
            JTable table = new JTable(dtm);
            table.getTableHeader().setDefaultRenderer(new HeaderRenderer(table));
    
            JScrollPane scrollPane = new JScrollPane(table);
    
            JPanel content = new JPanel(new BorderLayout());
            content.setPreferredSize(new Dimension(600, 250));
            content.add(scrollPane);
    
            JFrame frame = new JFrame("Demo");
            frame.setContentPane(content);
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new Demo().initGUI();
                }
            });
        }
    }