0

this is a small java program im working on for fun.
My issue is, the components in the SearchTab Class will not show! In initFrame(), i have a JTabbedPlane, which shows only when i comment out a lot of components in the SearchTab Class, but will not show when I un-comment a few components from the the SearchTab class.

what am i doing wrong? I dont know any other simplier way to ask this, sorry if i am unclear on the question.

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


public class View{

    public static JFrame        mainFrame;
    public static JTabbedPane   tabs;

    public static void initFrame(){
        mainFrame = new JFrame("Address Book Proggie");
        mainFrame.setSize(600,300);
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.setVisible(true);

        tabs = new JTabbedPane();
        tabs.addTab("Search", new SearchTab());
        tabs.addTab("Insert", new InsertTab("This is the insert tab!"));
        tabs.addTab("Delete", new DeleteTab("This is the tab used to delete items from the database"));
        mainFrame.add(tabs);

    }

//  public static void main(String args[]){
//      initFrame();
//  }
}

class DeleteTab extends JPanel{
    public DeleteTab(String str){
        JLabel deleteLabel = new JLabel(str);
        add(deleteLabel);
    }
}


class InsertTab extends JPanel{
    public InsertTab(String str){
        JLabel insertLabel = new JLabel(str);
        add(insertLabel);
    }
}


class SearchTab extends JPanel{
    public SearchTab(){
        //give this panel a border)
        this.setBorder(BorderFactory.createMatteBorder(10,10,10,10, Color.BLUE));

        //set the group layout
        GroupLayout gl = new GroupLayout(this);
        this.setLayout(gl);
        gl.setAutoCreateGaps(true);

        //create labels..       
        JLabel searchLabel = new JLabel("Search by...");
        JLabel nameLabel = new JLabel("Name: ");
        JLabel addressLabel = new JLabel("Address: ");
        JLabel phoneLabel = new JLabel("Phone Number: ");

        //create text fields...
        JTextField nameField = new JTextField(20);
        JTextField addressField = new JTextField(20);
        JTextField phoneField = new JTextField(20);


        gl.setHorizontalGroup(gl.createSequentialGroup()
            .addGroup(gl.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addComponent(searchLabel))
            .addGroup(gl.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addComponent(nameLabel)
                .addComponent(addressLabel)
                .addComponent(phoneLabel))      

);

        gl.setVerticalGroup(gl.createSequentialGroup()
            .addGroup(gl.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(searchLabel))
            .addGroup(gl.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(nameLabel)
                .addComponent(nameField))
            .addGroup(gl.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(addressLabel)
                .addComponent(addressField))
            .addGroup(gl.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(phoneLabel)
                .addComponent(phoneField))

);
System.out.println("Hello there");      



    }
}
vpatel589
  • 1
  • 1
  • `which shows only when i comment out a lot of components` - share what components – Scary Wombat Sep 02 '16 at 01:32
  • Try `mainframe.pack()` before you set it to visible. – Brick Sep 02 '16 at 01:33
  • @ScaryWombat The components that cause nothing to show are when i comment out all of the Text fields and labels (as well as the group layout code) in the SearchTab Class. The strange thing is, I can leave searchLabel component uncommented as well as all of the group layout code enabling the searchLabel uncommented, and the program will run just fine. but when i try to uncomment anything else, the program is blank. Again, sorry if im unclear. Ill try to explain more if needed. – vpatel589 Sep 02 '16 at 02:00
  • @Brick it didnt work =/ – vpatel589 Sep 02 '16 at 02:02
  • 1
    call `mainFrame.show();` after `mainFrame.add(tabs);` – Md Samiul Alim Sakib Sep 02 '16 at 06:34
  • Like @SakibSami wrote, you should set the mainFrame visible (but using setVisible(true) like you are doing now, since show() method is deprecated) as the last instruction of your initFrame method, so after you added all the tabs to the frame – Ansharja Sep 02 '16 at 06:42
  • If you do it, you obtain an exception caused by your textfields, in particular JTextField [....] is not attached to a horizontal group. This seems to be your problem, if you comment the code where you just add your textfields to the grouplayout, the program runs without exceptions.Unfortunatly, i've never used GroupLayout, so you should check why your textfields are giving this error – Ansharja Sep 02 '16 at 06:46

0 Answers0