CardLayout of JTabbedPanes

I have a little program that has a JSplitPane for the main layout of the GUI, but I built it on top of a JTabbedPane layout with nested JTabbedPanes. As you can guess it is the ugliest program ever. But I can't get a CardLayout to work instead of the main JTabbedPane. I was wondering if any one knew a good tutorial or example because I've seen it done.
-thanks

1) Because you are too impatient. Your question in no
more important than anybody elses
2) Because you've already been given the solution.I am extremely SORRY Mr.camickr.
I was in a hurry to finish my module, which cause some troubles in my codes. The solution you've given really works. The problem was lack of my concentration.
Any way THANK YOU very much.
Once more I am begging Pardon.
Tino Simon.
.

Similar Messages

  • Opening Another Form and other Qs

    I am very unfamiliar with Java, and could do with some help with the following :
    How do I call another form to open from an existing form, which is from a different file?
    How do I disable the 'Close', 'Maximize' and 'Minimize' buttons on the top right corner of a form?
    In Visual Basic, I remember learning something like app.path to reference to the path which the file is in. Is there any similar code to do this in Java?

    How do I call another form to open from an existing
    form, which is from a different file?Ther are a couple of ways you can acheive what you want. If you want a whole new window you just create a new Frame
    Frame newframe = new Frame("my new frame");
    newframe.setvisible = true;Otherwise if you just want to cycle through forms in a frame check out the CardLayout and JTabbedPane classes.
    How do I disable the 'Close', 'Maximize' and
    'Minimize' buttons on the top right corner of a form?Look at the API for frame, I know there is a setSizeable (or something to that effect) I think there are options to remove other buttons.
    hope that's a good start.

  • Interconnecting JFrames

    Hi, let me explain my problem with an example.
    --> We run the program, a login screen appears.
    --> We enter login info and another screen appears, let's say student info screen.
    --> And we click the button "See my Grades" in the Student info screen, then we're directed to another frame. The former screen disappears of course.
    The whole process is similar. We see a screen, click a button, and are directed to another screen, and so on. I want to use a central class, let's say GUIManager, to open and close all frames. Every class has actionPerformed methods, if I click on a button, the related frame will show up, but I dont want to create this frame using blabla = new JFrame(...) etc. I want to use GUIManager to do this job. By the way, my GUIManager is a singleton class.
    My idea is to use a function like GUIManager.getInstance().createBlaBlaFrame(); (getInstance method returns the singleton object) in actionPerformed method of each class.
    I'm not a pro OOP programmer.
    Please, I need some ideas, any recommendations are welcome.

    signore wrote:
    "Singleton -- sounds like a very bad idea to me"... why do u think so? In the project, there will be only one instance of this class, managing all the GUI interconnections. Which disadvantages did u think of ?Google: Singleton anti-pattern.
    Then read articles such as this one: [http://accu.org/index.php/journals/337]
    "Hopping JFrames -- sounds like a bad idea to me" Do u mean managing the process with one frame but multiple panels?Yes, most programs you use, be they word processors or games use one main panel and often show different views in this panel with an occasional dialog (separate dialog window). You should strive to emulate the programs you currently use.
    "Why not swap JPanels with a CardLayout or JTabbedPane?" JTabbedPane is not what I want. I need buttons directing to another screens, just like menus. In tabbed mode, we see all the panels together. Also, I looked at http://java.sun.com/docs/books/tutorial/uiswing/layout/card.html tutorial and cardLayout chooses panels from a drop-down menu, just another way of JTabbedPane's job. I dont need that :(I'm not getting you here. CardLayout has nothing to do with "drop-down menus". You may wish to re-read the tutorial, either that or clarify exactly what objections you have to it.

  • Make CardLayout/JTabbedPane the size of the current card/pane?

    Hi all,
    I have a set of panels that a user flips through, which I can implement either as a set of JPanels in a CardLayout or as a JTabbedPane with the tabs hidden -- it's no difference to me.
    If all the panels have been instantiated, the current panel is always the size of the largest panel in the set. So if the current panel is significantly smaller than the largest panel, there's a bunch of unnecessary white space.
    Is there any way to set things up so that the the currently visible panel is always that size that it would normally want to be (i.e. if it weren'tpart of a CardLayout/JTabbedPane)?
    Thanks!
    Sam
    Below is a self-contained example using a JTabbedPane, but the same applies to a JPanel with CardLayout.
    import java.awt.*;
    import javax.swing.*;
    public class SwingTester extends JFrame{
         public SwingTester(){
              JTabbedPane tabbedPane = new JTabbedPane();
              JComponent smallComponent = new JLabel("Test");
              tabbedPane.addTab("Small tab", smallComponent);
              JComponent largeComponent = new JLabel("Test ... Test ... Test");
              largeComponent.setBorder(BorderFactory.createLineBorder(Color.RED, 20));
              tabbedPane.addTab("Large tab", largeComponent);
              getContentPane().add(tabbedPane);
         public static void main(String[] args){
              SwingTester st = new SwingTester();
              st.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              st.pack();
              st.setVisible(true);
    }

    I think that you'll need to do more than revalidate and / or repaint. You'll need to repack the JFrame:
    import java.awt.BorderLayout;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.BorderFactory;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    class BigSmallPanels extends JPanel
        private static final String SMALL_PANEL = "Small Panel";
        private static final String LARGE_PANEL = "Large Panel";
        private JPanel smallPanel;
        private JPanel largePanel;
        BigSmallPanels(JFrame frame)
            setLayout(new BorderLayout(5, 5));
            setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));
            smallPanel = createSmallPanel();
            largePanel = createLargePanel();
            add(largePanel, BorderLayout.CENTER);
            JPanel buttonPanel = new JPanel();
            JButton swapButton = new JButton(SMALL_PANEL);
            buttonPanel.add(swapButton);
            add(buttonPanel, BorderLayout.PAGE_END);
            swapButton.addActionListener(new SwapButtonListener(this, frame));
        private class SwapButtonListener implements ActionListener
            private JPanel panel = null;
            private JFrame frame = null;
            SwapButtonListener(JPanel panel, JFrame frame)
                this.panel = panel;
                this.frame = frame;
            public void actionPerformed(ActionEvent e)
                String command = e.getActionCommand();
                if (command.equals(SMALL_PANEL))
                    ((JButton)e.getSource()).setText(LARGE_PANEL);
                    ((JButton)e.getSource()).setActionCommand(LARGE_PANEL);
                    panel.remove(largePanel);
                    panel.add(smallPanel, BorderLayout.CENTER);
                    frame.pack();
                    //panel.revalidate();
                    //panel.repaint();
                else if (command.equals(LARGE_PANEL))
                    ((JButton)e.getSource()).setText(SMALL_PANEL);
                    ((JButton)e.getSource()).setActionCommand(SMALL_PANEL);
                    panel.remove(smallPanel);
                    panel.add(largePanel, BorderLayout.CENTER);
                    frame.pack();
                    //panel.revalidate();
                    //panel.repaint();
        private JPanel createSmallPanel()
            JPanel p = new JPanel();
            p.add(new JLabel("Small"));
            return p;
        private JPanel createLargePanel()
            JPanel p = new JPanel(new GridLayout(4, 4, 5, 5));
            for (int i = 0; i < 4; i++)
                for (int j = 0; j < 4; j++)
                    JLabel largeLabel = new JLabel("Large");
                    largeLabel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
                    p.add(largeLabel);
            return p;
        private static void createAndShowGUI()
            JFrame frame = new JFrame("BigSmallPanels Application");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add(new BigSmallPanels(frame));
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        public static void main(String[] args)
            javax.swing.SwingUtilities.invokeLater(new Runnable()
                public void run()
                    createAndShowGUI();
    }

  • Jtabbedpane with replacing tab content

    Hello,
    I am developing an applet that should contain a JTabbedPane with 2 tabs.
    The second tab is easy to do because ti contains one Jpanel all the way.
    the first however is an issue, because i am supposed to change its content when the applet is running.
    this means i have 3 JPanels, J1, J2, J3.
    At tge beginning the applet contains J1 in the first tab.
    and J1 contains a button. when i click that button the applet should replace J1 with J2.
    the problem is i haven't managed to find a solution yet :(
    I have tried with setvisible(false) and validate(). It won't work. I also tried to add the J2 panel over J1, but encountered no succes.
    anybody has any idea ?
    Message was edited by:
    asrfel

    If you want to change back and forth repeatedly then wrap J1/2/3 in a JPanel with a CardLayout.
    If you can discard one when it's done with, use the remove() and insertTab() methods of JTabbedPane.

  • Simple JTabbedPane question

    This should be simple:
    I use a JTabbedPane in my application. On one tab I have everything correctly laid out (tables, lables, ...). The other
    tab is currently empty.
    I would now like to get a JPanel from a different application to this second tab.
    This should be easy by importing necessary class files and then pointing to the JPanel.
    Like this:
    myTabbedPane.addTab("ABC", myPanel);where "myPanel" is located in a different application in a different directory on my PC.
    How to get it done?
    I can also post the whole code here if necessary.
    OK!
    AUlo

    Not actually.
    The case:
    I have a project where there are different inner-frames in one JFrame. Each innerframe is a separate appliacation, doing different things. I first created two separate applications which now need to be united. I therefore modified the second one to have a CardLayout, JTabbedPane. The first Tab of the pane includes the original second application, but the second Tab is empty.
    I would now like to get the first app displayed on the second tab. And the first app is located in a physically different place in my computer than the second one.
    So I import the classes to the first app to the second one, but how do I refer to a specific JPane when I would like to include it on the JTabbedPane?

  • JTabbedPane cause second paint

    I have a problem with JTabbedPane that has me stumped. When a TabbedPane is displayed, it causes it's parent component to repaint itself. This appears to happen because the TabbedPane wants to paint the contents of it's visible tab. If I don't add any tabs to the TabbedPane, the problem goes away. I have given a simple example to demonstrate.import java.awt.*;
    import javax.swing.*;
    class TabTest
      public static void main(String[] args)
        JFrame frame = new JFrame("TabTest");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JTabbedPane tabbedPane = new JTabbedPane();
        tabbedPane.addTab("First Tab", new JPanel());
        tabbedPane.addTab("Second Tab", new JPanel());
        JPanel contentPane = new JPanel(new BorderLayout()){
          protected void paintComponent(Graphics g)
            try
              // sleep so we can watch the painting issue occur
              System.out.println("contentPane.paintComponent()");
              Thread.currentThread().sleep(1000);
            catch(Exception e){}
            super.paintComponent(g);
        contentPane.add(tabbedPane, BorderLayout.CENTER);
        frame.setContentPane(contentPane);
        frame.setBounds(300,300,300,300);
        frame.setVisible(true);
    }My only solution so far is to use a CardLayout and simulate a TabbedPane like interface. When the tabs have some components on them, you can see a flicker, which is my main problem with this.

    v1.4.2
    Yes that is what I mean. Only a TabbedPane requires a second paint of the frame. When the tabbedPane has some components on it, requiring more painting, you will notice a flicker when displaying the window. The flicker is gone if your remove all tabs from the tabbedPane. If you get rid of the tabbedPane and put a regular JPanel there, only one paint is performed. Or if you put a JPanel with a cardlayout only one paint is performed.
    In my application, it is actaully a modal JDialog with a borderlayout and the tabbedPane is put in the WEST region. If I do the little sleep(), I can see the borderlayouts CENTER, BOTTOM and NORTH regions painted and then the screen is wipped and finally get the WEST region...the tabbedPane. This requires a second paintComponent() call and several invalidates. This causes an annoying flicker which is only removed by removing all tabs from the tabbedPane or by using a CardLayout instead.

  • JTabbedPane layout problem

    I have a JTabbedPane as shown below:
    |                          |
    | ----- -----                         |
    |  |   | |   |                        |
    | ----------------------------------- |
    | | --------------- --------------- | |
    | | --------------- --------------- | |
    | | --------------- --------------- | |
    | | --------------- --------------- | |
    | | --------------- --------------- | |
    | | --------------- --------------- | |
    | |_________________________________| |
    |                                     |
    --------------------------------------|As you can see, each tab has two columns and with 5 rows each column. Each row is a horizontal javax.swing.Box of components.
    My problem sometimes I only have, for example, 2 rows in the second column. They become stretched to fill the remaining space and I don't like that.
    How do I resolve this so it doesn't do that?
    Thnks for any help!
    Liam

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.Random;
    public class Test extends JFrame {
      public Test() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container content = getContentPane();
        JTabbedPane jtp = new JTabbedPane();
        content.add(jtp, BorderLayout.CENTER);
        Random r = new Random();
        for (int i=0; i<2; i++) {
          JPanel tab = new JPanel(new GridLayout(6,2));
          jtp.add("Tab "+i, tab);
          for (int j=0; j<6; j++) {
            tab.add(new JLabel("Label-"+i+","+j+":", JLabel.RIGHT));
            if (j==0 || r.nextBoolean()) tab.add(new JTextField("Text-"+i+","+j));
            else tab.add(Box.createHorizontalBox());
        setSize(300, 300);
        setVisible(true);
      public static void main(String args[]) { new Test(); }
    class MyCardLayout extends CardLayout {
      public void show();
    }

  • JTabbedPane and focus

    I use a jTabbedPane in order to make un chain progression in data collections.
    I've searched in the functions and variables to know if it is possible to block
    the user on the last TabbedPane if he start using the last TabbedPane.
    I've placed previous and next buttons on each TabbedPane and i can disable these buttons,
    but i can't disable the tabbedPane above.
    I don't wish to use CardLayout for this (event if it is better for that), so if someone know how to
    solve this problem, thanks in advance.

    You can use JTabbedPane.setEnabledAt to set whether a particular tab is enabled or not. Just make a call to this with the code that enables/disables your previous and next buttons.
    Hope this helps.

  • How to hide/show a component of the JTabbedpane?

    Hi,
    I am working on a JTabbedPane, which hosts many jtables' panes. At some condition, some of panes should be hidden/shown under the user's action. How can I implement this? There is no such mothed to hide a pane instead of remove it.
    Thanks.

    add the panel to a holding panel set as a cardlayout
    (one side is the panel you 'may' want hidden, the other side a blank panel)
    add the holding panel to the tabbedPane
    if the panel is to be hidden (user action), show the blank panel

  • LostFocus not executed when clicking JTabbedPane

    Hi, my problem:
    I have many JTextFields on a JPanel on a JTabbedPane. For each JTextField a FocusListener is implemented. When I click from one JTextfield to the next one, the focusLost-method is called, everything works fine. But when I click on the JTabbedPane for the next pane, the focusLost-method is ignored.
    Why does this happen and how can I solve this?

    the problem is each tab panel is a separate focus cycle.
    do you need the tabs?
    perhaps the easiest way is to use a cardlayout (which is what the tabbedpane uses)
    then add a next/previous button to navigate the panels - and in your 'next' button
    code you include a check on the textfield - if OK, proceed to the next panel

  • Adding KeyEvents to cardLayout tabs

    I am upgrading an app that used a cardLayout to create tab functionality. I am trying to add a Tab-Key or Arrow-key functionality to this cardLayout that would allow for me to shift thru the tabs, the tabs are drawn tabs icons, I use a mouseListener to show/select the tab. When I add a keyListener to the class it does not work.
         addKeyListener(new KeyAdapter()
              public void keyPressed(KeyEvent ke)
                   int key = ke.getID();
                   if(ke.getKeyCode() == KeyEvent.VK_TAB)
                        System.out.println("Entered Tab Key Adapter");
                        setSelected((selected+1) % nCards,false);
                        next();
              public void keyReleased(KeyEvent ke)
              public void keyTyped(KeyEvent e)
    This is the listener functions. Also the class is extended from the panel class.
    Does anyone know this solution?
    Thanks

    I would say, for this situation, it would be better to "upgrade" the app to use a JTabbedPane instead of a CardLayout. All the functionality that you'd need is there.

  • Top Card of CardLayout

    Hi, I have a JPanel 'mainPanel' with CardLayout. I add several JPanel (p1, p2, ...) to the 'mainPanel'. During runtime of the application I switch between the card using the show(...)-Methode of CardLayout.
    Is there any possibility to ask the CardLayout witch card/panel is on top (other Components such as JTabbedPane can be asked that - getSelectedComponent()). I expect to find a methode like 'getActualShownComponent()' or something like that.
    Can anybody help ?
    Thanks
    Frank

    Hey,
    U hv methods in JPanel which returns boolean( True or False), isVisible().
    Ex:
    if(pl.isVisible()) {
    //do ur code here ....
    else if(p2.isVisible()) {
    //do ur code here ....

  • Using CardLayout and Null Layout

    I used search on the forums and found one person that asked the exact question I'm needing answered. But, he didn't get any responses either.
    I have a JFrame(myJFrame) which has a JPanel(myJpanel). myJpanel uses a CardLayout. I then have another class that extends JPanel(TestJpanel). TestJpanel uses a null layout. I am not able to get the TestJpanel to show correctly with the show(). method. However, if I change the layout of TestJpanel to anything else, presto it works. Has anyone uses null panels as the panels for a cardlayout panel?

    if you do setLayout(null) the component will leave all layout up to you. You can then use setSize(), setLocation(), and setBounds() on all children to position them however you'd like

  • CardLayout does not work

    I'm building a dynamic UI which has "x" number of labels and "x" no of fields. I'm able build the UI.
    When I make a selection in the ComboBox the UI(labels and fields change). For this I'm using a cardlayout.
    It does not seem to work.
    AttributesPanel builds the UI(Labels, Fields)
    import java.util.*;
    import java.util.List;
    import java.awt.event.*;
    import java.awt.*;
    import javax.swing.*;
    public class AttributesPanel extends JPanel {
        private JTextField[] fields;
        private List attributes;
        public AttributesPanel(String templateName) throws Exception{
            JSeparator sep = new JSeparator();
            JButton okButton = new JButton("OK");
            JButton cancelButton = new JButton("Cancel");
            com.insignia.profileManagement.dataLoader.NewProfileMetaDataLoader loader = new com.insignia.profileManagement.dataLoader.NewProfileMetaDataLoader();
            attributes = loader.BuildAttributes(templateName);
            for(int i = 0; i< attributes.size(); i ++) {
              String attributename = (String)attributes.get(i);
              System.out.println("Names of the attribute: " +attributename);
            fields = new JTextField[attributes.size()];
            JLabel[] labels = new JLabel[attributes.size()];
            for(int i = 0; i< attributes.size(); i++) {
                labels[i] = new JLabel((String)attributes.get(i));
                fields[i] = new JTextField(15);
            GridBagLayout gb1 = new GridBagLayout();
            GridBagConstraints gbc = new GridBagConstraints();
            setLayout(gb1);
            gbc.anchor = GridBagConstraints.NORTHWEST;
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            add(new JLabel("Assign Profile Template"), gbc);
            gbc.anchor = GridBagConstraints.NORTH;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.insets = new Insets(0,0,10,0);
            add(sep, gbc);
            gbc.anchor = GridBagConstraints.WEST;
            gbc.insets = new Insets(0,0,0,0);
            for(int i = 0;i < attributes.size(); i ++){
                gbc.gridwidth = 1;
                add(labels[i] ,gbc);
                add(Box.createHorizontalStrut(10));
                gbc.gridwidth = GridBagConstraints.REMAINDER;
                add(fields,gbc);
    JPanel buttonPanel = new JPanel();
    buttonPanel.add(okButton);
    buttonPanel.add(cancelButton);
    gbc.anchor = GridBagConstraints.SOUTH;
    gbc.insets = new Insets(15,0,0,0);
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridwidth = 7;
    add(buttonPanel,gbc);
    2nd class which calls AttributesPanel
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    import java.lang.*;
    import java.util.*;
    import java.util.List;
    public class ProfileUI extends JPanel implements ActionListener{
    private JPanel templatePanel;
    JComboBox comboBox = new JComboBox();
    private CardLayout cardLayout;
    com.insignia.profileManagement.dataLoader.NewProfileMetaDataLoader loader = new com.insignia.profileManagement.dataLoader.NewProfileMetaDataLoader();
    public ProfileUI() throws Exception {
    templatePanel = new JPanel();
    List templates = loader.BuildTemplates();
    comboBox.addActionListener(this);
    for(int i = 0; i< templates.size(); i ++) {
    comboBox.addItem(templates.get(i));
    //attributes = loader.BuildAttributes((String)templates.get(i));
    templatePanel.add(comboBox);
    List attributes = loader.BuildAttributes("Email Template");
    // //creating no of card layouts.
    cardLayout = new CardLayout();
    JPanel mainCardPanel = new JPanel(cardLayout);
    AttributesPanel[] cardPanels = new AttributesPanel[attributes.size()];
    for (int i = 0; i < attributes.size(); i++) {
    mainCardPanel.add(cardPanels[i], i);
    // createCardPanel(cardPanels[i], (String)comboBox.getItemAt(i));
    cardLayout.show(cardPanels[i], "" + i);
    templatePanel.add(mainCardPanel);
    //updateLabel(0);
    cardLayout.show(cardPanels[0], "" + 0);
    public void actionPerformed(ActionEvent e){
    if(e.getSource() == comboBox) {
    public static void main (String args []) throws Exception {
    JFrame frame = new JFrame("ProfileUI");
    JComponent newContentPane= new ProfileUI();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // use this or a
    newContentPane.setOpaque(true); //content panes must be opaque
    frame.setContentPane(newContentPane);
    frame.setSize(400,390);
    frame.setVisible(true);
    //frame.setResizable(false);
    thnx

    Personally, I've had numerous issues with CardLayout -- I'd suggest you try CardPanel instead:
    http://java.sun.com/products/jfc/tsc/articles/cardpanel/
    - Fromage

Maybe you are looking for