5

I have a JPanel with Constraint's of Y_Axis so that whenever I add a new Component it will automatically be Added on a new Line.But the Problem is that the Label inside is not Aligned to Left or Right. It is displayed at some distance above the JTable. How can JLabel be displayed at desired Alginment.

JPanel panel = new JPanel();

panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));

Then I added a JLabel inside panel.

JLabel labelSemester = new JLabel("Semester 1: ",SwingConstants.LEFT);
panel.add(labelSemester);

After label, I added a new JTable inside panel,

// Column Names for the Table
Object[] col_names = {"ID", "Name", "CH", "Marks", "Grade"};
// row data for the table
Object[][] table_rows = {{"CS123","Introduction to Computing",3,80,"A-"}};// One row only

JTable table = new JTable(table_rows, col_names);

panel.add(new JScrollPane(table));

Then I added a JFrame and added the Panel to show in the frame

JFrame frame = new JFrame();
// frame Title
frame.setTitle("DMC");
frame.setSize(400,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

// adding panel inside frame
frame.add(panel);

// displaying frame
frame.show()

Note: I have added code for auto Adjustment of column width of JTable. Output can be seen from attached Image

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Adnan Mohib
  • 331
  • 5
  • 16
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height - to show how the extra space should be used. 3) The way I'd approach the above is (probably) to create a panel with a `BorderLayout` (perhaps add that panel to the box layout) then add the label to the `PAGE_START` constraint of the border layout and the table to the `CENTER`. – Andrew Thompson Sep 16 '17 at 17:34
  • Thanks @AndrewThompson for your guidance. I have posted the minimal version of the problem and I have also tested the above code shown in screen shot . in Actual code I am adding many labels and tables dynamically in the frame. I didn't get your suggested solution would you please like to explain it little bit. What I get is that 1- Make a panel with BorderLayout 2-add that panel to box layout 3- add the label in the panel with constrain of PAGE_START 4- add the table in the panel with CENTER constraint correct me if I am getting it wrong. Thanks. – Adnan Mohib Sep 17 '17 at 12:58
  • 1
    *"I have posted the minimal version of the problem.."* Seemingly as minimal as your attention span. Note that MCVE has 4 letters, the uncompilable code snippets seen above are only M, but not C, V or an E. It's S, but not SC, C or an E. Follow the links, **read** the pages. – Andrew Thompson Sep 17 '17 at 13:57

1 Answers1

12

All components added to the BoxLayout need the same alignmentX, otherwise you can get some weird layouts:

//JLabel labelSemester = new JLabel("Semester 1: ",SwingConstants.LEFT);
JLabel labelSemester = new JLabel("Semester 1: ");
label.semester.setAlignmentX(JLabel.LEFT_ALIGNMENT);
panel.add(labelSemester);

...

JTable table = new JTable(table_rows, col_names);
//panel.add(new JScrollPane(table));
JScrollPane scrollPane = new JScrollPane( table );
scrollPane.setAlignmentX(JScrollPane.LEFT_ALIGNMENT);
panel.add( scrollPane );

Read the section from the Swing BoxLayout tutorial on Fixing Alignment Problems for more information. Keep a link to the tutorial handy for all Swing basics.

camickr
  • 321,443
  • 19
  • 166
  • 288