-1

in the below code i am trying to create tabs.

1-according to the code below, the jlabel the contains the word "Demo", at run time, it does not show up! why that is happening and how to force that jlabel to appear?

2-as you see in the code, the function createTab1() should create jTextField and JPasswordField. at run time they are appearing but they are alligined horizontally, what i want is, to display the JTextField and its jlabel beside each other horizontally and the second jlabel and the JpassworfField beside each other but under the jTextField and its jLabel, as shown below:

 Labe     jtextField
 label    jPasswordField

but the code below result the GUI shown in this image

Code:

private void setUpGUI() {
    // TODO Auto-generated method stub
    jFrame_Main = new JFrame("Main Window");
    jPanel_ContainerPanel = new JPanel(new BorderLayout());
    jPanel_ContainerPanel.setBorder(BorderFactory.createLoweredBevelBorder());
    jLabel_ContainerLabel = new JLabel("Demo");
    jLabel_ContainerLabel.setHorizontalAlignment(SwingConstants.CENTER);

    jPanel_ContainerPanel.add(jLabel_ContainerLabel, BorderLayout.NORTH);

    createTab1();
    //createTab2();

    jTabbedPane = new JTabbedPane();
    jTabbedPane.add("tab1", jPanel1);

    jFrame_Main.getContentPane().add(jPanel_ContainerPanel);
    jFrame_Main.getContentPane().add(jTabbedPane);
    jFrame_Main.pack();
    jFrame_Main.setVisible(true);
}

private void createTab1() {
    // TODO Auto-generated method stub
    jPanel1 = new JPanel();
    jPanel1.setBounds(10, 15, 150, 20);
    jLabel1 = new JLabel("userName");
    jPanel1.add(jLabel1);

    JTextField field = new JTextField();
    field.setBounds( 10, 35, 150, 50 );
    jPanel1.add( field );

    JLabel label2 = new JLabel( "Password:" );
    label2.setBounds( 10, 60, 150, 20 );
    jPanel1.add( label2 );

    JPasswordField fieldPass = new JPasswordField();
    fieldPass.setBounds( 10, 80, 150, 20 );
    jPanel1.add( fieldPass );
}

public static void main(String[] args) {
    GUITabs guiTabs = new GUITabs();
}

}

Amrmsmb
  • 1
  • 27
  • 104
  • 226
  • 1
    why don't you use netbeans ? you can easily design those type of GUI s. – MeshBoy Mar 29 '15 at 11:15
  • @MeshBoy actually i ahve not used it before but , does it offer more handy ways to draw the gui , like using drag and drop for an example...because currently i am using eclipse – Amrmsmb Mar 29 '15 at 11:20
  • 1
    yes darg and drop is enabled in netbeans. You can drag panels buttons labels and etc. to the jframe and make GUI s easily. – MeshBoy Mar 29 '15 at 11:22
  • 1
    [Link](https://netbeans.org/downloads/) you can download netbeans IDE from this link – MeshBoy Mar 29 '15 at 11:23
  • @MeshBoy i downloaded netbeans but i really do not know where to go to find swing editor with drag and drop...can u please help – Amrmsmb Mar 30 '15 at 08:15

1 Answers1

1

Problems:

  • You are using setBounds(...) to add a component to a container (a JPanel) that by default uses a FlowLayout, a layout that does not respect the size/position/bounds properties of its components.
  • While yes, you could use setBounds(...) on a JPanel that is explicitly defined to use null layouts, I'm going suggest that you not do this as this makes for very inflexible GUI's that while they might look good on one platform look terrible on most other platforms or screen resolutions and that are very difficult to update and maintain. Instead you will want to study and learn the layout managers and then nest JPanels, each using its own layout manager to create pleasing and complex GUI's that look good on all OS's.
  • It has been suggested that you use NetBeans drag-and-drop to create your complex GUI's, and while this will work, you will not understand the underlying layouts that NetBeans and Swing are using, and thus will eventually run up into a wall when you need to go beyond the abilities of NetBeans, such as if you want to change layout or add components in the midst of program run. For that reason you're far better off learning about and using the layout managers. You can find the layout manager tutorial here: Layout Manager Tutorial, and you can find links to the Swing tutorials and to other Swing resources here: Swing Info.
  • You need to give your JTextField and your JPasswordField a column value via setColumn(int column). This will give the field some width.
  • Myself, I'd use a GridBagLayout to place the JLabel's, JTextFields and JPasswordFields into your JPanel
  • I'd also not get username and password information from a tab, but rather from a modal dialog such as a JOptionPane. This way the user is prevented from accessing your main GUI until he appropriate handles the modal dialog.

For example:

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.Arrays;
import javax.swing.*;

public class UserNamePassword {
   private static final int COLUMN_COUNT = 10;
   private static final int I_GAP = 3;
   private JTextField userNameField = new JTextField();
   private JPasswordField passwordField = new JPasswordField();
   private JPanel mainPanel = new JPanel(new GridBagLayout());

   public UserNamePassword() {
      userNameField.setColumns(COLUMN_COUNT);
      passwordField.setColumns(COLUMN_COUNT);

      GridBagConstraints gbc = getGbc(0, 0, GridBagConstraints.BOTH);
      mainPanel.add(new JLabel("User Name:"), gbc);
      gbc = getGbc(1, 0, GridBagConstraints.HORIZONTAL);
      mainPanel.add(userNameField, gbc);
      gbc = getGbc(0, 1, GridBagConstraints.BOTH);
      mainPanel.add(new JLabel("Password:"), gbc);
      gbc = getGbc(1, 1, GridBagConstraints.HORIZONTAL);
      mainPanel.add(passwordField, gbc);
   }

   public static GridBagConstraints getGbc(int x, int y, int fill) {
      GridBagConstraints gbc = new GridBagConstraints();
      gbc.gridx = x;
      gbc.gridy = y;
      gbc.gridwidth = 1;
      gbc.gridheight = 1;
      gbc.weightx = 1.0;
      gbc.weighty = 1.0;
      gbc.insets = new Insets(I_GAP, I_GAP, I_GAP, I_GAP);
      gbc.fill = fill;

      return gbc;
   }

   public String getUserName() {
      return userNameField.getText();
   }

   public char[] getPassword() {
      return passwordField.getPassword();
   }

   public JComponent getMainPanel() {
      return mainPanel;
   }

   private static void createAndShowGui() {
      UserNamePassword project2 = new UserNamePassword();

      int input = JOptionPane.showConfirmDialog(null, project2.getMainPanel(),
            "Login", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
      if (input == JOptionPane.OK_OPTION) {
         if (project2.getUserName().equals("username") && 
               Arrays.equals(project2.getPassword(), "password".toCharArray())) {
            // you're good
         } else {
            JOptionPane.showMessageDialog(project2.getMainPanel(),
                  "error message", "Error Title", JOptionPane.ERROR_MESSAGE);
         }
      }
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}
Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373