JTabbedPane JLabel, JTextField

That's right, I have a JTabbedPane that has a JPanel that contains JLabel and JTextField.

my code

JTabbed Pane Announcement:

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

text field 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 my plan is to have a tabbed panel in which there is a JPanel where I have some shortcuts, text fields and buttons at fixed positions, but after that this is my result:

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

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

Does anyone know what my mistake is?

Thank you:)

+1
java swing jbutton jlabel jtextfield
Apr 24 '11 at 23:18
source share
3 answers

Which layout manager you use for your control panel, you probably want a BorderLayout by placing a shortcut to the west and a text filter in the center.

By the way, setting the size and position of the various components does not make sense if you are not using the Null layout, which is not a good idea. So I delete all this and let the Layout do it for you.

+3
Apr 24 '11 at 23:25
source share

Use the LayoutManager and also look at the setPreferredSize, setMinimumSize, setMaximumSize methods to adjust the borders of the components to suit your desired effect.

+3
Apr 24 '11 at 23:26
source share

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

Addendum:

JTextField full height

Something like that?

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); } } } 
+3
Apr 24 '11 at 23:33
source share



All Articles