code_buoy22
code_buoy22

Reputation: 53

adding tabs in java (swing)

i was given a project to make a java program named doctors care which has login panel( username and password) and a panel which has 3 tabs. the problem is i could add only one tab and when i tried to add another tab by duplicating the objects( like label2, panel2 etc) it does not appear in the executed JAR file. i have posted the code below.

public void actionPerformed(ActionEvent ae)

   {String value1 = text1.getText();
    String value2 = text2.getText();
    if(value1.equals("admin") && value2.equals("admin123456")) {
        NextPage page=new NextPage();
        page.setVisible(true);

        JTabbedPane tabbedPane1 = new JTabbedPane();
        ImageIcon icon1 = createImageIcon("images/middle.gif");
        JComponent panel1 = makeTextPanel("panel#1");
        tabbedPane1.addTab("Booking", icon1, panel1, "For booking appointments");
        JLabel label = new JLabel("Welcome "+value1);
        page.getContentPane().add(label);
        page.getContentPane().add(tabbedPane1);
        //page.add(icon);
        page.getContentPane().add(panel1);

  JTabbedPane tabbedPane2 = new JTabbedPane();
    ImageIcon icon2 = createImageIcon("images/middle.gif");
    JComponent panel2 = makeTextPanel("panel#2");
    tabbedPane2.addTab("Doctors", icon2, panel2, "For choosing appointments");
    JLabel label2 = new JLabel("Welcome "+value2);
    page.getContentPane().add(label2);
    page.getContentPane().add(tabbedPane2);
    //page.add(icon2);
    page.getContentPane().add(panel2);}

Upvotes: 3

Views: 11619

Answers (2)

Oo.oO
Oo.oO

Reputation: 13375

I, generally, always consider GUI builders as best tools for this task.

enter image description here

It is not true that code is not portable

  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  private void initComponents() {

    jTabbedPane = new javax.swing.JTabbedPane();
    jPanelFirst = new javax.swing.JPanel();
    jPanelSecond = new javax.swing.JPanel();
    jPanelThird = new javax.swing.JPanel();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jPanelFirst.setLayout(null);
    jTabbedPane.addTab("tab1", jPanelFirst);

    jPanelSecond.setLayout(null);
    jTabbedPane.addTab("tab2", jPanelSecond);

    jPanelThird.setLayout(null);
    jTabbedPane.addTab("tab3", jPanelThird);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addComponent(jTabbedPane)
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addComponent(jTabbedPane)
    );

    pack();
  }// </editor-fold>

Of course, if you load it inside Eclipse, and do some "manual mess", you will probably complain that NetBeans fails to load it. That's true.

Personally, I always ask myself. Do I want to devote my time to writing all the code manually or would I rather focus on business logic and do all the GUI building inside some tools that help to do it quickly?

There are always trade-offs.

Upvotes: 4

Kyle
Kyle

Reputation: 793

Here's a sample from my program I wrote awhile ago that uses tabbedPanes, if I had a larger picture I could help a bit more. Instead just use this as a possible resources to see if order might matter for adding in the pane then the tabs.

Note that contentPane is the generated JPanel in WindowsBuilder.

tabbedPane = new JTabbedPane(JTabbedPane.LEFT);
    tabbedPane.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
    tabbedPane.setBackground(Color.DARK_GRAY);
    tabbedPane.setForeground(Color.white);
    tabbedPane.setBounds(10, 11, 1900, 1058 - taskBarSize);
    tabbedPane.setFont(new Font("Arial Black", Font.BOLD, 14));
    contentPane.add(tabbedPane);

    te = new TournamentEnlisting();
    tabbedPane.add("<html><body><table width='250'>Tournament Enlisting</table></body></html>", te);

    su = new StreamUpdater();
    tabbedPane.addTab("<html><body><table width='250'>Stream Updating</table></body></html>", su);

    sc = new StreamCapture();
    tabbedPane.addTab("<html><body><table width='250'>Stream Capture</table></body></html>", sc);

    thumb = new ThumbnailEditor();
    tabbedPane.addTab("<html><body><table width='250'>Thumbnail Editor</table></body></html>", thumb);

Upvotes: 0

Related Questions