rawal
rawal

Reputation: 5

JTabbedPane with 2 tabs is not showing up

I am creating a program in which when a person click on label it will take you to a 2nd panel which will have 2 tabs. The problem is when I click on image label I only see blank window with no tabs and nothing in it.

import java.awt.*;
import static java.awt.Font.BOLD;
import java.awt.event.*;
import java.awt.event.*;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.*;
import javax.swing.Timer;
import javax.swing.border.*;
import javax.swing.event.*;

public class hotels extends JFrame{

    JButton hotel;
    JLabel image;
    JTabbedPane tabbed,tabbed1;
    JPanel panel;
    JPanel panel1;
    Container pane;

    public hotels(){
        panel=new JPanel();
        panel.setBackground(Color.cyan);
        hotel=new JButton();
        hotel.setText("Hotels");

        Font myFont = new Font("Serif", Font.BOLD, 18);

        hotel.setFont(myFont);
        panel.setLayout(null);
        panel.add(hotel);

        hotel.setBounds(50, 80, 100, 40);

        image=new JLabel();

        image.setBounds(50,1,80,80);
        image.setBorder(BorderFactory.createLineBorder(Color.yellow));
        image.setBackground(Color.white);
        image.setIcon(new ImageIcon("2.gif"));
        panel.add(image);

        panel1=new JPanel();

        tabbed=new JTabbedPane();

        tabbed.add( "Round Trip",panel1);
        tabbed.add("One Way",panel1);
        panel1.setVisible(false);

        panel1.revalidate();
        panel.revalidate();
        panel1.repaint();
        panel.repaint();

        pane=getContentPane();
        pane.add(tabbed);
        pane.add(panel1);
        pane.add(panel);

        image.addMouseListener(new MouseAdapter() {

            public void mouseClicked(MouseEvent e){
                if (e.getSource()==image){
                    panel1.show();
                    panel.setVisible(false);
                    panel1.setVisible(true);
                    tabbed.setVisible(true);
                }
            }
        });

    }

    public static void main(String[] args) {

        hotels mw=new hotels();
        mw.setVisible(true);
        mw.setSize(400, 400);
    }

}

Upvotes: 0

Views: 1842

Answers (2)

MadProgrammer
MadProgrammer

Reputation: 347184

There are a series of problems, including the use of null layouts, which will haunt you with no end of problems...

 tabbed.add( "Round Trip",panel1);
 tabbed.add("One Way",panel1);

The above code is adding the same panel to two different tabs, but since a component can only have a single parent, it will automatically remove the "Round Trip" tab

Then...

    pane=getContentPane();
    pane.add(tabbed);
    pane.add(panel1);
    pane.add(panel);

Which removes all the tabs from the tabbed lane (for the same reason above) and, depending on what layout manager your using, may only show panel

You don't need to change the visibility state of your components been managed by the JTabbedPane, as it will take care of all that for you

See How to Use Tabbed Panes for more details

Upvotes: 1

copeg
copeg

Reputation: 8348

 pane.add(tabbed);
 pane.add(panel1);
 pane.add(panel);

The above code attempts to add 3 children to the Content pane's - the Content pane by default has a BorderLayout, which cannot have more than 1 component in it's CENTER position - hence the last Component added is the Component that will be seen. Your options are

  1. Use a CardLayout which allows you to change the Panel shown dynamically
  2. Remove all items from the appropriate Container, then add them and revalidate/repaint

Example of 1 in link above. Example of 2 (in the MouseListener):

pane.removeAll();
pane.add(tabbed);//presuming you want tabbed to show now
pane.revalidate();//or invalidate/validate for <1.7 JRE versions
pane.repaint();

Upvotes: 2

Related Questions