0
public class RoteLearnerTabbedPanePage extends JFrame{
        private static final long serialVersionUID = 1L;
        private JTabbedPane tabbedPane;
        private JPanel panel1;
        private JPanel transientPanel;
        private JPanel nonTransientPanel;
        ReadXml rx=new ReadXml();
        Set<String> primaryKeys = rx.getPrimaryKeysOfGuiAttributes();
        public RoteLearnerTabbedPanePage() throws IOException, SAXException, ErrorCondition
        {
            tabbedPane=new JTabbedPane();
            FlowLayout fl=new FlowLayout();
            Document doc=rx.readXmlFile();
            Hashtable<String,String> mainclasnames=rx.getMainClassNames(doc, doc.getChildNodes());
            Set<XmlClass> guiAttrVals=rx.getGuiAttributeValues(doc, doc.getChildNodes());
            int j=-1;
            Iterator<Entry<String, String>> mainclass_it = mainclasnames.entrySet().iterator();
            while(mainclass_it.hasNext())
            {
                Entry<String, String> classDet = mainclass_it.next();
                String classname = classDet.getValue();
                final Hashtable<JLabel,JTextField> uiParams=new Hashtable<JLabel,JTextField>();
                JPanel buttons=new JPanel();
                JButton buttonLearn = new JButton("Learn");
                panel1= new JPanel();
                transientPanel=new JPanel(new GridBagLayout());
                nonTransientPanel=new JPanel(new GridBagLayout());
                GridBagConstraints constraints = new GridBagConstraints();
                Iterator<XmlClass> guiItr=guiAttrVals.iterator();
                while(guiItr.hasNext())
                {       
                    XmlClass xmlclasss=guiItr.next();
                    if(classname.equals(xmlclasss.getName()))
                    {
                        Set<XmlAttributes> xmlAttrs=xmlclasss.getAttributes();
                        Iterator<XmlAttributes> xmlattrItr=xmlAttrs.iterator();
                        while(xmlattrItr.hasNext())
                        {
                            XmlAttributes xa=xmlattrItr.next(); 
                            String attrname=xa.getName();
                            String isTransient=xa.isTransientAttr();
                            if(isTransient.equals("false"))
                            {
                                constraints.anchor = GridBagConstraints.NORTH;
                                constraints.insets = new Insets(10, 10, 10, 10);
                                JLabel name = new JLabel(attrname);
                                //name.setBounds( 10, 15, 150, 20 );
                                JTextField value = new JTextField(20);
                                //value.setBounds( 10, 15, 150, 20 );   
                                constraints.gridx = 0;
                                constraints.gridy = ++j;
                                if(primaryKeys.contains(name.getText()))
                                {
                                    name.setForeground(Color.RED);
                                }
                                else
                                {
                                    name.setForeground(Color.BLUE);
                                }
                                nonTransientPanel.add(name,constraints);
                                nonTransientPanel.add(value);
                                uiParams.put(name, value);
                                nonTransientPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Non Transient Attributes"));
                                nonTransientPanel.setLayout(fl);
                                panel1.add(nonTransientPanel);
                            }
                            else
                            {
                                constraints.anchor = GridBagConstraints.NORTH;
                                constraints.insets = new Insets(10, 10, 10, 10);
                                JLabel name = new JLabel(attrname);
                                //name.setBounds( 10, 15, 150, 20 );
                                JTextField value = new JTextField(20);
                                //value.setBounds( 10, 35, 150, 20 );
                                constraints.gridx = 0;
                                constraints.gridy = ++j;
                                if(primaryKeys.contains(name.getText()))
                                {
                                    name.setForeground(Color.RED);
                                }
                                else
                                {
                                    name.setForeground(Color.BLUE);
                                }
                                transientPanel.add(name,constraints);
                                transientPanel.add(value);
                                uiParams.put(name, value);
                                      transientPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Transient Attributes"));
                                transientPanel.setLayout(fl);
                                panel1.add(transientPanel); 
                            }
                        }
                        constraints.gridx = 0;
                        constraints.gridy = ++j;
                        constraints.gridwidth = 2;
                        constraints.anchor = GridBagConstraints.SOUTH;

                        panel1.setSize(400, 400);
                        panel1.setLayout(fl);
                        tabbedPane.addTab(classname, panel1);
                    }
                }

                buttons.add(buttonLearn, constraints);
                panel1.add(buttons);
             }

            setTitle( "Rote Learner Form" );
            setSize(500, 500);
            JPanel topPanel = new JPanel();
            topPanel.setLayout(new BorderLayout());
            getContentPane().add( topPanel );
            // Create the tab pages

            topPanel.add( tabbedPane, BorderLayout.CENTER );
        }

this is how I am using JTabbedPane to display two forms.

  1. How do I indent the JLabels so as to make it look like a standard form?

Could anyone answer this? urgently needed.. thanks in advance.. I have used JTabbedPane and an outer Jpanel inside which there are two JPanels TransietnPanel and nonGTransientPanel. This outer panel is added to JTabedPane.

This is the output I am getting

This is the output I am getting

This is what I want to make it look like. This is what I want to make it look like. --------------code after adding GroupLayout--------------------

while(mainclass_it.hasNext())
            {
                Entry<String, String> classDet = mainclass_it.next();
                String classname = classDet.getValue();
                final Hashtable<JLabel,JTextField> uiParams=new Hashtable<JLabel,JTextField>();
                JPanel buttons=new JPanel();
                JButton buttonLearn = new JButton("Learn");

                panel1= new JPanel();
                //GroupLayout layout = new GroupLayout(panel1);

                //panel1.setLayout(layout);
                transientPanel=new JPanel(new GridBagLayout());
                nonTransientPanel=new JPanel(new GridBagLayout());
                GridBagConstraints constraints = new GridBagConstraints();
                Iterator<XmlClass> guiItr=guiAttrVals.iterator();
                while(guiItr.hasNext())
                {       
                    XmlClass xmlclasss=guiItr.next();
                    if(classname.equals(xmlclasss.getName()))
                    {
                        GroupLayout layout1 = new GroupLayout(nonTransientPanel);
                        layout1 = new GroupLayout(transientPanel);
                        Set<XmlAttributes> xmlAttrs=xmlclasss.getAttributes();
                        Iterator<XmlAttributes> xmlattrItr=xmlAttrs.iterator();
                        while(xmlattrItr.hasNext())
                        {
                            XmlAttributes xa=xmlattrItr.next(); 
                            String attrname=xa.getName();
                            String isTransient=xa.isTransientAttr();
                            if(isTransient.equals("false"))
                            {
                                constraints.anchor = GridBagConstraints.NORTH;
                                constraints.insets = new Insets(10, 10, 10, 10);
                                JLabel name = new JLabel(attrname);
                                //name.setBounds( 10, 15, 150, 20 );
                                JTextField value = new JTextField(20);
                                //value.setBounds( 10, 15, 150, 20 );   
                                constraints.gridx = 0;
                                constraints.gridy = ++j;
                                if(primaryKeys.contains(name.getText()))
                                {
                                    name.setForeground(Color.RED);
                                }
                                else
                                {
                                    name.setForeground(Color.BLUE);
                                }

                                nonTransientPanel.setLayout(layout1);
                                nonTransientPanel.add(name,constraints);
                                nonTransientPanel.add(value);
                                uiParams.put(name, value);
                                nonTransientPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Non Transient Attributes"));
                                nonTransientPanel.setLayout(fl);
                                panel1.add(nonTransientPanel);
                            }
                            else
                            {
                                constraints.anchor = GridBagConstraints.NORTH;
                                constraints.insets = new Insets(10, 10, 10, 10);
                                JLabel name = new JLabel(attrname);
                                //name.setBounds( 10, 15, 150, 20 );
                                JTextField value = new JTextField(20);
                                //value.setBounds( 10, 35, 150, 20 );
                                constraints.gridx = 0;
                                constraints.gridy = ++j;
                                if(primaryKeys.contains(name.getText()))
                                {
                                    name.setForeground(Color.RED);
                                }
                                else
                                {
                                    name.setForeground(Color.BLUE);
                                }

                                transientPanel.setLayout(layout1);
                                transientPanel.add(name,constraints);
                                transientPanel.add(value);
                                uiParams.put(name, value);
                                transientPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Transient Attributes"));
                                transientPanel.setLayout(fl);
                                panel1.add(transientPanel); 
                            }
                        }
                        constraints.gridx = 0;
                        constraints.gridy = ++j;
                        constraints.gridwidth = 2;
                        constraints.anchor = GridBagConstraints.SOUTH;

                        panel1.setSize(400, 400);
                        panel1.setLayout(fl);
                        tabbedPane.addTab(classname, panel1);
                    }
                }

                buttons.add(buttonLearn, constraints);
                panel1.add(buttons);

0 Answers0