0

I have a JTable inside a JScrollView But It doesn't seem to be aligned well more over the scroll bar is tiny i am guessing that this is because of the alignment.Moreover it is here is a snippet from the real code :

    JFrame frame = new JFrame();
    frame.setResizable(false);
    frame.setBounds(100, 100, 771, 453);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JPanel panel = new JPanel();
    panel.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0)));
    panel.setBounds(10, 32, 747, 370);
    frame.getContentPane().add(panel);
    panel.setLayout(null);

    JPanel VPanel = new JPanel();
    VPanel.setBounds(297, 43, 440, 224);
    panel.add(VPanel);

    JTable TableV = new JTable();
    TableV.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    TableV.getTableHeader().setReorderingAllowed(false);
    DefaultTableModel Model = new DefaultTableModel(0, 0);
    String header[] = new String[] { "Country", "ID", "WAN IP", "User", "OS", "Java version" };

    VPanel.add(new JScrollPane(TableV, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED));

    // just adding some data to fill the table
    Model.setColumnIdentifiers(header);
    //set model into the table object
    TableV.setModel(Model);

     //just adding some diff data to check if it scrolls down
     for (int count = 1; count <= 50; count++)
     {
        Model.addRow(new Object[] { "data1", "data2", "data3", "data4", "data5", "data6" });
     }

     for (int count = 1; count <= 70; count++)
     {
        Model.addRow(new Object[] { "data100", "data200", "data300", "data400", "data500", "data600" });
     }

     frame.setVisible(true);
5
  • 1
    Your use of null layouts and setBounds is going to mess you over, either now or later, and you will want to get out of the newbie habit of doing this. Learn the layout managers and then use the layout managers. Jan 21, 2016 at 18:00
  • 1
    Understand that VPanel (which should be renamed vPanel to comply with Java naming rules) uses FlowLayout, and so the scrollpane may not fit well within it. Give it a BorderLayout and add the JScrollPane BorderLayout.CENTER if you want the scroll pane to fill it. Jan 21, 2016 at 18:01
  • If you need more exacting help, then tell us more about what is happening exactly since "it is not aligning well" is a bit vague. Consider posting an image of what you're seeing and what you want to see. Finally create and post your minimal reproducible example so we can actually run your code for ourselves. Jan 21, 2016 at 18:06
  • Thanks @HovercraftFullOfEels just edited the to JPanel VictimPanel = new JPanel(new BorderLayout()); and fixed it thanks, will learn more about layout managers, yes i do know that the namings are not following the conventions will work on it, i just like how it looks thats why mostly i code that way but will change it from now onwards thanks
    – user5342366
    Jan 21, 2016 at 18:06
  • It was as u said the BorderLayout issue @HovercraftFullOfEels, can you asnwer that so tha ti can mark it as the answer?
    – user5342366
    Jan 21, 2016 at 18:07

1 Answer 1

2

Understand that VPanel (which should be renamed vPanel to comply with Java naming rules) uses FlowLayout, and so the scrollpane may not fit well within it. Give it a BorderLayout and add the JScrollPane BorderLayout.CENTER if you want the scroll pane to fill it.

e.g.,

import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

@SuppressWarnings("serial")
public class TableFoo extends JPanel {
    private static final String[] HEADER = new String[] { "Country", "ID", "WAN IP", "User", "OS", "Java version" };
    private static final int PREF_W = 500;
    private static final int PREF_H = 400;
    private DefaultTableModel model = new DefaultTableModel(HEADER, 0);
    private JTable table = new JTable(model);

    public TableFoo() {
        for (int count = 0; count < 50; count++) {
            model.addRow(new Object[] { "data1", "data2", "data3", "data4", "data5", "data6" });
        }

        for (int count = 0; count < 70; count++) {
            model.addRow(new Object[] { "data100", "data200", "data300", "data400", "data500", "data600" });
        }

        JScrollPane scrollPane = new JScrollPane(table);

        setLayout(new BorderLayout());
        add(scrollPane, BorderLayout.CENTER);
    }

    @Override
    public Dimension getPreferredSize() {
        if (isPreferredSizeSet()) {
            return super.getPreferredSize();
        }
        return new Dimension(PREF_W, PREF_H);
    }

    private static void createAndShowGui() {
        JFrame frame = new JFrame("TableFoo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new TableFoo());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
            }
        });
    }
}
1
  • Thanks for the example @Hovercraft Full Of Eels
    – user5342366
    Jan 21, 2016 at 18:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.