JTabbedPane, and JPanel.

Hi im going thrue swing tutorials here on sun. And wonder a bit about hierarchy. I use JTabbedPane and every class have there own JPanel that i add to the extended JPanel. I show below with example.
In Main class
  public JFrame mstFrame() {
        JFrame frame = new JFrame("Main Frame");
        JTabbedPane mstPane = new JTabbedPane();
        mstPane.addTab("Start", new StartPage());
        frame.add(mstPane);
        frame.setVisible(true);
        return frame;
    }In StartPage Class
public class StartPage extends JPanel {
    JPanel createProjectPanel = new JPanel(new GridBagLayout());
     GridBagConstraints c = new GridBagConstraints();
     JScrollPane createCustomerPane = new JScrollPane();
     c.gridy = 1; // second row
     c.gridx = 0; // first cell
     createProjectPanel.add(createCustomerPane ,c);
add(createProjectPanel);Does this destory the hierarchy, or is it okey to do it this way?

It makes sense to have each tab represented by a panel, so I don't reallly understand the question.
public class StartPage extends JPanel
    JPanel createProjectPanel = new JPanel(new GridBagLayout());
    add( createProjectPanel );However, it is unecessary to create a second panel since your class already extends JPanel. Just add the scrollPane directly to the class.
setLayout( new GridBagLayout() ):
JScrollPane createCustomerPane = new JScrollPane();
add(createCustomerPane, c);

Similar Messages

  • What's difference between JPanel.remove(); and JPanel = null

    nice day,
    how can remove JPanel (including its JComponents), safe way, can you explain the difference between JPanel.remove () and JPanel = null,
    1/ because if JPanel does not contain any static JComponents
    2/ or any reference to static objects,
    then works as well as JPanel.remove or JPanel = null,
    or what and why preferred to avoid some action (to avoid to remove or to avoid to null)

    mKorbel wrote:
    nice day,
    how can remove JPanel (including its JComponents), safe way, can you explain the difference between JPanel.remove () and JPanel = null, Remove the JPanel from the container it was part of and make sure you do not keep any references to it from your own classes. Don't make it any more difficult than it has to be.

  • JTabbedPane and InsertTab method

    Hi at all!!!Sorry for my bad english!!
    For a university project I want to implement a copy of notepad. Only I want to manage a more document and I think that it is possible through the JTabbedPane and it works.
    But I don't know how I can implement a button that add/remove a tab during the runtime.
    Is there someone that can help me???
    thank u
    giuliano

    Your main window should have [AddNewTab] and [RemoveTab] buttons.
    In their event handler, launch a dialog and accept required user inputs.
    After you dispose the dialog, call JTabbedPane#add(), addTab() or remove().

  • JTabbedPane and removeTabAt() method

    Hello,
    I use in my application a JTabbedPane and i want allow the user to remove a tab of this JTabbedPane. When the JTabbedPane change i need to perform some traitement.
    So i use a ChangeListener to be informed of all changes of my JTabbedPane.
    But, when i removed a tab which is not in last or first position i'm not informed by the change.
    Thanks for your help.
    Guiguch

