2

I want to create a JTabbedPane for separate items, with JPanel and GridBaglayout, but every time, the second tab of the JTabbedPane bug my text field objects...

that is my animal register class, the extended class and the main class the problem is in the text field of the second table.

the Code, only the problem...

Code:

public class Cad_Animals extends Register_Screen{
    JTextField number = new JTextField(10);
    JTextField name = new JTextField(30);
    JTabbedPane pane1 = new JTabbedPane(1);
    JTabbedPane pane2 = new JTabbedPane(1);

    JTextField race = new JTextField(10);
    JTextField proprietario = new JTextField(10);
    JPanel jl1 = new JPanel();
    JPanel jl2 = new JPanel();

    JTextField father = new JTextField(10);
    JTextField mother = new JTextField(10);

    public Cad_Animals(){
        super("Animal Register");
        jl1.setLayout(new GridBagLayout());
        jl2.setLayout(new GridBagLayout());
        addComponent(1, 1, 1, 1, number,jl1);
        addComponent(2, 1, 1, 1, name,jl2);

        addComponent(3, 1, 1, 1, proprietario,jl1);
        addComponent(2, 1, 1, 1, race,jl2);
        addComponent(6, 1, 1, 1, father,jl1);
        addComponent(7, 1, 1, 1, mother,jl1);


        //pane1.addTab("table1", jl2);
        //pane2.addTab("table2", jl1);

        //addComponent(8, 1, 6, 6, pane1,1);
       // addComponent(8, 2, 3, 3, pane2,1);

        //painels("new2",jl2);
        //painels("new2", jl1);

        pack();
    } 
}

.

    public class Register_Screen extends JFrame {

    JTabbedPane la = new JTabbedPane();

    public Register_Screen(String titulo) {
        super(titulo);
        getContentPane().setLayout(new BorderLayout());     

        setVisible(true);
        pack();

    }

        public void addComponent(int line, int column, int height, int width, JComponent component,JComponent compnum) {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridy = line;
        gbc.gridx = column;
        gbc.gridheight = 1; //1
        gbc.gridwidth = 1; //1
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(1, 5, 3, 5);
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;



        gbc.gridx++;
        gbc.gridheight = height;
        gbc.gridwidth = width;
        //jpCampos.add(component, gbc);
        compnum.add(component, gbc);

        getContentPane().add(la);


        la.addTab("West",compnum);
        //jtpTabs.add("asdsa",jpCampos);            
    }
}

.

  public class MainClass {
      public static void main(String args[]) throws InterruptedException{
     try {
            UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");

        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Não foi possivel Carregar NimbusLookandFeel");
        }
        Cad_Animals cad = new Cad_Animals();
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720

1 Answers1

2

A guess:

You don't set the weightx nor the weighty of your GridBagConstraint, and so your GridBagConstraints will have a default value of 0.0 for these guys which will bunch up all your components into the center. Consider giving your weightx and weighty constraint fields a value of 1.0.

i.e.,

gbc.gridy = line;
gbc.gridx = column;
gbc.gridheight = 1;
gbc.gridwidth = 1;
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets(1, 5, 3, 5);

// ***** add these two lines
gbc.weightx = 1.0;
gbc.weighty = 1.0;

If this answer doesn't help, then I stand by the recommendation that I made in a comment to your original question, that you take some time to create and post a minimal example program.

Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • I do this, but that align just the comments, and not the textfield. – Samuel Ronha Sep 01 '14 at 19:29
  • @SamuelRonha: then it may be time to create and post your [mcve](http://stackoverflow.com/help/mcve). – Hovercraft Full Of Eels Sep 01 '14 at 19:32
  • @SamuelRonha: also you're not assigning your `gbc.fill` property. Consider giving your JTextField's GridBagConstraints object a fill property of `GridBagConstraints.HORIZONTAL` – Hovercraft Full Of Eels Sep 01 '14 at 19:33
  • @SamuelRonha: OK, but don't forget to first try setting the fill property. – Hovercraft Full Of Eels Sep 01 '14 at 19:36
  • 1
    @SamuelRonha: maybe someone will have time to go through your entire project, but not me as I have work/family/life obligations to attend to. You do understand that we're all volunteers, right? If you need further help from me, you should strongly put in more effort to create and post your minimal program. Please check out the link as it will help give you an understanding on what we'd need. – Hovercraft Full Of Eels Sep 01 '14 at 20:27
  • 1
    @SamuelRonha what Hovercraft was trying to say is, create a new project, take out all unnecesary code from your original project, it should demonstrate the problem with the less code you can post *here* on StackOverflow, avoid external links with your full project – Frakcool Sep 01 '14 at 20:28
  • 1
    and btw if @HovercraftFullOfEels's answer isn't the one that fits or solves your problem don't accept it yet, 'cause it won't figure out on the questions list and no more people will see this question so you won't have an answer. – Frakcool Sep 01 '14 at 20:29
  • Indeed, please listen to both of @Frakcool's recommendations. – Hovercraft Full Of Eels Sep 01 '14 at 20:31
  • @SamuelRonha: Thanks for the code! Now tell us what your GUI looks like vs. what you want it to look like, or better, show us. – Hovercraft Full Of Eels Sep 01 '14 at 21:32
  • @SamuelRonha: please clarify for those of us who put in the time and effort to study your code and try to understand your problem, what was wrong. Again, what were you trying to achieve??? – Hovercraft Full Of Eels Sep 01 '14 at 21:52
  • one of the tab are centering my textfields, the second tab, only the first was left aligned and i wanted both with the same alignment for the project. is just this. – Samuel Ronha Sep 01 '14 at 21:58
  • I just needed to change the proprieties of the gbc gridx to 0 for the components of the second tab.. – Samuel Ronha Sep 01 '14 at 22:10