1

Table under which jbuttons to be displayed

I have to display data on a jbutton which is retrieved from a database(mysql) but due to different length of the strings involved the presentation looks bad and disordered. Is there a way to get the alignment.This is my code to set the data on the jbutton 'j' is the jbutton array

  j[check].setText(rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3)+"  "+rs.getString(4)+" "+ws+rs.getString(5)+" "+rs.getString(6)+" "+rs.getString(7));

I have given some spaces for some particular set of data but in most cases it goes bad.

Here are 2 cases in which the lengths are different

CSE201 Programming Fundamentals 3 Saritha V. SJT-401 A1 59
CSE201 Programming Fundamentals 3 Yokesh Babu SJT-402 A1 58

Now after the 3rd string(i.e after "CSE201 Programming Fundamentals 3") the lengths differ and formatting goes bad.

mKorbel
  • 108,320
  • 17
  • 126
  • 296
harry4
  • 189
  • 1
  • 4
  • 16

1 Answers1

2

As kleopatra comments, you need to chose your desired layout. Absent a clear statement of what you mean by bad and disordered, consider these alternatives:

  • FlowLayout, the default for JPanel, will use the enclosed component's preferred size.

  • GridLayout will ensure that the enclosed components are the same size and large enough to accommodate the largest button. An example is shown here for a grid of JPanel instances each containing a JButton; note that the buttons are centered by default. As you want a single row with an arbitrary number of columns, use new GridLayout(1, 0).

  • BoxLayout with LINE_AXIS will attempt to use the preferred width of the enclosed components, as shown here.

Be sure to pack() the enclosing Window. If this is not helpful , please edit your question to include an sscce that exhibits any problem(s) you encounter.

Community
  • 1
  • 1
trashgod
  • 196,350
  • 25
  • 213
  • 918
  • Also consider `JToolBar`, illustrated [here](http://stackoverflow.com/a/11949899/230513). – trashgod Oct 22 '12 at 16:30
  • By bad and disordered i mean that the look and feel.Also the method you suggest would only order size of my buttons but the text in it differs. – harry4 Oct 23 '12 at 05:32
  • A button's preferred size is calculated by the UI delegate based on the size of the name in pixels. If you change the name at run-time, you can `validate()` the enclosing `Container` to recalculate the sizes and `repaint()` to refresh the display. – trashgod Oct 23 '12 at 13:16