JPanel disappear

I put a JPanel in a JFrame.After that I created a new JFrame where I put the last JPanel.When I saw the first JFrame the JPanel did not exist anymore.Why?
Please help!

A Component can only be in one Container at a time. Sorry.

Similar Messages

  • JLabel on JPanel Disappears(Small CODE!)

    Um... I had posted a message a earlier just ot make the complex problem simpler. I have made this small version of my massive code and again the JLabel does not get displayed on the JPanel.
    This is urgent please help me.
    import java.awt.event.*;
    import java.awt.*;
    import javax.swing.*;
    class LabelTry
         public static void main(String args[])
              JFrame aFrame= new JFrame("This is a Test");
              aFrame.addWindowListener(new WindowAdapter()
              {public void windowClosing(WindowEvent e)
              {System.exit(0);
              aFrame.setSize(500,500);
              Container c = aFrame.getContentPane();
              JPanel secPanel = new JPanel();
              secPanel.setLayout(null);
              secPanel.setBackground(Color.gray);
              secPanel.setBorder(BorderFactory.createEtchedBorder());
              JLabel aLabel = new JLabel(" ");
              aLabel.setBounds(100,100,10,10);
              aLabel.setBackground(Color.black);
              aLabel.setOpaque(false);
              aLabel.setVisible(true);
              secPanel.setOpaque(false);
              secPanel.add(aLabel);
              c.add(secPanel, BorderLayout.CENTER);
              aFrame.setVisible(true);
         }//end main
    }//end class LabelTry

    I've got it working now, but I cannot get it to work in a bigger program.
    the code that runs properly is as follows
    /;code
    class LabelTry
         public static void main(String args[])
              JFrame aFrame= new JFrame("This is a Test");
              aFrame.addWindowListener(new WindowAdapter()
              {public void windowClosing(WindowEvent e)
              {System.exit(0);
              aFrame.setSize(500,500);
              Container c = aFrame.getContentPane();
              JPanel secPanel = new JPanel();
              secPanel.setLayout(null);
              secPanel.setBackground(Color.gray);
              secPanel.setOpaque(false);
              secPanel.setBorder(BorderFactory.createEtchedBorder());
              JLabel aLabel = new JLabel(" ");
              aLabel.setBounds(200,200,10,10);
              aLabel.setBackground(Color.black);
              aLabel.setOpaque(true);
              aLabel.setVisible(true);
              secPanel.add(aLabel);
              c.add(secPanel, BorderLayout.CENTER);
              aFrame.setVisible(true);
         }//end main
    }//end class LabelTry
    //end code

  • The disappearing JPanel

    Hi All,
    I have a problem with a Jpanel and a JeditorPane, both are contained within a Jdialog.
    The JeditorPane is used to display and HTML result of xml/xslt just display no inter action.
    The Jpanel is rendered ontop of the JeditorPane with smaller red bordered panels contained within the jpanel.
    when the user clicks in the smaller red panel s/he can edit the html the problem is when i return from the editor form to the jdialog with the Jpanel and Jeditor pane sometimes the jpanel disappears and i can only see the jeditor with the update contents.
    I have tried combenations of revalidate and refreshes as well as show hide sequences i have also set the jdialogs layout to nulle and then after each content edit set the bounds of the jpanel and jeditor in an attempt to have the jpanel render ontop of the jeditor pane. But to no success
    Any help or directions is appreciated
    Cheers Wurns

    How about design like this
    myParentPanel
    | |
    JEditorPanel JButtonPanel
    When you update JEditorPanel, you forgot to add jpanel inside,
    although I don't know the update method look like. Everytime you update,
    you have to add jpanel like the first time, would you like to do that???
    I hope I understand correctly.

  • Problems sizing multiple JPanels.

    I'm writing a survey program that needs to be formatted to a certain size. I'm using calls to panel.setsize( ) to set the size to 800 x 600.
    I have nine JPanels as globals, all of which are created and "packed" initially. These JPanels are supposed to display in a global JFrame, one at a time. After each "next button" click I call frame.remove(panel) to remove the current panel and frame.add(panel) to add the next. Then I call pack again.
    Everything works fine until looping from the final panel to the first. Upon doing so all of the elements in the JPanel disappear, (although hovering over the invisible elements makes them reappear.)
    Is there anything I'm doing wrong?

    Well, calling repaint after pack seemed to solve the problem.
    Does anyone care to provide insight as to why?
    Thanks.

  • JSplitPane acts weird when resized very small

    Say I have two JPanels in a horizontal JSplitpane. One is in the left pane, the other the right. Each JPanel sits in another JPanel that uses a Borderlayout, holding a JPanel in the center.
    I place the JSplitPane in a JFrame. When I resize the JFrame to the size of a postage stamp, the right JPanel disappears. OK, I can live with that. But when I resize to say 600x800, the left JPanel takes all the room, the divider sits to the far right and the right JPanel is completely hidden. Argh! How can I avoid this? I know there must be a way but how? When I resize the JFrame to a small size, possibly 1cm x 1cm, one of the panels gets lost and upon resizing the divider will be either to the far right or left with one JPanel taking all the room and the other hidden.
    Help! My boss is threatening to fire me if I can't fix this. Ok, the last part was a joke but still a solution would be great.

    I would suspect that it has something to do with the minimum sizes of the components in the splitpane. This seems to work OK.import java.awt.*;
    import javax.swing.*;
    public class Test extends JFrame {
      public Test() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container content = getContentPane();
        JSplitPane jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
                                        new JLabel("Left"), new JLabel("Right"));
        content.add(jsp, BorderLayout.CENTER);
        setSize(300, 300);
        setVisible(true);
      public static void main(String[] args) { new Test(); }
    }

  • Disappearing lines in a JPanel

    I have some buttons in a panel and the panel is placed in another panel which contains some lines and these two panels are added to a scrollpane. The problem is that, if I scroll the frame or place another window on the frame the lines disappear. I have not used getGraphics() method. All the painting has been done in the paintComponent().What might be the reason.

    What might be the reason. You are doing something wrong.
    If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.
    And don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags so the code retains its original formatting.

  • Disappearing Graphics in Jpanels!

    Hi all,
    I am new to java and have a slight problem. I have two forms, one is my main form and the other is used for a settings option for changing various aspects of my program such as colours etc.
    On my main form I have 2 jPanels - one that shows my main image (it's a graph that I've created and used BufferImage to then draw it on the jPanel. The other shows a smaller version of this using similar method but scale the original image to make it smaller.
    I have a function called drawStuff which draws all the main graphs, one called drawScales which draws all the axis labels and small graphs. I called upon these when I want them to be drawn. I don't use paint() or paintcomponent() etc.
    My problem is that whenever I open my settings menu, the settings form appears over my jpanel, like it should, but all of a sudden all the images on both jpanels dissapear! so when I exit the settings menu both panels are empty! I want them to stay the way they are until I redraw them!
    public void drawStuff()
      Graphics2D l = (Graphics2D)g;         
               if (window==0){
               if ((mpress=true)| (bpre==1)){
               bpre=0;
               h.setColor(Color.white);
               h.fillRect(0,0,661,352);
               h.setColor(Color.white);
               h.fillRect(0,0,661,352);      
               h.setColor(Color.black);
               h.drawLine(0,0,0,351); // draw graph axis
               h.drawLine(0,351,661,351);
               h.setColor(jPanel9.getBackground());
               h.fillRect(1,0,660,351);
               h.setColor(jPanel10.getBackground());
               int temp=scalep;
                while (1+scalep+swp<661){
                h.fillRect((1+scalep),0,swp,351);
                scalep=scalep+swp+swp;
               if (1+scalep<661){
                h.fillRect(scalep,0,660,351);  
               scalep=temp;
               GetCols(5);
               int psize=jSlider8.getValue();
               for (int o=1;o<nolin+1;o++){   
                    if (zdata[o][1][zomlev]==1){
                        zdata[o][1][zomlev+1]=0;
                         h.fillRect(660-test1[o][1],0+test1[o][2],psize,psize);
               g.drawImage(Buffer,75,30,this);
               xl=0;
               xr=0;
               yl=0;
               yr=0;
             if (mdrag==true){
                  g.drawImage(Buffer,75,30,this);
                  g.setColor(Color.black);
                  l.setColor (Color.black);
                  l.setStroke(dashed);
                 //bottom right
                 if ((selx1 < selx2) & (sely1 < sely2)){
                 //g.setColor(Color.black);     
                 //g.drawRect(selx1, sely1, selx2-selx1, sely2-sely1);
                  g.drawRect(selx1, sely1,selx2-selx1, sely2-sely1);
                  //l.draw(rectangle);
                 xl=selx1;
                 xr=selx2;
                 yl=sely1;
                 yr=sely2;
                 //bottom left
                 if ((selx1 > selx2) & (sely1 < sely2)){
                 g.drawRect(selx2, sely1,selx1-selx2, sely2-sely1);
                 xl=selx2;
                 xr=selx1;
                 yl=sely1;
                 yr=sely2;
                 //top left
                 if ((selx1 > selx2) & (sely1 > sely2)){
                 g.drawRect(selx2, sely2,selx1-selx2, sely1-sely2);
                 xl=selx2;
                 xr=selx1;
                 yl=sely2;
                 yr=sely1;
                 //top right
                  if ((selx1 < selx2) & (sely1 > sely2)){
                 g.drawRect(selx1, sely2,selx2-selx1, sely1-sely2);
                 xl=selx1;
                 xr=selx2;
                 yl=sely2;
                 yr=sely1;
                 GetCols(4);
                 int psize=jSlider8.getValue();
                  for (int o=1;o<nolin+1;o++){
                     if (zdata[o][1][zomlev]==1){
                     if ((735-test1[o][1]>xl) & (735-test1[o][1]<xr) & (30+test1[o][2]>yl) & (30+test1[o][2]<yr)){               
                         g.fillRect(735-test1[o][1],30+test1[o][2],psize,psize);
                         zdata[o][1][zomlev+1]=1;
             sxl=xl;
             sxr=xr;
             syl=yl;
             syr=yr;
             l.setStroke(stroke);
               /*if ((nbox==1) | (red==0)){
               g.setColor(Color.white);
               g.fillRect(3,16, 764, 401);
               g.setColor(Color.black);
               g.drawLine(75,380,725,380); // draw graph axis
               g.drawLine(75,380,75,30);
               //timescale();
               //timetwofour();
               g.drawString(twofours,50,400);
               String minyS=String.valueOf(miny);
               g.drawString(minyS,10,380);
               g.drawString(twofoure,705,400);
               String maxyS=String.valueOf(maxy);
               g.drawString(maxyS,10,40);
               g.drawString("Peak Energy",5,205);
               g.drawString("Time of Signal",400,400);
           if (nbox==1){
                 g.setColor(Color.white); // clear previous box
                 g.fillRect(xl, yl, xr-xl+1, yr-yl+1);
                 for (int o=1;o<nolin+1;o++){
                     if (zdata[o][1][zomlev]==1){
                     if ((725-test1[o][1]>xl) & (725-test1[o][1]<xr) & (30+test1[o][2]>yl) & (30+test1[o][2]<yr)){
                         g.setColor(Color.lightGray);                
                         g.fillOval(725-test1[o][1],30+test1[o][2],2,2);
                 nbox=0;
                 xl=0;
                 xr=0;
                 yl=0;
                 yr=0;
             if (red==1){
               g.setColor(Color.black);
               g.drawLine(75,380,725,380); // draw graph axis
               g.drawLine(75,380,75,30);
                 //bottom right
                 if ((selx1 < selx2) & (sely1 < sely2)){
                 if (sxl>0){
                 g.setColor(Color.white);
                 g.fillRect(sxl-2, syl-2, sxr-sxl+4, syr-syl+4);//all
                 for (int o=1;o<nolin+1;o++){
                     if (zdata[o][1][zomlev]==1){
                     if ((725-test1[o][1]>sxl-3) & (725-test1[o][1]<sxr+3) & (30+test1[o][2]>syl-3) & (30+test1[o][2]<syr+3)){
                         g.setColor(Color.lightGray);                
                         g.fillOval(725-test1[o][1],30+test1[o][2],2,2);
                 g.setColor(Color.black);
                 g.drawRect(selx1, sely1,selx2-selx1, sely2-sely1);
                 g.setColor(Color.white);
                 g.fillRect(selx1+1, sely1+1, selx2-selx1-1, sely2-sely1-1);
                 xl=selx1;
                 xr=selx2;
                 yl=sely1;
                 yr=sely2;
                 //bottom left
                 if ((selx1 > selx2) & (sely1 < sely2)){
                 if (sxl>0){
                 g.setColor(Color.white);
                 g.fillRect(sxl-2, syl-2, sxr-sxl+4, syr-syl+4);//all
                 for (int o=1;o<nolin+1;o++){
                     if (zdata[o][1][zomlev]==1){
                     if ((725-test1[o][1]>sxl-4) & (725-test1[o][1]<sxr+4) & (725-test1[o][2]>syl-4) & (30+test1[o][2]<syr+4)){
                         g.setColor(Color.lightGray);                
                         g.fillOval(725-test1[o][1],30+test1[o][2],2,2);
                 g.setColor(Color.black);
                 g.drawRect(selx2, sely1,selx1-selx2, sely2-sely1);
                 g.setColor(Color.white);
                 g.fillRect(selx2+1, sely1+1, selx1-selx2-1, sely2-sely1-1);
                 xl=selx2;
                 xr=selx1;
                 yl=sely1;
                 yr=sely2;
                 //top left
                 if ((selx1 > selx2) & (sely1 > sely2)){
                 if (sxl>0){
                 g.setColor(Color.white);
                 g.fillRect(sxl-2, syl-2, sxr-sxl+4, syr-syl+4);//all
                 for (int o=1;o<nolin+1;o++){
                     if (zdata[o][1][zomlev]==1){
                     if ((725-test1[o][1]>sxl-4) & (725-test1[o][1]<sxr+4) & (30+test1[o][2]>syl-4) & (30+test1[o][2]<syr+4)){
                         g.setColor(Color.lightGray);                
                         g.fillOval(725-test1[o][1],30+test1[o][2],2,2);
                 g.setColor(Color.black);
                 g.drawRect(selx2, sely2,selx1-selx2, sely1-sely2);
                 g.setColor(Color.white);
                 g.fillRect(selx2+1, sely2+1, selx1-selx2-1, sely1-sely2-1);
                 xl=selx2;
                 xr=selx1;
                 yl=sely2;
                 yr=sely1;
                 //top right
                  if ((selx1 < selx2) & (sely1 > sely2)){
                 if (sxl>0){
                 g.setColor(Color.white);
                 g.fillRect(sxl-2, syl-2, sxr-sxl+4, syr-syl+4);//all
                 for (int o=1;o<nolin+1;o++){
                     if (zdata[o][1][zomlev]==1){
                     if ((725-test1[o][1]>sxl-4) & (725-test1[o][1]<sxr+4) & (30+test1[o][2]>syl-4) & (30+test1[o][2]<syr+4)){
                         g.setColor(Color.lightGray);                
                         g.fillOval(725-test1[o][1],30+test1[o][2],2,2);
                 g.setColor(Color.black);
                 g.drawRect(selx1, sely2,selx2-selx1, sely1-sely2);
                 g.setColor(Color.white);
                 g.fillRect(selx1+1, sely2+1, selx2-selx1-1, sely1-sely2-1);
                 xl=selx1;
                 xr=selx2;
                 yl=sely2;
                 yr=sely1;
                 for (int o=1;o<nolin+1;o++){
                     if (zdata[o][1][zomlev]==1){
                     if ((725-test1[o][1]>xl) & (725-test1[o][1]<xr) & (30+test1[o][2]>yl) & (30+test1[o][2]<yr)){
                         g.setColor(Color.blue);                
                         g.fillOval(725-test1[o][1],30+test1[o][2],2,2);
             sxl=xl;
             sxr=xr;
             syl=yl;
             syr=yr;
             if (window==0){
                 for (int o=1;o<nolin+1;o++){
                     if (red==0){
                         if (zdata[o][1][zomlev]==1){
                         g.setColor(Color.lightGray);                
                         g.fillOval(725-test1[o][1],30+test1[o][2],2,2);
                     //if (red==0){
                    // if (data[o][1]==1){
                    // if (((540-test1[o][1])<selx2) && ((540-test1[o][1])>selx1) && ((380-test1[o][2])>sely1) && ((380-test1[o][2])<sely2)){
                     //       g.setColor(new java.awt.Color(200, 200, 200));}
                 //   g.fillOval(540-test1[o][1],380-test1[o][2],2,2);   
          /*if (window==1){
               if ((nbox==1) | (red==0)){
               g.setColor(Color.white);
               g.fillRect(3,16, 764, 401);
               g.setColor(Color.black);
               g.drawLine(75,zeroy,725,zeroy); //  y zero axis
               g.drawLine(zerox,30,zerox,380); // x zero axis
               String minxS="0";
               g.drawString(minxS,zerox,400);
               String minyS="0";
               g.drawString(minyS,10,zeroy);
               g.drawString("Log 10 R",10,205);
               g.drawString("Difference in Time of Flight",400,400);
           if (nbox==1){
                 g.setColor(Color.white); // clear previous box
                 g.fillRect(xl, yl, xr-xl+1, yr-yl+1);
                 for (int o=1;o<nolin+1;o++){
                     if (zdata[o][1][zomlev]==1){
                     if ((725-test1[o][1]>xl) & (725-test1[o][1]<xr) & (30+test1[o][2]>yl) & (30+test1[o][2]<yr)){
                         g.setColor(Color.lightGray);                
                         g.fillOval(725-test1[o][1],30+test1[o][2],2,2);
                 nbox=0;
                 xl=0;
                 xr=0;
                 yl=0;
                 yr=0;
             if (red==1){
               g.setColor(Color.black);
               g.drawLine(75,zeroy,725,zeroy); //  y zero axis
               g.drawLine(zerox,30,zerox,380); // x zero axis
                 //bottom right
                 if ((selx1 < selx2) & (sely1 < sely2)){
                 if (sxl>0){
                 g.setColor(Color.white);
                 g.fillRect(sxl-2, syl-2, sxr-sxl+4, syr-syl+4);//all
                 for (int o=1;o<nolin+1;o++){
                     if (zdata[o][1][zomlev]==1){
                     if ((725-test1[o][1]>sxl-3) & (725-test1[o][1]<sxr+3) & (30+test1[o][2]>syl-3) & (30+test1[o][2]<syr+3)){
                         g.setColor(Color.lightGray);                
                         g.fillOval(725-test1[o][1],30+test1[o][2],2,2);
                 g.setColor(Color.black);
                 g.drawRect(selx1, sely1,selx2-selx1, sely2-sely1);
                 g.setColor(Color.white);
                 g.fillRect(selx1+1, sely1+1, selx2-selx1-1, sely2-sely1-1);
                 xl=selx1;
                 xr=selx2;
                 yl=sely1;
                 yr=sely2;
                 //bottom left
                 if ((selx1 > selx2) & (sely1 < sely2)){
                 if (sxl>0){
                 g.setColor(Color.white);
                 g.fillRect(sxl-2, syl-2, sxr-sxl+4, syr-syl+4);//all
                 for (int o=1;o<nolin+1;o++){
                     if (zdata[o][1][zomlev]==1){
                     if ((725-test1[o][1]>sxl-4) & (725-test1[o][1]<sxr+4) & (725-test1[o][2]>syl-4) & (30+test1[o][2]<syr+4)){
                         g.setColor(Color.lightGray);                
                         g.fillOval(725-test1[o][1],30+test1[o][2],2,2);
                 g.setColor(Color.black);
                 g.drawRect(selx2, sely1,selx1-selx2, sely2-sely1);
                 g.setColor(Color.white);
                 g.fillRect(selx2+1, sely1+1, selx1-selx2-1, sely2-sely1-1);
                 xl=selx2;
                 xr=selx1;
                 yl=sely1;
                 yr=sely2;
                 //top left
                 if ((selx1 > selx2) & (sely1 > sely2)){
                 if (sxl>0){
                 g.setColor(Color.white);
                 g.fillRect(sxl-2, syl-2, sxr-sxl+4, syr-syl+4);//all
                 for (int o=1;o<nolin+1;o++){
                     if (zdata[o][1][zomlev]==1){
                     if ((725-test1[o][1]>sxl-4) & (725-test1[o][1]<sxr+4) & (30+test1[o][2]>syl-4) & (30+test1[o][2]<syr+4)){
                         g.setColor(Color.lightGray);                
                         g.fillOval(725-test1[o][1],30+test1[o][2],2,2);
                 g.setColor(Color.black);
                 g.drawRect(selx2, sely2,selx1-selx2, sely1-sely2);
                 g.setColor(Color.white);
                 g.fillRect(selx2+1, sely2+1, selx1-selx2-1, sely1-sely2-1);
                 xl=selx2;
                 xr=selx1;
                 yl=sely2;
                 yr=sely1;
                 //top right
                  if ((selx1 < selx2) & (sely1 > sely2)){
                 if (sxl>0){
                 g.setColor(Color.white);
                 g.fillRect(sxl-2, syl-2, sxr-sxl+4, syr-syl+4);//all
                 for (int o=1;o<nolin+1;o++){
                     if (zdata[o][1][zomlev]==1){
                     if ((725-test1[o][1]>sxl-4) & (725-test1[o][1]<sxr+4) & (30+test1[o][2]>syl-4) & (30+test1[o][2]<syr+4)){
                         g.setColor(Color.lightGray);                
                         g.fillOval(725-test1[o][1],30+test1[o][2],2,2);
                 g.setColor(Color.black);
                 g.drawRect(selx1, sely2,selx2-selx1, sely1-sely2);
                 g.setColor(Color.white);
                 g.fillRect(selx1+1, sely2+1, selx2-selx1-1, sely1-sely2-1);
                 xl=selx1;
                 xr=selx2;
                 yl=sely2;
                 yr=sely1;
                 for (int o=1;o<nolin+1;o++){
                     if (zdata[o][1][zomlev]==1){
                     if ((725-test1[o][1]>xl) & (725-test1[o][1]<xr) & (30+test1[o][2]>yl) & (30+test1[o][2]<yr)){
                         g.setColor(Color.red);
                         g.fillOval(725-zerox+test1[o][1],30+zeroy-test1[o][2],2,2);
                     if ((725-test2[o][1]>xl) & (725-test2[o][1]<xr) & (30+test2[o][2]>yl) & (30+test2[o][2]<yr)){
                         g.setColor(Color.blue);
                         g.fillOval(725-zerox+test2[o][1],30+zeroy-test2[o][2],2,2);
                    if ((725-test3[o][1]>xl) & (725-test3[o][1]<xr) & (30+test3[o][2]>yl) & (30+test3[o][2]<yr)){
                    g.setColor(Color.green);
                   g.fillOval(725-zerox+test3[o][1],30+zeroy-test3[o][2],2,2);   
             sxl=xl;
             sxr=xr;
             syl=yl;
             syr=yr;
             if (window==1){ 
             //plot points depending on choosen sets of data 
             if ((cho==1) | (cho==4) | (cho==5) | (cho==7)){
             for (int o=1;o<nolin+1;o++){
                 if (zdata[o][1][zomlev]==1){ 
                 g.setColor(Color.red);
                   g.fillOval(zerox+test1[o][1],zeroy-test1[o][2],2,2);   
             if ((cho==2) | (cho==4) | (cho==6) | (cho==7)){
             for (int o=1;o<nolin+1;o++){
                 if (zdata[o][1][zomlev]==1){ 
                 g.setColor(Color.blue);
                   g.fillOval(zerox+test2[o][1],zeroy-test2[o][2],2,2);   
             if ((cho==3) | (cho==5) | (cho==6) | (cho==7)){
             for (int o=1;o<nolin+1;o++){
                 if (zdata[o][1][zomlev]==1){
                 g.setColor(Color.green);
                   g.fillOval(zerox+test3[o][1],zeroy-test3[o][2],2,2);   
        // =========  START HERE FOR SENSOR PAIRS ===============
           if (window==1){
               if ((mpress=true)| (bpre==1)){
               bpre=0;   
               h.setColor(Color.white);
               h.fillRect(0,0,661,352);
               h.setColor(Color.black);
               h.setColor(jPanel9.getBackground());
               h.fillRect(0,0,661,352);
               h.setColor(jPanel10.getBackground());
               scale=0;
               int factor=0;
               if (jRadioButton13.isSelected()==true){
                    factor=5;
                if (jRadioButton14.isSelected()==true){
                    factor=10;
                if (jRadioButton15.isSelected()==true){
                    factor=30;
                if (jRadioButton16.isSelected()==true){
                    factor=60;
               sw=(int)(factor/divfx);
               while (1+scale+sw<661){
               h.fillRect((1+scale+zerox),0,sw,352);
               scale=scale+sw+sw;
               if (1+scale<661){
               h.fillRect((1+scale+zerox),0,661,352); 
               scale=zerox;
               while (scale-sw-sw>0){
               h.fillRect((scale-sw-sw),0,sw,352);
               scale=scale-sw-sw;
               if (scale-sw>0){
               h.fillRect(0,0,scale-sw,352); 
               //if (1+scale<661){
               //h.fillRect(scale,0,660,351);  
               h.setColor(Color.black);
               h.drawLine(0,zeroy,661,zeroy); //  y zero axis
               h.drawLine(zerox,0,zerox,381); // x zero axis
             int psize=jSlider1.getValue();
             if ((cho==1) | (cho==4) | (cho==5) | (cho==7)){
              GetCols(6);
                 for (int o=1;o<nolin+1;o++){
                 if (zdata[o][1][zomlev]==1){
                   zdata[o][1][zomlev+1]=0; 
                   h.fillRect(zerox+test1[o][1],zeroy-test1[o][2],psize,psize);   
             if ((cho==2) | (cho==4) | (cho==6) | (cho==7)){
                  GetCols(7);
                 for (int o=1;o<nolin+1;o++){
                 if (zdata[o][1][zomlev]==1){ 
                   zdata[o][1][zomlev+1]=0;
                   h.fillRect(zerox+test2[o][1],zeroy-test2[o][2],psize,psize);   
             if ((cho==3) | (cho==5) | (cho==6) | (cho==7)){
             GetCols(8);
                 for (int o=1;o<nolin+1;o++){
                 if (zdata[o][1][zomlev]==1){
                   zdata[o][1][zomlev+1]=0;
                   h.fillRect(zerox+test3[o][1],zeroy-test3[o][2],psize,psize);   
               xl=0;
               xr=0;
               yl=0;
               yr=0;
                g.drawImage(Buffer,75,30,this);
             if (mdrag==true){
               g.drawImage(Buffer,75,30,this);
                  g.setColor(Color.black);
                  l.setColor (Color.black);
                  l.setStroke(dashed);
                 //bottom right
                 if ((selx1 < selx2) & (sely1 < sely2)){
                 //g.setColor(Color.black);     
                 //g.drawRect(selx1, sely1, selx2-selx1, sely2-sely1);
                  g.drawRect(selx1, sely1,selx2-selx1, sely2-sely1);
                  //l.draw(rectangle);
                 xl=selx1;
                 xr=selx2;
                 yl=sely1;
                 yr=sely2;
                 //bottom left
                 if ((selx1 > selx2) & (sely1 < sely2)){
                 g.drawRect(selx2, sely1,selx1-selx2, sely2-sely1);
                 xl=selx2;
                 xr=selx1;
                 yl=sely1;
                 yr=sely2;
                 //top left
                 if ((selx1 > selx2) & (sely1 > sely2)){
                 g.drawRect(selx2, sely2,selx1-selx2, sely1-sely2);
                 xl=selx2;
                 xr=selx1;
                 yl=sely2;
                 yr=sely1;
                 //top right
                  if ((selx1 < selx2) & (sely1 > sely2)){
                 g.drawRect(selx1, sely2,selx2-selx1, sely1-sely2);
                 xl=selx1;
                 xr=selx2;
                 yl=sely2;
                 yr=sely1;
             int psize=jSlider1.getValue();
             if ((cho==1) | (cho==4) | (cho==5) | (cho==7)){
              GetCols(9);
                 for (int o=1;o<nolin+1;o++){
                 if (zdata[o][1][zomlev]==1){
                     if ((75+zerox+test1[o][1]>xl) & (75+zerox+test1[o][1]<xr) & (30+zeroy-test1[o][2]>yl) & (30+zeroy-test1[o][2]<yr)){
                     g.fillRect(75+zerox+test1[o][1],30+zeroy-test1[o][2],psize,psize);   
                     zdata[o][1][zomlev+1]=1;
             if ((cho==2) | (cho==4) | (cho==6) | (cho==7)){
                  GetCols(12);
                 for (int o=1;o<nolin+1;o++){
                 if (zdata[o][1][zomlev]==1){ 
                 if ((75+zerox+test2[o][1]>xl) & (75+zerox+test2[o][1]<xr) & (30+zeroy-test2[o][2]>yl) & (30+zeroy-test2[o][2]<yr)){
                   g.fillRect(75+zerox+test2[o][1],30+zeroy-test2[o][2],psize,psize);   
               zdata[o][1][zomlev+1]=1;
             if ((cho==3) | (cho==5) | (cho==6) | (cho==7)){
             GetCols(13);
                 for (int o=1;o<nolin+1;o++){
                 if (zdata[o][1][zomlev]==1){
                 if ((75+zerox+test3[o][1]>xl) & (75+zerox+test3[o][1]<xr) & (30+zeroy-test3[o][2]>yl) & (30+zeroy-test3[o][2]<yr)){
                   g.fillRect(75+zerox+test3[o][1],30+zeroy-test3[o][2],psize,psize);   
                 zdata[o][1][zomlev+1]=1;
             sxl=xl;
             sxr=xr;
             syl=yl;
             syr=yr;
             l.setStroke(stroke);
           // =========  START HERE FOR PHASE ===============
           if (window==2){
               if ((mpress=true)| (bpre==1)){
               bpre=0;   
               h.setColor(Color.white);
               h.fillRect(0,0,661,352);
               h.setColor(jPanel9.getBackground());
               h.fillRect(1,0,165,351);
               h.fillRect(331,0,165,351);
               h.setColor(jPanel10.getBackground());
               h.fillRect(166,0,165,351);
               h.fillRect(496,0,165,351);
               h.setColor(Color.black);
               h.drawLine(0,0,0,351); // draw graph axis
               h.drawLine(0,351,661,351);
               GetCols(11);
               int psize=jSlider9.getValue();
                 for (int o=1;o<nolin+1;o++){
                     if (zdata[o][1][zomlev]==1){
                         zdata[o][1][zomlev+1]=0;
                         h.fillRect(660-test1[o][1],0+test1[o][2],psize,psize);
               xl=0;
               xr=0;
               yl=0;
               yr=0;
                g.drawImage(Buffer,75,30,this);
             if (mdrag==true){
               g.drawImage(Buffer,75,30,this);
                  g.setColor(Color.black);
                  l.setColor (Color.black);
                  l.setStroke(dashed);
                 //bottom right
                 if ((selx1 < selx2) & (sely1 < sely2)){
                 //g.setColor(Color.black);     
                 //g.drawRect(selx1, sely1, selx2-selx1, sely2-sely1);
                  g.drawRect(selx1, sely1,selx2-selx1, sely2-sely1);
                  //l.draw(rectangle);
                 xl=selx1;
                 xr=selx2;
                 yl=sely1;
                 yr=sely2;
                 //bottom left
                 if ((selx1 > selx2) & (sely1 < sely2)){
                 g.drawRect(selx2, sely1,selx1-selx2, sely2-sely1);
                 xl=selx2;
                 xr=selx1;
                 yl=sely1;
                 yr=sely2;
                 //top left
                 if ((selx1 > selx2) & (sely1 > sely2)){
                 g.drawRect(selx2, sely2,selx1-selx2, sely1-sely2);
                 xl=selx2;
                 xr=selx1;
                 yl=sely2;
                 yr=sely1;
                 //top right
                  if ((selx1 < selx2) & (sely1 > sely2)){
                 g.drawRect(selx1, sely2,selx2-selx1, sely1-sely2);
                 xl=selx1;
                 xr=selx2;
                 yl=sely2;
                 yr=sely1;
                 GetCols(10);
                 int psize=jSlider9.getValue();
                  for (int o=1;o<nolin+1;o++){
                     if (zdata[o][1][zomlev]==1){
                     if ((735-test1[o][1]>xl) & (735-test1[o][1]<xr) & (30+test1[o][2]>yl) & (30+test1[o][2]<yr)){               
                         g.fillRect(735-test1[o][1],30+test1[o][2],psize,psize);
                         zdata[o][1][zomlev+1]=1;
             sxl=xl;
             sxr=xr;
             syl=yl;
             syr=yr;
             l.setStroke(stroke);
    public void drawscales(){
        Font font1 = new Font("TW Cen MT", Font.BOLD,12);
        Font font2 = new Font("TW Cen MT",Font.PLAIN,10);
        Font font3 = new Font("TW Cen MT",Font.BOLD,10);
        g.setColor(Color.white);
        g.fillRect(2,2, 764, 409);
        g.setColor(Color.black); 
        if (window==0){
               g.setFont(font2);
               g.drawString(twofours,52,400);
               String minyS=String.valueOf(miny);
               g.drawString(minyS,50,383);
               g.drawString(twofoure,712,400);
               String maxyS=String.valueOf(may);
               g.drawString(maxyS,50,33);
               g.setFont(font1);
               g.drawString("Energy againt Time Graph",330,20);
               g.setFont(font3);
               g.drawString("Peak Energy",10,205);
               g.drawString("/ pJ",30,217);
               g.drawString("Time of Signal",370,400);  
               /*h.setColor(Color.black);
               h.drawLine(0,0,0,351); // draw graph axis
               h.drawLine(0,351,661,351);
               h.setColor(jPanel9.getBackground());
               h.fillRect(1,0,660,351);
               h.setColor(jPanel10.getBackground());
                while (1+scale+sw<661){
                h.fillRect((1+scale),0,sw,351);
                scale=scale+sw+sw;
               if (1+scale<661){
                h.fillRect(scale,0,660,351);  
               for (int o=1;o<nolin+1;o++){
                   GetCols(5);
                     if (zdata[o][1][zomlev]==1){
                         int psize=jSlider8.getValue();
                         h.fillRect(660-test1[o][1],0+test1[o][2],psize,psize);
               //g.drawImage(Buffer,75,30,this);
               sma.drawImage(smallpow,1,1,jPanel2);
               double ndf =((zmpmr-zmpml)/219);
               double ndyf=((zmpmt-zmpmb)/116);
               int wdp=(int)((dmax-dmix)/ndf);
               int htp=(int)((may-miy)/ndyf);
               int sx=(int)((dmix-zmpml)/ndf);
               int sy=(int)((zmpmt-may)/ndyf);
               sma.drawRect(1+sx,1+sy,wdp,htp);
        if (window==1){
               g.setColor(Color.black);
               g.setFont(font2);
               String minxS="0";
               g.drawString(minxS,zerox+72,395);
               String minyS="0";
               g.drawString(minyS,65,zeroy+34);
               g.setFont(font3);
               g.drawString("Log 10 R /",9,200);
               g.drawString("Ratio of",13,212);
               g.drawString("Energy",13,224);
               g.drawString("Difference in Time of Flight / Seconds",315,408);
               g.setFont(font1);
               g.drawString("Sensor Pairs Graph",350,20);
              /* h.setColor(Color.white);
               h.fillRect(0,0,661,352);
               h.setColor(Color.black);
               h.setColor(new java.awt.Color(240, 240, 255));
               h.fillRect(0,0,661,352);
               h.setColor(new java.awt.Color(220, 220, 255));
               scale=0;
               sw=(int)(10/divfx);
               while (1+scale+sw<661){
               h.fillRect((1+scale+zerox),0,sw,352);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              

    Thanks,
    I have tried it using
    public void paintComponent(Graphics g){
    drawStuff();
    }in the main body of code and then call upon PaintComponent when I close the settings frame but it makes no difference, I still get the big white box as shown in Q3 - I've tried reading the problem solving section for swing graphics from sun but to no avail.
    Any advice on where paintcomponent should be placed / called?
    Any ideas would be great.
    Thanks,
    John

  • Loaded image on jPanel won't disappear

    Gents, I have an image on a panel that I am drawing over with another image,
    the problem is the new image will not appear because the previous image
    keeps being displayed. there must be a way to completly erase the previous
    image, thanks for any help.....
    Jacques
    This is the code that I am using to draw the image:
    class ImagePanel extends JPanel
    Image imageX;
    int i;
    int IMAGE_ID;
    public ImagePanel (Image ImageX, int myInt, int IMAGE_ID)
    imageX = theImage;
    i = myInt;
    public void setImage (Image myImage)
    imageX = myImage;
    repaint(300);
    public void paint (Graphics g)
    super.paint(g);
    if (theTracker.checkID(IMAGE_ID, true))
    g.drawImage(imageX, 0, 0, getSize().width, getSize().height, this);
    if (i == 0) //i = 0 draw text labels on tank image not on FD Logo
    //g.drawString(elevz1.getText(), 20, 45);
    //g.drawString(atmphpress.getText(), 78, 13);
    //g.drawString(pressh.getText(), 68, 48);
    //g.drawString(vappress.getText(), 55, 119);
    //g.drawString(frictf.getText(), 115, 119);
    else
    g.drawString("Loading image: please wait..", 10, 50);
    repaint(300);
    } //end class

    hi.
    you might want to override the paintComponent() method
    this method is automatically called every time the JPanel component needs to redraw itself.

  • JPanel's Graphics disappears!!

    When calling jpanel.getGraphics().drawLine(...), for example, the line is drawn on a panel.
    But when another window is dragged over this panel, the graphics is erased(it is in memory, but it's not shown on a panel).
    How to avoid this problem?
    (Someone said i should use TimerTask for re-draw the content every some period[i], but i think it's not the best idea :)))
    Thanks in advance!!

    Hello,
    If you want the line to be persistent in your JPanel, you should draw it in the paint method of your subclassed JPanel. Otherwise, there is no reason why it should be repainted on the panel's repaint.

  • Need some help in making a label disappear for certain time

    hi
    i need to make a program for my java class
    this is the question
    Design and implement a memory game. In this game you show the user a sequence of
    random integer numbers in the range of 0 to 99, and then ask him to type these
    numbers. Display how many he has remembered as a score. The level of game depends
    on how many numbers are there in the sequence. The duration of the display for the
    sequence is the speed. The user can adjust the level and the speed.
    my question is how to view the numbers for the game and make them disappear after certain time
    in need help with the syntax for it
    this is what i made so far
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import java.awt.BorderLayout;
    import java.awt.GridLayout;
    import java.awt.FlowLayout;
    import java.awt.Color;
    import javax.swing.JButton;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.util.*;
    import java.io.*;
    import javax.swing.*;
    import java.awt.*;
    class game extends JFrame implements ActionListener {
    public static final int WIDTH = 500;
    public static final int HEIGHT = 300;
    public static final int x = 3;
    public static final int y = 3000;
    public static void main(String[] args) {
    game gui = new game( );
    gui.setVisible(true);
    public game( )
    super("MEMORY GAME");
    setSize(WIDTH, HEIGHT);
    setDefaultCloseOperation(JFrame.EXIT_ON_…
    setLayout(new BorderLayout( ));
    JPanel buttonPanel = new JPanel( );
    buttonPanel.setBackground(Color.LIGHT_GR…
    buttonPanel.setLayout(new FlowLayout( ));
    JButton b1 = new JButton("MAKE IT HAREDER!!!");
    b1.addActionListener(this);
    buttonPanel.add(b1);
    JButton b2 = new JButton("MAKE IT FASTER!!!");
    b2.addActionListener(this);
    buttonPanel.add(b2);
    add(buttonPanel, BorderLayout.SOUTH);
    public void actionPerformed(ActionEvent e)
    String buttonString = e.getActionCommand( );
    if (buttonString.equals("MAKE IT HAREDER!!!"))
    else if (buttonString.equals("MAKE IT FASTER!!!"))
    }

    To post code, use the code tags -- [code]Your Code[/code] will display asYour CodeOr use the code button above the editing area and paste your code between the {code}{code} tags it generates.
    I strongly suggest you forget about the GUI part and first make a console application that fulfils the game specifications. If you have problems with this, you can post your questions in the New to Java section of these forums.
    db

  • Why do the menus disappear?

    I am quite new to Java and I use project builder on my mac running OSX. Anyway I found some sample code and I was wondering if anybody could help me out. Basically when the app is compiled it draws "Hello World" in the window and there are two menus in the menubar -> 'File' and 'Edit', but when you go to the about box the menus disappear until you either close the aboutbox or bring the focus back to the main window.
    How can I always keep the menus showing when there are multiple windows in the app. Remember that mac menus are fixed to the top of the screen...
    anyway heres the code:
    import java.awt.*;
    import java.awt.event.*;
    import com.apple.mrj.*;
    import javax.swing.*;
    public class test extends JFrame
                          implements  ActionListener,
                                      MRJAboutHandler,
                                      MRJQuitHandler
        static final String message = "Hello World!";
        private Font font = new Font("serif", Font.ITALIC+Font.BOLD, 36);
        protected AboutBox aboutBox;
        // Declarations for menus
        static final JMenuBar mainMenuBar = new JMenuBar();
        static final JMenu fileMenu = new JMenu("File");
        protected JMenuItem miNew;
        protected JMenuItem miOpen;
        protected JMenuItem miClose;
        protected JMenuItem miSave;
        protected JMenuItem miSaveAs;
        static final JMenu editMenu = new JMenu("Edit");
        protected JMenuItem miUndo;
        protected JMenuItem miCut;
        protected JMenuItem miCopy;
        protected JMenuItem miPaste;
        protected JMenuItem miClear;
        protected JMenuItem miSelectAll;
        public void addFileMenuItems() {
            miNew = new JMenuItem ("New");
            miNew.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.Event.META_MASK));
            fileMenu.add(miNew).setEnabled(true);
            miNew.addActionListener(this);
            miOpen = new JMenuItem ("Open...");
            miOpen.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.Event.META_MASK));
            fileMenu.add(miOpen).setEnabled(true);
            miOpen.addActionListener(this);
            miClose = new JMenuItem ("Close");
            miClose.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_W, java.awt.Event.META_MASK));
            fileMenu.add(miClose).setEnabled(true);
            miClose.addActionListener(this);
            miSave = new JMenuItem ("Save");
            miSave.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.Event.META_MASK));
            fileMenu.add(miSave).setEnabled(true);
            miSave.addActionListener(this);
            miSaveAs = new JMenuItem ("Save As...");
            fileMenu.add(miSaveAs).setEnabled(true);
            miSaveAs.addActionListener(this);
            mainMenuBar.add(fileMenu);
        public void addEditMenuItems() {
            miUndo = new JMenuItem("Undo");
            miUndo.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Z, java.awt.Event.META_MASK));
            editMenu.add(miUndo).setEnabled(true);
            miUndo.addActionListener(this);
            editMenu.addSeparator();
            miCut = new JMenuItem("Cut");
            miCut.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, java.awt.Event.META_MASK));
            editMenu.add(miCut).setEnabled(true);
            miCut.addActionListener(this);
            miCopy = new JMenuItem("Copy");
            miCopy.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.Event.META_MASK));
            editMenu.add(miCopy).setEnabled(true);
            miCopy.addActionListener(this);
            miPaste = new JMenuItem("Paste");
            miPaste.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_V, java.awt.Event.META_MASK));
            editMenu.add(miPaste).setEnabled(true);
            miPaste.addActionListener(this);
            miClear = new JMenuItem("Clear");
            editMenu.add(miClear).setEnabled(true);
            miClear.addActionListener(this);
            editMenu.addSeparator();
            miSelectAll = new JMenuItem("Select All");
            miSelectAll.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, java.awt.Event.META_MASK));
            editMenu.add(miSelectAll).setEnabled(true);
            miSelectAll.addActionListener(this);
            mainMenuBar.add(editMenu);
        public void addMenus() {
            addFileMenuItems();
            addEditMenuItems();
            setJMenuBar (mainMenuBar);
        public test() {
            super("test");
            this.getContentPane().setLayout(null);
            addMenus();
            aboutBox = new AboutBox();
            Toolkit.getDefaultToolkit();
            MRJApplicationUtils.registerAboutHandler(this);
            MRJApplicationUtils.registerQuitHandler(this);
            setVisible(true);
        public void paint(Graphics g) {
              super.paint(g);
            g.setColor(Color.blue);
            g.setFont (font);
            g.drawString(message, 40, 80);
        public void handleAbout() {
            aboutBox.setResizable(false);
            aboutBox.setVisible(true);
            aboutBox.show();
        public void handleQuit() {     
            System.exit(0);
        // ActionListener interface (for menus)
        public void actionPerformed(ActionEvent newEvent) {
            if (newEvent.getActionCommand().equals(miNew.getActionCommand())) doNew();
            else if (newEvent.getActionCommand().equals(miOpen.getActionCommand())) doOpen();
            else if (newEvent.getActionCommand().equals(miClose.getActionCommand())) doClose();
            else if (newEvent.getActionCommand().equals(miSave.getActionCommand())) doSave();
            else if (newEvent.getActionCommand().equals(miSaveAs.getActionCommand())) doSaveAs();
            else if (newEvent.getActionCommand().equals(miUndo.getActionCommand())) doUndo();
            else if (newEvent.getActionCommand().equals(miCut.getActionCommand())) doCut();
            else if (newEvent.getActionCommand().equals(miCopy.getActionCommand())) doCopy();
            else if (newEvent.getActionCommand().equals(miPaste.getActionCommand())) doPaste();
            else if (newEvent.getActionCommand().equals(miClear.getActionCommand())) doClear();
            else if (newEvent.getActionCommand().equals(miSelectAll.getActionCommand())) doSelectAll();
        public void doNew() {}
        public void doOpen() {}
        public void doClose() {}
        public void doSave() {}
        public void doSaveAs() {}
        public void doUndo() {}
        public void doCut() {}
        public void doCopy() {}
        public void doPaste() {}
        public void doClear() {}
        public void doSelectAll() {}
        public static void main(String args[]) {
            new test();
    }������������������������������������������
    and the coe of the aboutbox
    ������������������������������������������
    //     File:     AboutBox.java
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class AboutBox extends JFrame
                          implements ActionListener
        protected JButton okButton;
        protected JLabel aboutText;
        public AboutBox() {
         super();
            this.getContentPane().setLayout(new BorderLayout(15, 15));
            this.setFont(new Font ("SansSerif", Font.BOLD, 14));
            aboutText = new JLabel ("About test");
            JPanel textPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 15, 15));
            textPanel.add(aboutText);
            this.getContentPane().add (textPanel, BorderLayout.NORTH);
            okButton = new JButton("OK");
            JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 15, 15));
            buttonPanel.add (okButton);
            okButton.addActionListener(this);
            this.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
            this.pack();
        public void actionPerformed(ActionEvent newEvent) {
            setVisible(false);
    }thanks

    please anyone wanna help me?

  • Adding JMenu directly to a JPanel

    I am trying to add a JMenu directly to a JPanel, but it isn't functioning properly. It shows up, and looks the way I expect (with an arrow, like a sub-menu), but it doesn't display its popup menu when clicked. If I add the menu to a JMenuBar, and add that to the panel, it works, but the popup drops down below the button instead of to the right, and the arrow disappears. JMenuBar appears to be adding its own actions to every menu that it contains. JMenu is a descendant of AbstractButton, so I figured it would behave like one, but no luck.
    Any help would be appreciated. By the way, I have thought of using a JButton and a JPopupMenu together, but that is not a very elegant solution. I am looking for a way to make the JMenu behave as if it were in a JMenuBar.

    Hello Vijesh
    See the code below and tell me whether it is useful ?
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    class SwingA implements ActionListener
    JPopupMenu pop;
    JFrame frame;
    JPanel panel;
    JButton cmdPop;
         public static void main(String[] args)
         SwingA A=new SwingA();
         SwingA()
                   frame=new JFrame("PopUp");
                   frame.setSize(600,480);
                   frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
                   pop=new JPopupMenu();
                   cmdPop=new JButton("Click");
                   cmdPop.addActionListener(this);
    JMenuItem item = new JMenuItem("First");
    JMenuItem item1 = new JMenuItem("Second");
    pop.add(item);
    pop.add(item1);
                   panel=new JPanel();
                   panel.add(cmdPop);
                   frame.getContentPane().add(panel);
                   frame.setVisible(true);
         public void actionPerformed(ActionEvent source)
    pop.show(cmdPop, cmdPop.getWidth(), 0);
    kanad

  • Table header disappearing

    I've been scratching my heads for hours and still can't understand why this is happening. I have two JDialog. One to display the table, let's call it Dialog 1, and the other to display the row details, Dialog 2. When Dialog 2 displays on top of the table headers and you close the dialog, the column headers seems to disappear. I've checked and rechecked the code to see if there's anything funky but I can't see any problems with this. The code is displayed below.
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Dialog;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import javax.swing.BorderFactory;
    import javax.swing.ButtonGroup;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JCheckBox;
    import javax.swing.JComboBox;
    import javax.swing.JDialog;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JRadioButton;
    import javax.swing.JScrollPane;
    import javax.swing.JSeparator;
    import javax.swing.JTable;
    import javax.swing.JTextField;
    import javax.swing.JToolBar;
    import javax.swing.ListSelectionModel;
    import javax.swing.event.ListSelectionEvent;
    import javax.swing.event.ListSelectionListener;
    * Created on Mar 2, 2004
    * To change the template for this generated file go to
    * Window>Preferences>Java>Code Generation>Code and Comments
    * @author Administrator
    * To change the template for this generated type comment go to
    * Window>Preferences>Java>Code Generation>Code and Comments
    class PaymentTypeSetupDialog extends JDialog {
         private JTable paymentTable;
         private PaymentTypeListTableModel paymentModel;
         private JTextField lookup;
         private JRadioButton byCode, byAcctCode, byPaymentType;
         private ButtonGroup group;
         private JButton add, edit, delete;
         public PaymentTypeSetupDialog(JFrame parent) {
              super(parent, "Payment Setup", true);
              initialize();
              setSize(new Dimension(600, 400));
              setResizable(false);
              setVisible(true);
         public PaymentTypeSetupDialog(Dialog parent) {
              super(parent, "Payment Setup", true);
              initialize();
              setSize(new Dimension(600, 400));
              setResizable(false);
              setVisible(true);
         private void initialize() {
              Container pane = getContentPane();
              pane.add(paymentCtrls(), BorderLayout.NORTH);
              pane.add(paymentList(), BorderLayout.CENTER);
              pane.add(paymentLookup(), BorderLayout.SOUTH);
         private JToolBar paymentCtrls() {
              final JSeparator sep = new JSeparator(JSeparator.VERTICAL);
              sep.setPreferredSize(new Dimension(20, 20));
              JToolBar bar = new JToolBar();
              bar.setLayout(new FlowLayout(FlowLayout.LEFT, 4, 4));
              bar.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.GRAY));
              bar.setFloatable(false);          
              bar.setRollover(true);
              add = new JButton("Add", new ImageIcon("images/Add16.gif"));
              add.addActionListener(new ActionListener() {
                   public void actionPerformed(ActionEvent e) {
                        PaymentType payment = new PaymentType();
                        PaymentDetails dialog = new PaymentDetails(PaymentTypeSetupDialog.this, payment, true);
                        if (!payment.getPaymentID().equals("")) {
                             insertTableRow(payment);
                   private void insertTableRow(PaymentType payment) {
                        int index = paymentTable.getSelectedRow();
                        int size = paymentTable.getRowCount();
                        ArrayList data = new ArrayList();
                        data.add(new Boolean(payment.isEnabled()));
                        data.add(payment.getPaymentID());
                        data.add(payment.getAccountID());
                        data.add(payment.getPaymentType());
                        // if no selection or if item in last position is selected.
                        // add the new one to the end of the list, and select new one.
                        if (index == -1 || index+1 == size) {
                             paymentModel.addNewRow(size, data);
                             paymentTable.getSelectionModel().setSelectionInterval(size, size);
                        } else {
                             paymentModel.addNewRow(index, data);
                             paymentTable.getSelectionModel().setSelectionInterval(index+1, index+1);
              add.setEnabled(true);          
              add.setFocusable(false);
              bar.add(add);
              edit = new JButton("Edit", new ImageIcon("images/Edit16.gif"));
              edit.addActionListener(new ActionListener() {
                   public void actionPerformed(ActionEvent e) {
                        int row = paymentTable.getSelectedRow();
                        final PaymentType payment = new PaymentType();
                        payment.setPaymentID(paymentTable.getValueAt(row, 1).toString());
                        try {
                             DBPaymentType.getPaymentType(payment);
                        } catch (SQLException sqle) {
                             // TODO Auto-generated catch block
                             sqle.printStackTrace();
                        PaymentDetails dialog = new PaymentDetails(PaymentTypeSetupDialog.this, payment, false);
                        if (!payment.getPaymentID().equals("")) {
                             ArrayList data = new ArrayList();
                             data.add(new Boolean(payment.isEnabled()));
                             data.add(payment.getPaymentID());
                             data.add(payment.getAccountID());
                             data.add(payment.getPaymentType());
                             paymentModel.updateRow(row, data);
              edit.setEnabled(false);          
              add.setFocusable(false);
              bar.add(edit);
              delete = new JButton("Delete", new ImageIcon("images/Delete16.gif"));
              delete.addActionListener(new ActionListener() {
                   public void actionPerformed(ActionEvent e) {
                        int selection = JOptionPane.showConfirmDialog(PaymentTypeSetupDialog.this, "Are you sure you want to remove this payment type?");
                        if (selection == 0) {
                             int row = paymentTable.getSelectedRow();
                             int size = paymentTable.getRowCount();     
                             if (size == 0) { // Nobody left
                                  edit.setEnabled(false);
                                  delete.setEnabled(false);
                             } else {
                                  PaymentType payment = new PaymentType();
                                  payment.setPaymentID(paymentTable.getValueAt(row, 1).toString());
                                  try {
                                       DBPaymentType.removePaymentType(payment);
                                       paymentModel.removeRow(row);
                                       if (row+1 == size) {                              
                                            row--;
                                       paymentTable.getSelectionModel().setSelectionInterval(row, row);
                                  } catch (SQLException e1) {
                                       // TODO Auto-generated catch block
                                       e1.printStackTrace();
              delete.setEnabled(false);
              delete.setFocusable(false);
              bar.add(delete);
              bar.add(sep);
              JButton btn = new JButton("Exit", new ImageIcon("images/Stop16.gif"));
              btn.addActionListener(new ActionListener() {
                   public void actionPerformed(ActionEvent e) {
                        dispose();
              btn.setFocusable(false);
              bar.add(btn);
              return bar;
         private JScrollPane paymentList() {
              paymentModel = new PaymentTypeListTableModel();
              paymentTable = new JTable(paymentModel);
              paymentTable.setFocusable(false);
              paymentTable.setCellSelectionEnabled(false);
              paymentTable.setRowHeight(20);
              paymentTable.setSelectionBackground(Color.yellow);
              paymentTable.setSelectionForeground(Color.black);
              paymentTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
              paymentTable.getTableHeader().setReorderingAllowed(false);
              paymentTable.getTableHeader().setResizingAllowed(false);
              paymentTable.getColumnModel().getColumn(0).setPreferredWidth(50);     
              paymentTable.getColumnModel().getColumn(1).setPreferredWidth(50);
              paymentTable.getColumnModel().getColumn(2).setPreferredWidth(50);
              paymentTable.getColumnModel().getColumn(3).setPreferredWidth(200);
              paymentTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
                   public void valueChanged(ListSelectionEvent e) {
                        if (paymentTable.getRowCount() > 0) {
                             if (!edit.isEnabled())
                                  edit.setEnabled(true);
                             if (!delete.isEnabled())
                                  delete.setEnabled(true);
                        } else {
                             edit.setEnabled(false);
                             delete.setEnabled(false);
              paymentTable.addMouseListener(new MouseAdapter() {
                   public void mouseClicked(MouseEvent e) {
                        if (e.getClickCount() == 2 && e.getButton() == 1) {
                             int row = paymentTable.getSelectedRow();
                             final PaymentType payment = new PaymentType();
                             payment.setPaymentID(paymentTable.getValueAt(row, 1).toString());
                             try {
                                  DBPaymentType.getPaymentType(payment);
                             } catch (SQLException sqle) {
                                  // TODO Auto-generated catch block
                                  sqle.printStackTrace();
                             PaymentDetails dialog = new PaymentDetails(PaymentTypeSetupDialog.this, payment, false);
                             if (!payment.getPaymentID().equals("")) {
                                  ArrayList data = new ArrayList();
                                  data.add(new Boolean(payment.isEnabled()));
                                  data.add(payment.getPaymentID());
                                  data.add(payment.getAccountID());
                                  data.add(payment.getPaymentType());
                                  paymentModel.updateRow(row, data);
              JScrollPane scroll = new JScrollPane(paymentTable, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,       
                   JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
              scroll.setCorner(JScrollPane.UPPER_RIGHT_CORNER,paymentTable.getTableHeader());
              scroll.getViewport().setBackground(Color.white);
              return scroll;
         private JPanel paymentLookup() {
              JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT, 2, 2));
              panel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
              group = new ButtonGroup();
              panel.add(new JLabel("Search"));
              lookup = new JTextField(20);
              lookup.addActionListener(new ActionListener() {
                   public void actionPerformed(ActionEvent e) {
                        if (lookup.getText().equals("")) {
                             JOptionPane.showMessageDialog(PaymentTypeSetupDialog.this, "Look up value is empty.");
                        } else {
                             int col;
                             if (byCode.isSelected()) {
                                  col = 1;
                             } else if (byAcctCode.isSelected()) {
                                  col = 2;
                             } else {
                                  col = 3;
                             int index = paymentModel.getIndexFor(col, lookup.getText());
                             if (index != -1) {
                                  paymentTable.getSelectionModel().setSelectionInterval(index, index);
              panel.add(lookup);
              byCode = new JRadioButton("Code");
              byCode.setSelected(true);
              byCode.setFocusable(false);
              group.add(byCode);
              panel.add(byCode);
              byAcctCode = new JRadioButton("Account code");
              byAcctCode.setSelected(true);
              byAcctCode.setFocusable(false);
              group.add(byAcctCode);
              panel.add(byAcctCode);
              byPaymentType = new JRadioButton("Payment type");
              byPaymentType.setFocusable(false);
              group.add(byPaymentType);
              panel.add(byPaymentType);
              return panel;
         public void setVisible(boolean visible) {
              if (visible) {
                   Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
                   Dimension size = getSize();
                   int x;
                   int y;
                   x = screenSize.width / 2 - size.width / 2;
                   y = screenSize.height / 2 - size.height / 2;
                   setBounds(x, y, size.width, size.height);               
              super.setVisible(visible);
         class PaymentDetails extends JDialog {
              private PaymentType payment;
              private boolean newPayment;
              private JTextField code, type, image, imageUrl;
              private JComboBox accountList;
              private JCheckBox isEnabled;
              private JLabel paymentImage;
              public PaymentDetails(Dialog parent, PaymentType payment, boolean newPayment) {
                   super(parent, "Payment Details", false);
                   this.payment = payment;
                   this.newPayment = newPayment;
                   initialize();
                   pack();
                   setVisible(true);
              private void initialize() {
                   Container pane = getContentPane();
                   pane.add(paymentDetails(), BorderLayout.CENTER);
                   pane.add(detailCtrls(), BorderLayout.SOUTH);
              private JPanel paymentDetails() {
                   JPanel panel = new JPanel(new GridBagLayout());
                   panel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
                   GridBagConstraints gbc = new GridBagConstraints();
                   gbc.anchor = GridBagConstraints.WEST;
                   gbc.fill = GridBagConstraints.NONE;
                   gbc.insets = new Insets(4, 2, 4, 2);
                   JLabel label;
                   gbc.gridx = 0;
                   gbc.gridy = 0;
                   gbc.weightx = 0;
                   label = new JLabel("Code");
                   panel.add(label, gbc);
                   gbc.gridx++;
                   gbc.weightx = 1;
                   code = new JTextField(payment.getPaymentID(), 10);
                   code.setEditable(newPayment);
                   panel.add(code, gbc);
                   gbc.gridx++;
                   isEnabled = new JCheckBox("Activate", payment.isEnabled());
                   panel.add(isEnabled, gbc);
                   gbc.gridx = 0;
                   gbc.gridy++;
                   gbc.fill = GridBagConstraints.HORIZONTAL;
                   label = new JLabel("Account code");
                   panel.add(label, gbc);
                   gbc.gridx++;
                   gbc.weightx = 1;
                   gbc.gridwidth = 2;
                   accountList = new JComboBox(new AccountTypeListModel(payment.getAccountID()));
                   panel.add(accountList, gbc);
                   gbc.gridx = 0;
                   gbc.gridy++;
                   gbc.gridwidth = 1;
                   label = new JLabel("Payment Type");
                   panel.add(label, gbc);
                   gbc.gridx++;
                   gbc.weightx = 1;
                   gbc.gridwidth = 2;
                   type = new JTextField(payment.getPaymentType(), 20);
                   panel.add(type, gbc);
                   gbc.gridx = 0;
                   gbc.gridy++;
                   gbc.gridwidth = 1;
                   label = new JLabel("Image");
                   panel.add(label, gbc);
                   gbc.gridx++;
                   gbc.weightx = 1;
                   gbc.gridwidth = 2;
                   panel.add(paymentImage(), gbc);
                   gbc.gridx = 0;
                   gbc.gridy++;
                   gbc.gridwidth = 1;
                   label = new JLabel("Image URL");
                   panel.add(label, gbc);
                   gbc.gridx++;
                   gbc.fill = GridBagConstraints.NONE;
                   imageUrl = new JTextField(payment.getImage(), 10);
                   panel.add(imageUrl, gbc);
                   gbc.gridx++;
                   JButton btn = new JButton("Browse");
                   btn.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                             System.out.println("Browse image directory");
                   panel.add(btn, gbc);
                   return panel;
              private JScrollPane paymentImage() {
                   paymentImage = new JLabel(new ImageIcon("images/Category48.gif"));
                   JScrollPane scroll = new JScrollPane(paymentImage, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,       
                        JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
                   scroll.setCorner(JScrollPane.UPPER_RIGHT_CORNER,paymentTable.getTableHeader());
                   scroll.getViewport().setBackground(Color.white);
                   scroll.setPreferredSize(new Dimension(200, 200));
                   return scroll;
              private JPanel detailCtrls() {
                   JPanel panel = new JPanel();
                   JButton btn = new JButton("Save");
                   btn.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                             if (code.getText().equals("")) {
                                  JOptionPane.showMessageDialog(PaymentDetails.this, "Code cannot be empty.");
                             } else {
                                  payment.setPaymentID(code.getText());
                                  payment.setAccountID(((AccountType) accountList.getSelectedItem()).getAccountID());
                                  payment.setPaymentType(type.getText());
                                  payment.setImage(imageUrl.getText());
                                  payment.setEnabled(isEnabled.isSelected());
                                  if (newPayment) {
                                       try {
                                            DBPaymentType.addPaymentType(payment);
                                            dispose();
                                       } catch (SQLException e1) {
                                            // TODO Auto-generated catch block
                                            e1.printStackTrace();
                                  } else {
                                       try {
                                            DBPaymentType.updatePaymentType(payment);
                                            dispose();
                                       } catch (SQLException e1) {
                                            // TODO Auto-generated catch block
                                            e1.printStackTrace();
                   panel.add(btn);
                   btn = new JButton("Cancel");
                   btn.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                             payment.setPaymentID("");
                             dispose();
                   panel.add(btn);
                   return panel;
              public void setVisible(boolean visible) {
                   if (visible) {
                        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
                        Dimension size = getSize();
                        int x;
                        int y;
                        x = screenSize.width / 2 - size.width / 2;
                        y = screenSize.height / 2 - size.height / 2;
                        setBounds(x, y, size.width, size.height);               
                   super.setVisible(visible);
    }Please help!
    Thx.

    Hmmm...Interesting. If you add paymentTable.updateUI(); after any edits or adds, the whole column headers disappear. It only seems to happen when the 2nd modal dialog is on top of the column header. If it is not, the application works fine. I've tested this against 1.4.2 and 1.5 beta and it i get the same result.

  • Display problem in JPanel

    Hi,
    I am facing a probelm of displaying an image in JPanel in the following code. I want to display "displayImage" in "CenterPanel as shown in the program. The problem I am facing is that the image displyed on this panel is momentairly and then it disappears. Can anyone help me with this out??
    <Code>
    //Author @ Jaikishan Jalan;
    //Date @ September 29,2005;
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.Graphics.*;
    import java.awt.image.*;
    import java.awt.image.BufferedImage;
    import javax.imageio.*;
    import java.io.*;
    import javax.swing.JOptionPane;
    import java.util.Random;
    //This is the main class to achieve the desired objective;
    public class Task3 extends JFrame{
    JFrame frame;
         public Task3(){
         frame = new JFrame("Cryptography Demo");
         //mainpanel is the panel designed to generate the layout for the main frame;
    frame.setContentPane(new mainpanel(this));
         frame.setSize(1000,700);
    frame.setVisible(true);
         frame.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
    public static void main(String args[]){
         new Task3();
    //This class corresponds to mainpanel class;
    //It contains a Label named as string, a textfield named as stringp and a button named as submit;
    //Their name clearly indicates what are they used for;
    class mainpanel extends JPanel implements ActionListener{
    JFrame temp;
    JTextField stringp;
    JPanel centerPanel;
    protected static final int b = Color.black.getRGB();
    protected static final int w = Color.white.getRGB();
    static int pixels[];
    int string_length=0;
    Image displayImage= null;
    RenderedImage originalimg=null;
    Image img1=null,img2=null,img3=null;
    public mainpanel(JFrame x){ // setLayout(new BorderLayout());
              temp = x ;
              setLayout(new BorderLayout());
              JPanel southPanel = new JPanel();
              southPanel.setBackground(Color.GRAY);
              JPanel eastPanel=new JPanel();
              centerPanel=new JPanel();
              centerPanel.setBackground(Color.lightGray);
              eastPanel.setBackground(Color.GRAY);
              eastPanel.setLayout(new BoxLayout(eastPanel, BoxLayout.Y_AXIS));
              eastPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
              JLabel string = new JLabel("String : ");
    stringp = new JTextField("Enter String",14);
    JButton submit = new JButton("Submit");
         JButton close = new JButton("Close Window");
         JButton share_1 = new JButton("Share 1");
              JButton share_2 = new JButton("Share 2");
              JButton original = new JButton("Original Image");
              JButton combine = new JButton("Combine Share");
    southPanel.add(string);
    southPanel.add(stringp);
    southPanel.add(submit);
         southPanel.add(close);
              eastPanel.add(Box.createVerticalGlue());
              eastPanel.add(original);
              eastPanel.add(Box.createRigidArea(new Dimension(0,10)));
              eastPanel.add(share_1);
              eastPanel.add(Box.createRigidArea(new Dimension(0,10)));
              eastPanel.add(share_2);
              eastPanel.add(Box.createRigidArea(new Dimension(0,10)));
              eastPanel.add(combine);
              eastPanel.add(Box.createVerticalGlue());
              eastPanel.setBorder(BorderFactory.createEtchedBorder());
              southPanel.setBorder(BorderFactory.createEtchedBorder());
              add(southPanel, BorderLayout.SOUTH);
              add(eastPanel, BorderLayout.EAST);
              centerPanel.setBorder(BorderFactory.createEtchedBorder());
         add(centerPanel, BorderLayout.CENTER);
    stringp.addActionListener(this);     
         submit.addActionListener(this);
         close.addActionListener(this);
         original.addActionListener(this);
              share_1.addActionListener(this);
              share_2.addActionListener(this);
              combine.addActionListener(this);
         setVisible(true);
              repaint();
    public void actionPerformed(ActionEvent e) {
    String str= e.getActionCommand();
         string_length = stringp.getText().length();
         if(str.equals("Submit")){
              if((string_length<4)|(string_length>14))
              {JOptionPane.showMessageDialog(null,"String size should be in between 4 and 14","Error",JOptionPane.OK_OPTION); }
              else
                   JOptionPane.showConfirmDialog(null,"Are you sure you want to submit","Confirm",JOptionPane.YES_NO_OPTION);
              if(JOptionPane.YES_OPTION==0){
                   String name = new String(stringp.getText());
                   Canvas cn = new Canvas();
              originalimg= new BufferedImage(256,256,BufferedImage.TYPE_BYTE_GRAY);
    Graphics cg = ((BufferedImage)originalimg).getGraphics();
              cg.setColor(Color.WHITE);
              cg.fillRect(0,0,256,256);
              cg.setColor(Color.BLACK);
              cg.setFont(new Font("",Font.BOLD,25));
              FontMetrics fm = cg.getFontMetrics();
              int x = (256- fm.stringWidth(name))/2;
              int y = (fm.getAscent()+(256-(fm.getAscent()+fm.getDescent()))/2);
    cg.drawString (name,x,y);
                        //Some algorthim which produces img1, img2 , img3 from "originalimg"
                             img1 = createImage(new MemoryImageSource(column*2,row,block1,0,column*2));
                             img2 = createImage(new MemoryImageSource(column*2,row,block2,0,column*2));
                             for(int t=0;t<row*column*2;t++){
                             if((block1[t] == w) & (block2[t] ==w))
                                  comb[t] = w;
                                  else comb[t] = b;
                             img3 = createImage(new MemoryImageSource(column*2,row,comb,0,column*2));
                   displayImage = (Image)originalimg;
                   repaint();     
         else if(str.equals("Close Window")){
              System.exit(0);}
         else if(str.equals("Original Image"))
         { displayImage = (Image)originalimg;
              repaint();
         else if(str.equals("Share 1")){
              displayImage = img1;
              repaint();
         else if(str.equals("Share 2")){
              displayImage = img2;
              repaint();
         else if(str.equals("Combine Share")){
              displayImage = img3;
              repaint();
    public Insets getInsets(){return new Insets(10,10,10,10);}
    public void paint(Graphics g){
         super.paint(g);
         if(displayImage != null) {
              try{ MediaTracker m = new MediaTracker(this);
                        m.addImage(displayImage,0);
                        m.waitForID(0);
              centerPanel.getGraphics().drawImage(displayImage,(centerPanel.getWidth()-displayImage.getWidth(null))/2,(centerPanel.getHeight()-displayImage.getHeight(null))/2,this);
         }catch(InterruptedException ie){System.out.println(ie);};
    }

    Do you want me to use Image Icon componenet to display the image in a JPanel? ImageIcon is not a component and cannot be directly added to a JPanel.
    I want you to:
    a) read the tutorial
    b) run the examples
    c) understand how the examples work
    d) use the information from above and make the changes to your code.

  • Disappearing Scrollbars

    Hi,
    I have a problem with the JScrollPane.
    When adding some components to a panel inside a JScrollPane whereby most components are invisible at the start and made visible by user actions, the scrollbars disappear when making components visible.
    More precisely, I always resize the whole frame when making a component visible. If the frame is too big to fit on the screen, they first appear, but when making one more component visible, they disappear again.
    Very strange is also, that when I minimize the dialog and then maximize it again, they reappear again. This sounds like a bug for me...
    Any help would be appreciated. BTW, I'm using Java 1.6.0.
    import java.awt.BorderLayout;
    import java.awt.Font;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    import java.util.List;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JToolBar;
    public class ScrollBarTest
         public static void main(String[] args)
              final JFrame frame = new JFrame("ScrollBar Test");
              frame.setLayout(new BorderLayout());
              Font font = new Font(Font.DIALOG, Font.PLAIN, 80);
              JPanel panel = new JPanel(new GridBagLayout());
              GridBagConstraints cons = new GridBagConstraints();
              cons.gridwidth = GridBagConstraints.REMAINDER;
              final List<JLabel> labels = new ArrayList<JLabel>(20);
              for (int i = 0; i < 20; i++)
                   JLabel label = new JLabel("Label " + i);
                   label.setFont(font);
                   labels.add(label);
                   if (i > 0)
                        label.setVisible(false);
                   panel.add(label, cons);
              frame.add(new JScrollPane(panel), BorderLayout.CENTER);
              JToolBar toolbar = new JToolBar();
              JButton button = new JButton("Klick");
              button.addActionListener(new ActionListener()
                   int     firstInvisible     = 1;
                   @Override
                   public void actionPerformed(ActionEvent e)
                        labels.get(firstInvisible).setVisible(true);
                        firstInvisible++;
                        frame.pack();
              toolbar.add(button);
              frame.add(toolbar, BorderLayout.NORTH);
              frame.pack();
              frame.setVisible(true);
    }

    If the frame is too big to fit on the screen, they first appear, but when making one more component visible, they disappear again.I reset the maximum frame height and it works the way I would expect it to. That is the frame height doesn't change and scrollbars appear.
    frame.pack();
    GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
    Rectangle bounds = env.getMaximumWindowBounds();
    int height = bounds.height;
    Dimension d = frame.getSize();
    if (d.height > height)
         d.height = height;
         frame.setSize(d);
    }

Maybe you are looking for