Line in a JLabel that disappear when adding to a new frame

Hi,
I have the main frame (SampleFrame), where I put a JPanel with CardLayout, I want when I click on a button, a new JPanel (SamplePanel) is shown.
The problem is this new JPanel shows a picture (a line in a JLabel), the poblem is the picture does not appear...
The code is the following:
package sample;
//Main Class
public class Main {
    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new SampleFrame().setVisible(true);
//SampleFrame
package sample;
import  javax.swing.JPanel;
import javax.swing.*;
import java.awt.*;
public class SampleFrame extends javax.swing.JFrame {
    private SamplePanel samplePanel;
    private boolean flag;
     /** Creates new form SampleFrame */
    public SampleFrame() {
        initComponents();
        flag=false;
        samplePanel = new SamplePanel();
        jPanel1.add(samplePanel,"sample");
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">                         
    private void initComponents() {
        jPanel1 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        jPanel1.setLayout(new java.awt.CardLayout());
        jButton1.setText("OK");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 189, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .add(81, 81, 81)
                        .add(jButton1)))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 182, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 19, Short.MAX_VALUE)
                .add(jButton1)
                .addContainerGap())
        pack();
    }// </editor-fold>                       
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
// TODO add your handling code here:
        CardLayout cl = (CardLayout)(jPanel1.getLayout());
        cl.show(jPanel1, "sample");
        flag=true;
      /*  this.setVisible(false);
        this.setVisible(true);*/
     public void paint(Graphics g)
        super.paint(g);
        if(flag)samplePanel.ReDraw();
    public void printAll(Graphics g)
        super.printAll( g);
        if(flag)samplePanel.ReDraw();
    // Variables declaration - do not modify                    
    private javax.swing.JButton jButton1;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration                  
//SamplePanel
package sample;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class SamplePanel extends javax.swing.JPanel {
    /** Creates new form SamplePanel */
    public SamplePanel() {
        initComponents();
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">                         
    private void initComponents() {
        jLabel1 = new javax.swing.JLabel();
        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 150, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 150, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    }// </editor-fold>                       
     public void paint(Graphics g)
        super.paint(g);
        ReDraw();
    public void printAll(Graphics g)
        super.printAll( g);
        ReDraw();
    public void ReDraw()
        Graphics g = jLabel1.getGraphics();
        g.drawLine(0,0,100,100);
    // Variables declaration - do not modify                    
    private javax.swing.JLabel jLabel1;
    // End of variables declaration                  
}I have discovered that with the comment lines in SampleFrame (method jButton1ActionPerformed):
/*  this.setVisible(false);
this.setVisible(true);*/works properly, but I dont like this way...
Can anyone helps me? Thanks ;)

