Results 1 to 8 of 8
  1. #1
    okabeer is offline Member
    Join Date
    Apr 2009
    Posts
    15
    Rep Power
    0

    Default organizing elements in the jpanel/jbox

    Hi Everyone,

    I am still new to jswing and I am teaching myself/ learning from this forum. i was wondering how could i left justify this or organize it so it looks organized.

    private JPanel sizePanel() {
    JPanel panel = new JPanel();
    BoxLayout box = new BoxLayout(panel, BoxLayout.Y_AXIS);
    panel.setLayout(box);
    TitledBorder TB = new TitledBorder("Run Size");
    panel.setBorder(TB);

    timeStepTF = new JTextField(5);
    ionsTF = new JTextField(5);
    electronsTF = new JTextField(5);
    JPanel row1P = newRow("Time Step", timeStepTF, "0.15", "Ions", ionsTF, "10", "Electrons", electronsTF, "10");

    radialTF = new JTextField(5);
    poloidalTF = new JTextField(5);
    toroidalTF = new JTextField(5);
    JPanel row2P = newRow("Radial", radialTF, "90","Poloidal", poloidalTF, "640","Toroidal", toroidalTF, "32");

    subcycleTF = new JTextField(5);
    trackTF = new JTextField(5);
    JPanel row3P;
    row3P = newRow("Substeps", subcycleTF, "0","Track Particles", trackTF, "0");

    panel.add(row1P);
    panel.add(row2P);
    panel.add(row3P);
    return panel;
    }

  2. #2
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    7

    Default

    For each row, you can also use a (horizontal) box. I'd recommend using the Box class and the method Box.createHorizontalBox().

  3. #3
    okabeer is offline Member
    Join Date
    Apr 2009
    Posts
    15
    Rep Power
    0

    Default thank you

    thank you let me try and see if i works and if i can figure it out.

  4. #4
    okabeer is offline Member
    Join Date
    Apr 2009
    Posts
    15
    Rep Power
    0

    Default i am still confused

    I was wondering if you could illustrate what u said earlier with an example and then i would research into it and do it myself.

  5. #5
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    7

    Default

    So make your 'newRow' method return a horizontal box:

    Java Code:
    private Component newRow(String label, JTextField field1....) {
      Box b = Box.createHorizontalBox();
      b.add(new JLabel(label));
      b.add(field1);
      ...
      return b;
    }
    I'd actually then probably use a vertical box to hold the rows:

    Java Code:
    Box vb = Box.createVerticalBox();
    vb.add(new Row("Blah", tf1, ...));
    ...
    add(vb, BorderLayout.CENTER);
    To stop the rows resizing vertically, you may find you need to add some vertical "glue" after the rows:

    Java Code:
    vb.add(Box.createVerticalGlue());

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Lots of ways to do this one. Consider using GridLayouts or GridBagLayout too. Another nice layout to consider is the third party MigLayout. I've heard nice things about that one.

  7. #7
    okabeer is offline Member
    Join Date
    Apr 2009
    Posts
    15
    Rep Power
    0

    Default easier way

    I have to change this for atleast 20 different types of jlabels and j text boxes. its a 20 page code don't worry i won't post it here... can anyone give a template for the one above so i can work on it... i don't want to change alot of the code since it is being used by other codes and I am in a cvs.

  8. #8
    logicbug's Avatar
    logicbug is offline Member
    Join Date
    Jan 2009
    Location
    The Great White North, eh?
    Posts
    76
    Rep Power
    0

    Default

    Ill extend on what Fubarable said, there are lots of ways to do this, I prefer using the GridBagLayout and GridBagConstraints to do this, a quick search in google will give you examples of how to use it. In my opinion it's the best in the standard java library.

Similar Threads

  1. what does <Elements> mean
    By JordashTalon in forum New To Java
    Replies: 1
    Last Post: 01-31-2009, 11:19 PM
  2. organizing larger application code / modules
    By zenner in forum Advanced Java
    Replies: 5
    Last Post: 07-28-2008, 10:18 AM
  3. using elements from other classes
    By Camden in forum New To Java
    Replies: 1
    Last Post: 03-21-2008, 08:25 AM
  4. Elements package
    By BlitzA in forum New To Java
    Replies: 0
    Last Post: 12-28-2007, 12:58 AM
  5. deleting elements
    By nalinda in forum New To Java
    Replies: 2
    Last Post: 12-06-2007, 02:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •