1

Right, I have a JTabbedPane that has a JPanel that contains a JLabel and a JTextField.

my code

JTabbed Pane declaration :

        this.tabPane = new JTabbedPane();
    this.tabPane.setSize(750, 50);
    this.tabPane.setLocation(10, 10);
        tabPane.setSize(750,450);
    tabPane.add("ControlPanel",controlPanel);

textfield declaration :

    this.channelTxtFld = new JTextField("");
    this.channelTxtFld.setFont(this.indentedFont);
    this.channelTxtFld.setSize(200, 30);
    this.channelTxtFld.setLocation(200, 10);

JLabel : this.channelLabel = new JLabel("Channel name : "); this.channelLabel.setSize(150, 30); this.channelLabel.setLocation(10,10);

private void createPanels() {
    controlPanel = new JPanel();
    controlPanel.setSize(650,500);
}
 private void fillPanels() {
    controlPanel.add(channelLabel);
    controlPanel.add(channelTxtFld);

}

So what my plan is, was to have a tabbed pane that has a JPanel where I have some Labels, textfields and buttons on fixed positions, but after doing this this is my result:

https://i.stack.imgur.com/vXa68.png

What I wanted was that I had the JLabel and next to it a full grown JTextfield on the left side not in the middle.

Anyone any idea what my mistake is ?

thank you :)

Lucas Kauffman
  • 6,789
  • 15
  • 60
  • 86

3 Answers3

3

What kind of Layout Manager are you using for your controlPanel, you probably want BorderLayout, putting the Label in the West, and the TextField in the center.

BTW, setting the size and position of various components doesn't make sense unless you are using a Null Layout, which isn't a good idea. So i'd remove all that stuff and let the Layout Manager do it for you.

MeBigFatGuy
  • 28,272
  • 7
  • 61
  • 66
  • I wasn't using a layoutmanager, but I only have experience with the flow layout which I didnt like, I will try the borderLayout. Does it also work if you have to add like 16 Labels,textfields and buttons. I didn't want to use a LayOutManager but I guess I will have to :( I wanted to position it myself within the panel. thanks for the answer I'll play a bit with it :) – Lucas Kauffman Apr 24 '11 at 23:31
  • 1
    Yes, you do want to use a Layout Manager, doing otherwise will cause all kinds of problems for you. If you are building a complicated form-like dialog, then i'd recommend using jgoodies form layout. http://www.jgoodies.com/freeware/forms/ – MeBigFatGuy Apr 24 '11 at 23:37
3

Use a LayoutManager and consider also the methods setPreferredSize, setMinimumSize, setMaximumSize to adjust components bounds according on which is your desired effect.

Heisenbug
  • 38,762
  • 28
  • 132
  • 190
  • The `setPreferredSize()` method generally carries more weight with layout managers than the other two, but should be called sparingly. – Andrew Thompson Apr 24 '11 at 23:43
3

Assuming the default JPanel layout, FlowLayout, give the JTextField a non-zero number of columns, and give the JLabel a JLabel.LEFT constraint.

Addendum:

a full grown JTextField

Something like this?

enter image description here

import java.awt.*;
import javax.swing.*;

/**
 * @see http://stackoverflow.com/questions/5773874
 */
public class JTabbedText {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            private final JTabbedPane jtp = new JTabbedPane();

            @Override
            public void run() {
                JFrame f = new JFrame();
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                jtp.setPreferredSize(new Dimension(400, 200));
                jtp.addTab("Control", new MyPanel("Channel"));

                f.add(jtp, BorderLayout.CENTER);
                f.pack();
                f.setVisible(true);
            }
        });
    }

    private static class MyPanel extends JPanel {

        private final JLabel label = new JLabel("", JLabel.LEFT);
        private final JTextField text = new JTextField();

        public MyPanel(String name) {
            this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
            label.setText(name);
            label.setAlignmentY(JLabel.TOP_ALIGNMENT);
            text.setAlignmentY(JTextField.TOP_ALIGNMENT);
            this.add(label);
            this.add(text);
        }
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045