Hi again crwood!!
Okay, you're taking the content pane, a Container, from a top�level container (you mentioned a frame, "oldFrame"). And this container contains some JLabels which have lines drawn in them. You want to add this container to a card in a Cardlayout. Okay, so far so good.
That�s exactly what I want ;).
So (using your code):
I have the panel:
class SP extends JPanel {
    private int x1;
    private int y1;
    private int x2;
    private int y2;
    private Color color;
     private JLabel label;
    public SP(int x1, int y1, int x2, int y2, Color c) {
        this.x1 = x1;
        this.y1 = y1;
        this.x2 = x2;
        this.y2 = y2;
        color = c;
        setLayout(new BorderLayout());
        label =new JLabel();
        add(label);
    protected void paintComponent(Graphics g) {
                super.paintComponent(g);
          label.getGraphics().setColor(color);
          label.getGraphics().drawLine(x1, y1, x2, y2);
    public Dimension getPreferredSize() {
        return new Dimension(300, 200);
}And the main frame:
public class SF extends JFrame {
    private JPanel jPanel1;
    public SF() {
        // Initialize components.
        jPanel1 = new JPanel(new CardLayout());
        jPanel1.add(new SP(10,10,100,100,Color.red), "sample 1");
        jPanel1.add(new SP(50,150,200,50,Color.blue), "sample 2");
        JButton jButton1 = new JButton("OK");
        jButton1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                CardLayout cl = (CardLayout)(jPanel1.getLayout());
                cl.next(jPanel1);
        JPanel south = new JPanel();
        south.add(jButton1);
        // Configuration and assemble JFrame.
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        add(jPanel1);   // Default center section.
        add(south, "Last");
        pack();
        setLocation(200,200);
        setVisible(true);
    public static void main(String[] args) {
        new SF();
}The problem is I cant see the black line...
(I am able to see it by moving the window until hide part of it)

Similar Messages

  • HT1347 When I add a folder to the library, does that mean that 'later' when I drop a new file into that folder (already added) it will automatically end up in iTunes? (or do I have to add file/folder to library every time I have a new file??) I'm using XP

    When I add a folder to the library, does that mean that 'later' when I drop a new file into that folder (already added) it will automatically end up in iTunes? (or do I have to add file/folder to library every time I have a new file??) I'm using XP...

    iTunes does not actively scan the media folders.  If you manually add content to a folder, then you must add that content to iTunes manually via the File > Add File/Folder feature.
    The only folder that gets scanned is the "Automatically add to iTunes" folder.

  • TS3714 How can i restore or retrieve my calendar inputs that disappeared when i tried to sync it to my internet accounts?? I didnt find it on my yahoo and gmail accounts on thw web. Pls help me

    How can i restore or retrieve my calendar inputs that disappeared when i tried to sync it to my internet accounts?? I didnt find it on my yahoo and gmail accounts on thw web. Pls help me

    You don’t need to do that. Click here and follow the instructions, or if they don't cover the type of adware on the computer, these ones. If you're willing to download software to resolve this issue(you don't need to, but may find it easier), you can instead run Adware Medic; this link is a direct download.
    (117389)

  • Is there anyway to get an App Tab to stay when I close the program then reopen it?...they always disappear when I open a new window etc.

    Is there anyway to get an App Tab to stay when I close the program then reopen it?...they always disappear when I open a new window etc.
    And I thought the whole point of an App Tab was to always have those tabs there no matter what window is open all the time period.
    It's a minor complaint but I would really appreciate the option to permanently have them there.

    I thought that app tabs would be more persistent, like pinning programs to the taskbar in Windows, but this is not the case.
    Instead, once you close a Firefox window, all its tabs and app tabs are lost.
    Essentially, this means that the close button in the upper-left corner of a Firefox window is now a "kill all open tabs and loose all App Tabs without warning++ " button. That is not cool.
    I realize that "Firefox > Quit Firefox" behaves as intended, but many people use the more obvious red close button instead of the menu to close windows on a Mac. And it is easy to forget that the red button is now also an app-tab-death button. I've lost my app tabs many times to this issue. It is an unforgiving user interface decision which hampers the usability of app tabs. Why should I go through the extra clicks of setting up app tabs if I can loose them so easily?
    Ideally, App Tabs could be saved separately from the currently open windows (perhaps create an option to save then to a special Tab Group that opens on startup?). App tabs would be exceedingly useful to me if they weren't so easy to loose.
    On that note, saving Tab Groups would also be a useful functionality.
    ++ You will get a "closing multiple tabs" warning if you have only one window open, and that one window has multiple non-app tabs open, and you have enabled the warning in Preferences. But this too unreliable to help prevent the loss of app tabs.

  • IMovie disappears when Adding the overlap transition?

    Hello all
    haveing a bit of a problem when adding the Overlap Transition:(:(
    So far all the others work fine its just the overlap, any Ideas?
    Ive checked for updates but none avalible. next step will be uninstalling the imove and reinstalling it.
    Thanks

    sounds like you may have corrupted video clips (i know you said pictures but imovie turns the pictures into small video files when you import them) try pulling out the pictures that are giving you this issue. after that close imovie. reopen imovie and reimport the pictures back into the project and try to add the same transition.
    I ran into the same issue running a project off a external hard disk. Ended up the 2 pictures i was trying to add the transition between somehow became corrupt. Deleteing them. Closing out imovie and reopening and reimporting the images all over into imovie resolved this issue for me. Hope it works for you as well
    Cheers.

  • Image Disappears when Adding Adjustments in Aperture 3

    I am experiencing odd behavior when using the adjustment tools in Aperture 3. I tried to crop an image and instead of staying on screen for me to finish the adjustment, the image disappeared. I was able to save it by undoing. I created a version of the image and I could crop that OK. I have Add New Version when Making Adjustments checked but it does not seem to be working.

    I've had a similar "disappearing" image problem when trying to apply the retouch tool. I don't have the "create version when applying adjustments" preference checked though. Hopefully these issues are additional 3.0 bugs that will get fixed in 3.0.1. Be sure to report the issue to Apple using the "Aperture > Provide Aperture Feedback" menu item.

  • Are your bookmarks disappearing when you add a new one in Safari?

    When I add a new bookmark many of the old ones just disappear!
    I have recovered my archived bookmarks from Time Machine but the minute I go to add a new bookmark, many disappear again!
    HELP please!!!!
    Anyone else experiencing this???

    Quit Safari. Disable Safari synchronization in iCloud, if applicable.
    Hold down the option key and select Go ▹ Go to Folder… from the Finder menu bar. Copy the following line into the text box that opens, then press return:
    ~/Library/Safari/Bookmarks.plist
    Move the selected file to the Desktop, leaving the Finder window open. Relaunch Safari. Select File ▹ Import Bookmarks from the Safari menu bar. Import from the bookmarks file you moved to the Desktop. Test. If Safari now performs normally, you can delete the old bookmarks file. Otherwise, quit Safari again and put back the file you moved, replacing the newer one with the same name. Close the Finder window and post again.

  • The firefox theme(Lava V1-purple) I installed does not show permanently its background image because it quickly disappears when I click a new tab.. how can i fix this???

    When i click a new tab button the background image appears but quickly disappears... I know that this shouldn't happen but it always happen no matter what i try... Can anyone help me??? Please??? I want my background, that is provided by the theme, to stay pemanently when i click the new tab button because the white background for a new tab is totally not my style..

    If you go to [www.zigboom.com ZigBoom] there is a customization tab that will explain how to set your about:blank (New Tab) background image using the Firefox Add-On Stylish. Hope this helps.

  • How do I change the default page that appears when I open a new tab or window?

    I downloaded a program (Babylon) to translate text. After trying it out, I removed the program using their uninstall and then the add/remove functionality within windows. Nevertheless, remnants remain. When I attempt to open a new tab in Firefox (and IE for that matter), I get the Babylon Search page. In Chrome, when I changed the homepage, it eliminated the issue when opening a new tab. How do I establish a new default page when I open a new tab?

    See:
    *http://www.babylon.com/support/faq/usage.html#22
    *[[/questions/746530]]

  • How come my text disappears when I choose a new theme?

    I'm new to keynote and I've imported a powerpoint presentation and I want to change the theme. When I do this, my text disappears. Can anyone help?

    sometimes this can appear to happen if the text is in a "body" box but the master slide doesn't have the body box turned on by default.
    Once your text goes missing, click on the slide inspector and then on the Appearance tab. See if the title or body are turned off, and check them to turn them back on.
    Also, when you choose the new theme, use the theme choose, don't just pick a theme from the list, and when the chooser comes up, UNCHECK the retain changes to theme defaults. This should force your ppt into the new theme more correctly.
    If your text is still missing, then there's something else going on here.

  • Why my movie clip disappears when it reaches to last frame?

    Hi,
    I have a movie clip on stage which length is 600 frames. I put a actionscript 3 code "stop();)" at the end of movie. it stops when it reaches to 600 frames but it disappears from the stage as well. Which I don't want. It should stop animating but stay with last fram which has a welcome message.
    How can I do this?

    I should remain visible, so you must be overstepping the last frame or the movieclip in some way.  Maybe the timeline your animation is in does not go as far as the timeline.

  • How do you change the default swatches that appear when I open a new document?

    When I open a new Illustrator CC file, I would like to have some of my favorite color swatches available in the color swatches panel.  How do I edit the swatches panel so they always appear on new documents?
    Thanks,
    Paul

    Paul,
    You may add them to your Adobe Illustrator Startup_CMYK and/or Adobe Illustrator Startup_RGB files in your Plugins folder.
    What happens if you DoubleClick those swatches and Tick Global?

  • [Help]Timeout when going to a new frame!

    Hello Community!!! so i searched for something but i can't found anything!!!
    so i have a button... and that button when you are pressing it you ar go to an another frame... but when you will press it again it will go back to the first frame!
    so cause i have an error with "FadeSymbolIn" code that when you are going to frame and pressing too early the button to go back... the Flash Player 11 show me an error.... and the Alpha on this frame is going to 2 from 1.
    so i want when you are pressing the button and going to a new frame to can't press other button so early... but for example to be freezed for 1 second!!! i want something like that!!!
    Please help me if you can! i will be very glad for your help! i am new o flash and don't know too much!!! And sorry for my bad English!!!!!

    i don't have any problem on swf...
    only on Projector i have this error...
    i think is a flash player bug!!! not any wrong code on my script! but take a look!!!
    TypeError: Error #1009: Cannot access a property or method of a null object reference.
       at FinalVersion_fla::MainTimeline/fl_FadeSymbolIn_18()[FinalVersion_fla.MainTimeline::frame19:160]
    the frame doesn't matter... it will be to all the frames if i will press the buttons too quickly...
    and the code that gives the error is:
    OSRAM_popup.addEventListener(Event.ENTER_FRAME, fl_FadeSymbolIn_18);
    OSRAM_popup.alpha = 0;
    function fl_FadeSymbolIn_18(event:Event)
              OSRAM_popup.alpha += 0.1;
              if(OSRAM_popup.alpha >= 1)
                        OSRAM_popup.removeEventListener(Event.ENTER_FRAME, fl_FadeSymbolIn_18);
    is flash player bug cause on swf file i don't have this error when i press the button too quickly

  • Page Content Disappears When Adding WebPart SP 2010

    I have been working on a project (wiki page).  I have added WPs.  There are only 2 on the page.  One day one of the WPs was not there I am trying to upload the same one.  When I do, the entire page disappears.  So I close it without
    editing it and it returns.  Any suggestions would be great.  I have not altered the master pages.
    d.bell

    You can try checking if the other web part is already on the page or not. Someone might have closed that web part. 
    At the end of your page URL type
    contents=1
    e.g.
    http://intranet.contoso.com/Project1/Pages/Default.aspx?contents=1
    If you see your two web parts there then Delete the one which is not visible on the page. You may have to checkout the page for this. 
    Now go back and add the web part back and see if that works.
    Amit

  • Task Line Numbers Move Out of Order When Adding Resource to a Task

    I am using MS Project 2013 and we are in the process of setting up Project Server 2013. 
    When I add a resource to a task and close the summary task and re-open the task is moved to the bottom of that section and the line numbers are out of order.
    For example line number 382 is the third task under the summary task.  When I add a resource, hide the subtasks and then expand the subtasks line number 382 is now at the bottom after line 389.  The line numbers then go 381, 383, 384... until 389
    then 382 is after 389.
    I want to keep the tasks in sequential order.
    Has anyone seen this problem before?  I have done extensive research online and cannot seem to find any one with a similar issue.
    Thank you! :)

    Hi Dnbree,
    Do you have any filters applied to the view in Project Pro? Try to change view and check if you experience the same issue.
    Also check if the view uses a sorting in the view tab of the ribbon, such as sort by resources.
    Hope this helps.
    Guillaume Rouyre - MBA, MCP, MCTS

Maybe you are looking for