    I had the same problem. I fixed it by overriding the remove() method to fire a StateChanged event:
    tabbedPane = new JTabbedPane()
         public void remove(int tab)
              super.remove( tab );
              int after = getSelectedIndex();
              //  The selected tab remained the same after this remove, consider
              //  it a state changed
              if (after == tab)
                   fireStateChanged();
    };

  • Resizing JFrames and JPanels !!!

    Hi Experts,
    I had one JFrame in which there are three objects, these objects are of those classes which extends JPanel. So, in short in one JFrame there are three JPanels.
    My all JPanels using GridBagLayout and JFrame also using same. When I resize my JFrame ,it also resize my all objects.
    My Problem is how should i allow user to resize JPanels in JFrame also?? and if user is resizing one JPanel than other should adjust according to new size ...
    Plese guide me, how should i do this ...
    Thanknig Java Community,
    Dhwanit Shah

    Hey there, thanx for your kind intereset.
    Here is sample code .
    In which there is JFrame, JPanel and in JPanel ther is one JButton.Jpanel is added to JFrame.
    I want to resize JPanel within JFrame,I am able to do resize JFrame and JPanel sets accroding to it.
    import java.awt.*;
    import javax.swing.*;
    import com.borland.jbcl.layout.*;
    public class FramePanel extends JFrame {
    JPanel contentPane;
    GridBagLayout gridBagLayout1 = new GridBagLayout();
    public FramePanel() {
    try {
    jbInit();
    catch(Exception e) {
    e.printStackTrace();
    public static void main(String[] args) {
    FramePanel framePanel = new FramePanel();
    private void jbInit() throws Exception {
    contentPane = (JPanel) this.getContentPane();
    contentPane.setLayout(gridBagLayout1);
    this.setSize(new Dimension(296, 284));
    this.setTitle("Frame and Panel Together");
    MyPanel myPanel = new MyPanel();
    this.getContentPane().add(myPanel);
    this.setVisible(true);
    class MyPanel extends JPanel {
    public MyPanel() {
    this.setSize(200,200);
    this.setLayout(new FlowLayout());
    this.setBackground(Color.black);
    this.setVisible(true);
    this.add(new JButton("Dhwanit Shah"));
    I think i might explained my problem
    Dhwanit Shah
    [email protected]

  • JTabbedPane and focus problem.

    Hello. I currently have a JTabbedPane which contains the various panels that make up different tabs. These tabs are extensions of panels, and are seperate classes imported into the class which contains my JTabbedPane and then added. The frame which conatins my JTabbedPane also has a button, which when pressed I wish it to look at the tab which is currently active, and call a method from the class which this tab refers to. The problem is that I don't know how to get my button to determine which tab is currently active. Any help will be gratefully recieved.

    Use getSelectedComponent() or getSelectedIndex() of your JTabbedPane object.

  • JTabbedPane and one Button for all Tabs

    Hello everybody,
    im new here an would say HELLOOO to everyone
    So ive got an issue.
    I am creating in "class A"  tabs for a JTabbedPane. In "class B" i create the JTabbedPane and creat instances of class A and add them to the TabbedPane.
    in Class B i also create a button, for save actions. When i click the button, a method "save()" will be called and read from every tab the Strings out of textfields.
    My problem now is, that it dont work, i call the save method with the instance from the tab, but it dont getText() from the textfield.
    For example:
    //create an instance tab, parameter is the save file
    Tab myTab = new Tab(file);
         //other code
    (Tab)myTab.save(); //here i eclipse cast to Tab automatically
    the save-Method in class A (Tab):
    save() {
    sysout(texfield1.getText)
    BTW how can i highlight code in this forum?

    You can add syntax highlighting by going into the advanced editor, that will add a button for it. Unfortunately there are no quick code tags :/
    After you find the button post your ACTUAL code. not just some random snippet which looks like your code. What you posted doesn't even compile.

  • Inserting a Gui program using JFrames and JPanel

    I'm trying to insert a chat program into a game that I've created! The chat program is using JFrames and JPanels. I want to insert this into a GridLayout and Panel. How can I go about doing this?

    whatever is in the frame's contentPane now, you add to a separate JPanel.
    you also add your chat stuff to the separate panel
    the separate panel is added to the frame as the content pane

  • JTabbedPane and JScrollPane problem

    Hi all,
    I'm trying to make working JScrollPane into JTabbedPane. I have been trying several times to make it but with no acceptable effects :( I attached below the simple code to show what I'm interested in.
    PANEL1 has its dimension. I'd like to make that the JScrollPane will be active and enabled to roll when user change JFrame size to less than PANEL1 on pane1.
    Please help!
    Thanks for everyone,
    Greetings,
    import java.lang.*;
    import java.util.*;
    import java.awt.*;
    import java.awt.geom.*;
    import javax.swing.*;
    public class Sample     {
    static JFrame fr;
    static JPanel pane1, pane2, pane3;
    static JLabel AAA, BBB, CCC;
    static JPanel PANEL1;
    static JTabbedPane panel;
    public static void main(String args[])     {
              fr = new JFrame("Sample");
              pane1 = new JPanel();
              pane1.setLayout(null);
              pane2 = new JPanel();
              pane2.setLayout(null);
              pane3 = new JPanel();
              pane3.setLayout(null);
              panel = new JTabbedPane();
              Font myfont1 = new Font("SansSerif",0,20);
              Font myfont2 = new Font("SansSerif",0,15);
              Font myfont3 = new Font("SensSerif",1,15);
              AAA = new JLabel("AAA");
              BBB = new JLabel("BBB");
              CCC = new JLabel("CCC");
              AAA.setFont(myfont2);
              BBB.setFont(myfont2);
              CCC.setFont(myfont2);
              AAA.setBounds(20,20,300,20);
              BBB.setBounds(20,50,300,20);
              CCC.setBounds(20,80,300,20);
              PANEL1 = new JPanel();
              PANEL1.setLayout(null);
              PANEL1.setBackground(java.awt.Color.CYAN);
              PANEL1.setBounds(20,15,250,300);
              PANEL1.add(AAA);
              PANEL1.add(BBB);
              PANEL1.add(CCC);
              pane1.add(PANEL1);
              panel.insertTab("A", null, pane1, null, 0);
              panel.setSelectedIndex(0);
              panel.insertTab("B", null, pane2, null, 1);
              panel.setSelectedIndex(0);
              panel.insertTab("C", null, pane3, null, 2);
              panel.setSelectedIndex(0);
              panel.setBounds(0,0,550,450);
              fr.setLayout(null);
              //fr.setResizable(false);
              fr.setBackground(java.awt.Color.CYAN);
              fr.setForeground(java.awt.Color.CYAN);
              fr.add(panel);
              fr.pack();
              fr.setSize(550, 450);
              fr.setLocationRelativeTo(null);
              fr.setVisible(true);
    }

    Unfortunately no :( Besides, I can't understand at
    all this line:
    JTabbedPane (panel)TabbedPane = ... new
    w JTabbedPane(...).add/insertTab( ... new
    JScrollPane(new JPanel(...)) ... )It's to big cut-off like for me. Can you insert it
    into my Sample?
    Regards and thanks,It's only my "dev speak". Put a ";" and a var name (, ...) whereever needed, and remove some"()".
    (Sorry, normally I don't help dev beginners on so more complex problems, with just an easy answer.)

  • JTabbedPane and generated code

    Hi all -
    I am teaching myself Java etc using the free copy of JSE provided so kindly by Sun, and have run into a speedbump that I can get around, but doesn't make me happy at all. If I create a jTabbedPane object in the GUI and add objects to it, they are always called "Tab1", "Tab2", etc etc. No matter what I do, I can't change these default names to what I want. I have tried editing the source in Notepad to no avail.
    If I change the Pre-Init code property to add tabbed compnents manually, I can have names I actually want to use - but I have to remove the components from the tabbed pane in the GUI editor. If this was the only way to do it, I would immediately throw this product onto the great steaming pile of almost-useful refuse. However, I can't believe that something that fundamental would have been overlooked by the developers.
    There has to be a simple solution. Please help.

    Ref: http://www.netbeans.org/kb/faqs/gui-editor.html#FaqFormSettingTabNames
    How do I set tab names in a JTabbedPane?
    Select the component representing the tab (typically a JPanel) and go to the Layout properties tab in Inspector. You can set tab title here, as well as tooltip and icon. For example, step through the following procedure.
    1. Create a new JFrame.
    2. Drop a JTabbedPane component onto the JFrame in the Form designer window.
    3. Drop a JPanel component onto the JTabbedPane. A tab will appear.
    4. In Inspector select the JPanel, then click the Layout tab for the JPanel.
    5. The Tab Icon, Tab Title, and Tab Tooltip properties will be exposed, and can be modified.

  • JScrollPane for pane and JTabbedPane and Graphics

    Sort of have two topics with same problem.
    I have a set of Panels. I get a stream of jpegs which I have to decode to the fly.
    There are seperate video feeds in the stream and I have to read jpeg data to see which image goes where.
    I was trying to see if I could have a larger image in smaller area. this is why I tried to draw to panel inside a scrollpane.
    One: I tried to put a JPanel into JScrollPane and tried to draw an image to panel with Graphics g. No Scrollability and even when scroll bars are made to stay on permanently, they get overwritten.
    g=panel.getGraphics();
    if(isVisible()) g.drawImage(image,0,0,320,240,Color.BLUE,null);
    two: When putting the pane with the drawn images into toplevel pane into JtabbedPane. I can turn off the drawing of images with a check to isVisible(). When they are not in top level pane isVisible() does not work and the images get drawn over the other tabbed panes.
    BTW everything is swing. JPanel,JScrollPane,JTabbedPane.

    No Scrollability and even when scroll bars are made to stay on permanently,First of all the scrollbars will only appear when the preferred size of the panel is greater than the size of the scrollpane.
    A think a better design for updating the image is to extend JPanel and create a setImage method. The set image method would set the preferred size of the panel and then invoke repaint() on the panel. You would then also need to override the paintComponent(..) method to do the actual drawing of the image. Then when the image changes you just use the new setImage(..) method.

  • Can we have a JTabbedPane inside JPanel ?

    I am having following containment hierarchy.
    JFrame --> JSplitPane(RightComponent) --> JPanel -->
    JTabbedPane --> (JPanel1 and JPanel2)
    When I run the application, I see no tabs and only JPanel2 is loaded.
    regards,
    Nirvan

    Thanks for the reply.
    Encephalopathic wrote:
    nirvan wrote:
    When I run the application, I see no tabs and only JPanel2 is loaded.
    Then you've got a bug, but where it's impossible to guess. I recommend that you show us the smallest compilable > program possible that demonstrates your problem, an [SSCCE.|http://sscce.org] Please click on the link to see > how to create this. Best of luck.
    Intially, I just wanted to be sure whether the containment hierarchy is allowed or not. Hence I didn't post SSCCE. Anyways, you were right that there was a simple bug in the program. I have resolved it.
    regards,
    nirvan

  • JTabbedPane and scrolling

    I have a JTabbedPane that contains 3 JPanels (which are each tabs).
    The first 2 JPanels have a small size that fits in the window, but the 3rd JPanel contains a JScrollPane that has a large number of fields so it scrolls horizontally.
    My problem is the first 2 Panels no stretch the space between the fields so now I have to scoll to see what should fit on one screen.
    I am using GridBagLayout for the first 2 Panels and a FlowLayout for the 3rd panel which contains the JScollPane.
    Any ideas?

    I'm not sure I understand your question.
    If your scrollPane is the only component in the third panel then you should use BorderLayout so that it fills the whole tab.

  • JTabbedPane and content placement on the tab

    Hello,
    I created a tabbed pane using JTabbedPane. Added two tabs to it using addTab. One of the tab's content is quite tall while other tab's content is quite short. Contents of both tab are created and added to JPanel which is then added to the tab.
    When that window is displayed, contents in the short tab shows at the center of the screen. How do I make contents from short tab display at top....kind of like bind to the top.

    Of the many possible ways, the one that first comes to mind is to add the panel containing your "short" contents to a panel with a BorderLayout at NORTH, and add the outer panel to the tabbed pane.
    db

  • JTabbedPane and variable issue

    ok so here's what I've got, it's a mess I know, everything is added and defined and such, but got cut out to make the code fit, but before I can clean it up I have to figure out what is wrong, my problem as far as I can tell is I don't know how to reference variables in gui, I think I had this problem before but I don't know how to fix it, the variable names are right, it's looking in the right class but nothing in the tab can be found from the action listener. I don't know where else to look. I'm really hoping that's the issue anyway, and not something that requires the demolition of all the code. thank you,
    import java.awt.*;
    import javax.swing.*;
    import java.util.*;
    import java.text.*;
    import java.awt.event.*;
    import java.io.*;
    public class PoolEstimator extends JFrame
    public PoolEstimator()
    super( "Pool Estimator" );
    DefaultListModel listModel = new DefaultListModel();
    JTabbedPane tab = new JTabbedPane();
    // constructing the second panel
    JPanel CustInfo = new JPanel();
    CustInfo.setLayout(new FlowLayout(FlowLayout.LEFT));
    JLabel FirstName, LastName, Street, City, Zip, Phone, poolDepthup, poolDepthlow,
    yardLength, poolLength, yardWidth, poolWidth, state;
    JTextField FirstNamein, LastNamein, Streetin, Cityin, Zipin,
    Phonein, poolDepthupin, poolDepthlowin, yardLengthin, poolLengthin, yardWidthin, poolWidthin;
    String[] States = {"AL","AK","AS","AZ","AR","CA","CO","CT","DE","DC","FL","GA","HI","ID","IL","IN","IA",
    "KS","KY","LA","ME","MD","MA","MI","MN","MS","MO","MT","NE","NV","NH","NJ","NM","NY","NC","ND","OH","OK",
    "OR","PA","RI","SC","SD","TN","TX","UT","VT","VA","WA","WV","WI","WY"};
    JComboBox State = new JComboBox(States);
    FirstName = new JLabel("First Name");
    LastName = new JLabel("Last Name");
    Street = new JLabel("Street Address");
    City = new JLabel("City");
    state = new JLabel("State");
    Zip = new JLabel("Zip Code");
    Phone = new JLabel("Phone Number");
    yardLength = new JLabel("Length of the yard.");
    yardWidth = new JLabel("Width of the yard.");
    poolDepthup = new JLabel("Deepest depth of the pool.");
    poolDepthlow = new JLabel("Shalowest depth of the pool.");
    poolLength = new JLabel("Length of the pool.");
    poolWidth = new JLabel("Width of the pool.");
    FirstNamein  = new JTextField(20);
    LastNamein   = new JTextField(20);
    Streetin   = new JTextField(25);
    Cityin   = new JTextField(15);
    Zipin  = new JTextField(5);
    Phonein  = new JTextField(5);
    poolDepthupin  = new JTextField(2);
    poolDepthlowin  = new JTextField(2);
    yardLengthin  = new JTextField(3);
    poolLengthin  = new JTextField(3);
    yardWidthin  = new JTextField(3);
    poolWidthin  = new JTextField(3);
    JButton Add = new JButton("Add");
    CustInfo.add(Add);
    tab.addTab( "New Customer Estimate", null, CustInfo, "Customer Information Page" );
    getContentPane().add( tab );
    setSize( 325,400 );
    setVisible( true );
    addWindowListener(new WindowAdapter()
       public void windowClosing(WindowEvent e)
       System.exit(0);
         AddButtonHandler AddNew = new AddButtonHandler();
         Add.addActionListener(AddNew);
    class AddButtonHandler implements ActionListener
         public void actionPerformed(ActionEvent e)
              try
              //PoolEstimator run  = new PoolEstimator();
              String StateSelection = PoolEstimator.State.getSelectedItem().toString();
              FileWriter AddEstimate = new FileWriter("Estimate.dat", true);
              AddEstimate.write("First Name: ");
              //AddEstimate.write(run.FirstNamein.getText());
              AddEstimate.write("\nLast Name:  ");
              //AddEstimate.write(LastNamein.getText());
              AddEstimate.write("\nTotal Cost:  ");
              //int width, length, depthupper, depthlower, SurfaceArea, Cement, Rebar, Labor, totalCost;
              //width =Integer.parseInt(poolWidthin);
              //length = Integer.parseInt(poolLengthin);
              //depth = (Integer.parseInt(poolDepthupin) + Integer.parseInt(poolDepthlowin))/2;
              //SurfaceArea = (width * depth)*2+(length*depth)*2;
              //Cement = SurfaceArea * 3;
              //Rebar = SurfaceArea *5;
              //Labor = SurfaceArea%10*18;
              //totalCost = Cement+Rebar+Labor;
              //AddEstimate.write(totalCost);
              AddEstimate.write("\n,");
              AddEstimate.close();
              //run.listmodel.add(run.FirstNamein + run.LastNamein);
            catch(IOException r)
            JOptionPane.showMessageDialog(null,r.getMessage(),"ERROR",2);
    public static void main( String args[] )
    PoolEstimator run = new PoolEstimator();
    run.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    }

    Im a newbie but Im finding it hard to understand what your talking about.
    Could you explain what your program is suppose to do and what you want it to do that it isnt doing yet because you dont know how?
    Plus I just thought I would mention this incase but its easier to use the following JFrame method instead of adding a window adaptor:
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Maybe you are looking for

  • Lightroom 4.3 to Lightroom 5 fails

    I recently upgraded and found to my dismay that only the photos  in Lightroom 4.3 were passed into Lightroom 5. Everything else failed to come in, keywords, presets, identity plate, etc. Did I do something wrong when I upgraded?? Thanks, Jennings

  • UCCX CUIC Custom Reports

    Hi Community, I'm creating new reports for one of our customers with the CUIC NFR kit in our lab. But I do not want to create the reports from scratch and want to use the report definition templates from UCCX. But I don't know where I can download th

  • Phone hog links give me error message "an error occuredon the server when processing the URL

    When ever I click on the links sent from phone hog.com to earn free minutes I get the following error message. "An error occurred on the server when processing the URL. Please contact the system administrator" I have made several contacts with phone

  • Gnome applet Icons Missing

    I installed a fresh arch linux on another pc.  I installed gnome and gnome-extras. There is one problem: The icons on the "system tray" are missing. The applets are there but the icons are not shown: I already rebuilt the hicolor and gnome icons (and

  • Can't open PSD? 0 bytes?

    Hello, I have Photoshop CS5.1 and Windows 8. I have been having an issue with opening a PSD file and I can't seem to find someone asking the same question anywhere. My issues is that I have a .psd file that was fine yesterday but today I tried to ope