Jlist not appearing in jpanel

I'm just messing around trying to get a JList to show work, but it won't show up. I've been going off the JList tutorial at http://java.sun.com/docs/books/tutorial/uiswing/components/list.html
but I don't know what I'm doing wrong.
     private class FileListPanel extends JPanel{
          public FileListPanel(){
               String[] arr = {";laskdfj","asdf"};
               JList fileList = new JList(arr);
               add(fileList);
               fileList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
               fileList.setLayoutOrientation(JList.HORIZONTAL_WRAP);
               fileList.setVisible(true);
               fileList.setVisibleRowCount(-1);
               fileList.setSelectedIndex(1);
               JScrollPane listScroller = new JScrollPane(fileList);
               listScroller.setPreferredSize(new Dimension(100,100));
               listScroller.setAlignmentX(LEFT_ALIGNMENT);
     }and I'm calling that from the constructor for the class using
Container gcp = getContentPane();
gcp.setLayout(new BorderLayout());
gcp.add(new FileListPanel(), BorderLayout.CENTER);
...

In situations like this, you're shooting yourself in the foot if you don't post a SSCCE. Demo:
import java.awt.*;
import javax.swing.*;
public class JListExample {
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable(){
            public void run() {
                launch();
    static void launch() {
        String[] data = {"aaa","bbb","ccc","ddd","eee",
            "fff","ggg","hhh","iii","jjj"};
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new JScrollPane(new JList(data)));
        f.setSize(100,100);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
}And in the future, please post Swing questions to the Swing forum.

Similar Messages

  • JLabel do not Appear On JPanel

    Help! PPl I am upto my eyes in this problem and I cannot solve it.
    I have a JFrame with BorderLayout on which I have 2 JPanels.The JPanel on the SOUTH of borderLayout is called secPanel and the one at the CENTER is called newPanel.
    On newPanel I am basically trying to make a lot of squares appear some should be whit and some black which is based on a random calculation. I have put in a lot of time on this and it worked on the first go and now it does not seem to work after I changed some code which I cannot recall what I did wrong. Now all the calculcations work out fine and everything only the squares which are really JLabels with a single space character in them are not displayed . The main file proj.java and the file that contains class aSquare are give below can someone please have a look and tell me how can I do it RIGHT! PLEASE
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    class proj
         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(700,700);
              Container c = aFrame.getContentPane();
              //c.setBackground(Color.blue);
              JPanel secPanel = new JPanel();
              secPanel.setLayout(new GridLayout(1,3));
              secPanel.setBackground(Color.gray);
              secPanel.setOpaque(false);
              secPanel.setBorder(BorderFactory.createEtchedBorder());
              JButton first, second, third;
              first= new JButton("Initialise New Maze");
              second = new JButton("Random New Genes");
              third = new JButton("Run Halt Genes");
              secPanel.add(first);
              secPanel.add(second);
              secPanel.add(third);
              JPanel newPanel = new JPanel();
              newPanel.setLayout(null);
              newPanel.setOpaque(true);
              newPanel.setBorder(BorderFactory.createEtchedBorder());
              newPanel.setVisible(true);
              aSquare squares[][] = new aSquare[40][40];
              int xPos=5;
              int yPos=5;
              for(int i=0; i<40; i++)
                   yPos= yPos+10;
                   xPos=5;
                   for(int j=0; j<40;j++)
                        xPos= xPos+10;
                        squares[i][j] = new aSquare(i,j);
                        if (nZrandom()%nZrandom()== 0 && nZrandom()%nZrandom() == 0)
                             squares[i][j].changeColor(Color.black);
                             System.out.println("MAde Black"+i+" "+j);
                        squares[i][j].setBounds(xPos, yPos,10,10);
                        System.out.println(xPos+" "+yPos);
                        newPanel.add(squares[i][j]);
                   }//end for j
              }//end for i
              newPanel.setOpaque(false);
              c.add(newPanel,BorderLayout.CENTER);
              c.add(secPanel,BorderLayout.SOUTH);
              aFrame.setVisible(true);
              aFrame.show();
         }//end main
         public static int nZrandom()
              int randno =(int)(Math.random()*10);
              while (true)
                   if (randno != 0)
                        return randno;
                   }else
                        randno = (int)(Math.random()*10);
                   }//end if
              }//end while
         }//end nZrandom
    }//end class proj
    CLASS aSquare
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    class aSquare extends JLabel implements MouseListener
         private int x, y;
         private boolean wall, beenThere;
         public aSquare(int a,int b)
              super(" ");
              this.setOpaque(true);
              this.setBackground(Color.white);
              this.setToolTipText(String.valueOf(a)+","+String.valueOf(b));
              this.setVisible(true);
              this.addMouseListener(new MouseAdapter()
                   public void mousePressed(MouseEvent e)
                        if (((JLabel)e.getSource()).getBackground().equals(Color.black))
                             ((JLabel)e.getSource()).setBackground(Color.white);
                        System.out.println("You are on the Label");
                        }else
                             ((JLabel)e.getSource()).setBackground(Color.white);
                             System.out.println("You are on the Label");
                        }//end if
              int x=a;
              int y=b;
              wall= false;
              beenThere= false;
         }//end constructor;
         public void changeColor(Color change)
              this.setBackground(change);
              if (change.equals(Color.cyan))
                   this.setBeenThere(true);
              if(change.equals(Color.black))
                   this.setWall(true);
         }//end changeColor
         public void mousePressed(MouseEvent me)
         public void mouseClicked(MouseEvent me)
         public void mouseReleased(MouseEvent me){}
         public void mouseEntered(MouseEvent me){}
         public void mouseExited(MouseEvent me){}
         public int getX()
         return this.x;
         public int getY()
         return this.y;
         public boolean getBeenThere()
         return this.beenThere;
         public void setBeenThere(boolean beenThere)
         this.beenThere = beenThere;
         public boolean getWall()
         return this.wall;
         public void setWall(boolean wall)
         this.wall = wall;
    }//end class aSquare

    There seems to be some problem in getting the label to display at the proper location on the JPanel cos I just wrote short code in which I pasted a JLabel and one instance of my class aSquare here renamed to Alab .
    the label get displayed at the proper location but the instance of class Alab does not . I went through the code line by line but I cannot understand it at all.. U have been very kind and patient.. can u gimme a hand with this one...
    thanks again..
    here is the small code
    //code
    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.setOpaque(true);
              secPanel.setBorder(BorderFactory.createEtchedBorder());
              JLabel aLabel = new JLabel(" ");
              aLabel.setBounds(200,200,10,10);
              aLabel.setBackground(Color.black);
              aLabel.setOpaque(true);
              aLabel.setVisible(true);
              Alab bLabel = new Alab(1,2);
              bLabel.setBounds(200,200,10,10);
              bLabel.setBackground(Color.black);
              bLabel.setOpaque(true);
              bLabel.setVisible(true);
              secPanel.add(aLabel);
              secPanel.add(bLabel);
              c.add(secPanel, BorderLayout.CENTER);
              aFrame.setVisible(true);
         }//end main
    }//end class LabelTry
    CLASS ALAB //WHICH IS NOTHING BUT class aSquare
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    class Alab extends JLabel implements MouseListener
         private int x, y;
         private boolean wall, beenThere;
         public Alab(int a,int b)
              super(" ");
              this.setOpaque(true);
              this.setBackground(Color.white);
              this.setToolTipText(String.valueOf(a)+","+String.valueOf(b));
              this.setVisible(true);
              this.addMouseListener(new MouseAdapter()
                   public void mousePressed(MouseEvent e)
                        if (((JLabel)e.getSource()).getBackground().equals(Color.black))
                             ((JLabel)e.getSource()).setBackground(Color.white);
                        System.out.println("You are on the Label");
                        }else
                             ((JLabel)e.getSource()).setBackground(Color.white);
                             System.out.println("You are on the Label");
                        }//end if
              int x=a;
              int y=b;
              wall= false;
              beenThere= false;
         }//end constructor;
         public void changeColor(Color change)
              this.setBackground(change);
              if (change.equals(Color.cyan))
                   this.setBeenThere(true);
              if(change.equals(Color.black))
                   this.setWall(true);
         }//end changeColor
         public void mousePressed(MouseEvent me)
         public void mouseClicked(MouseEvent me)
         public void mouseReleased(MouseEvent me){}
         public void mouseEntered(MouseEvent me){}
         public void mouseExited(MouseEvent me){}
         public int getX()
         return this.x;
         public int getY()
         return this.y;
         public boolean getBeenThere()
         return this.beenThere;
         public void setBeenThere(boolean beenThere)
         this.beenThere = beenThere;
         public boolean getWall()
         return this.wall;
         public void setWall(boolean wall)
         this.wall = wall;
    }//end class aSquare

  • Button not appearing (obvious fix most likely (no errors))

    import java.awt.*;
    import java.awt.event.*;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.*;
    public class ChatClient
      private TextArea output;
      private TextField input;
      private Button sendButton;
      private Button quitButton;
      private JButton button;
      private ImageIcon image;
      private ImageIcon image1;
      private Panel p1;
      private Frame frame;
      private Panel p2;
      public ChatClient()
        output = new TextArea(10,50);
        input = new TextField(50);
        frame = new Frame("Chat Room");
        sendButton = new Button("Send");
        quitButton = new Button("Quit");
        button = new JButton(image);
        image = new ImageIcon("c:/aztecsun.jpg");
        image1 = new ImageIcon("c:/aztecsun.jpg");
        p1 = new Panel();
        p2 = new Panel();
      public void launchFrame()
               button.setContentAreaFilled(false);
               button.setBorderPainted(false);
               button.setFocusPainted(false);
               button.setPressedIcon(image1);
               p1.add(sendButton);
               p1.add(quitButton);
               p2.add(button);
             // Use the Border Layout for the frame
             frame.setLayout(new BorderLayout());
             frame.add(output, BorderLayout.WEST);
             frame.add(input, BorderLayout.SOUTH);
             frame.add(p1, BorderLayout.EAST);
             frame.add(p2, BorderLayout.CENTER);
             // Add the button panel to the center
             frame.pack();
             frame.setVisible(true);
             //frame.addWindowListener(this);
             sendButton.addActionListener(new sendHandler());
             input.addActionListener(new inputHandler());
             button.addActionListener(new azteca());
             quitButton.addActionListener(new ActionListener()
                  public void actionPerformed(ActionEvent ae)
                       System.exit(0);
             frame.addWindowListener(new WindowAdapter()
                  public void windowClosing(WindowEvent we)
                       System.exit(0);
      /*     class windowTerminate implements WindowListener
                public void windowClosing(WindowEvent we)
                     System.exit(0);
                public void windowDeactivated(WindowEvent we)
                public void windowIconified(WindowEvent we)
                public void windowDeiconified(WindowEvent we)
                public void windowActivated(WindowEvent we)
                public void windowOpened(WindowEvent we)
                public void windowClosed(WindowEvent we)
       class azteca implements ActionListener
              public void actionPerformed(ActionEvent ae)
                   if(ae.getSource() == button)
                        String text = input.getText();
                        output.setText(output.getText() + text + "/n");
                        input.setText("");
           public void copyText()
                String text = input.getText();
                output.setText(output.getText() + text + "\n");
                input.setText("");
           class sendHandler implements ActionListener
                public void actionPerformed(ActionEvent ae)
                     copyText();
           class inputHandler implements ActionListener
                public void actionPerformed(ActionEvent ae)
                     copyText();
      public static void main(String[] args)
        ChatClient c = new ChatClient();
        c.launchFrame();
    }I'm practicing with events/buttons with icons and the button is not appearing...
    Yes the image is in the correct directory... (for the obvious fix type of people)...
    But yeah...
    Thanks!

    I know the fix but I do not know how to do it... I haven't added the image button correctly therefore it is not appearing, but I lack the necessary skills to fix the problem.
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.*;
    public class ChatClient
      private JTextArea output;
      private JTextField input;
      private JButton sendButton;
      private JButton quitButton;
      private JButton button;
      private ImageIcon image;
      private ImageIcon image1;
      private JPanel p1;
      private JFrame frame;
      private JPanel p2;
      public ChatClient()
        output = new JTextArea(10,50);
        input = new JTextField(50);
        frame = new JFrame("Chat Room");
        sendButton = new JButton("Send");
        quitButton = new JButton("Quit");
        button = new JButton(image);
        image = new ImageIcon("c:/aztecsun.jpg");
        image1 = new ImageIcon("c:/aztecsun.jpg");
        p1 = new JPanel();
        p2 = new JPanel();
      public void launchFrame()
               button.setContentAreaFilled(false);
               button.setBorderPainted(false);
               button.setFocusPainted(false);
               button.setPressedIcon(image1);
               p1.add(sendButton);
               p1.add(quitButton);
               p2.add(button);
             // Use the Border Layout for the frame
             frame.setLayout(new BorderLayout());
             frame.add(output, BorderLayout.WEST);
             frame.add(input, BorderLayout.SOUTH);
             frame.add(p1, BorderLayout.EAST);
             frame.add(p2, BorderLayout.CENTER);
             // Add the button panel to the center
             frame.pack();
             frame.setVisible(true);
             //frame.addWindowListener(this);
             sendButton.addActionListener(new sendHandler());
             input.addActionListener(new inputHandler());
             button.addActionListener(new azteca());
             quitButton.addActionListener(new ActionListener()
                  public void actionPerformed(ActionEvent ae)
                       System.exit(0);
             frame.addWindowListener(new WindowAdapter()
                  public void windowClosing(WindowEvent we)
                       System.exit(0);
      /*     class windowTerminate implements WindowListener
                public void windowClosing(WindowEvent we)
                     System.exit(0);
                public void windowDeactivated(WindowEvent we)
                public void windowIconified(WindowEvent we)
                public void windowDeiconified(WindowEvent we)
                public void windowActivated(WindowEvent we)
                public void windowOpened(WindowEvent we)
                public void windowClosed(WindowEvent we)
       class azteca implements ActionListener
              public void actionPerformed(ActionEvent ae)
                   if(ae.getSource() == button)
                        String text = input.getText();
                        output.setText(output.getText() + text + "/n");
                        input.setText("");
           public void copyText()
                String text = input.getText();
                output.setText(output.getText() + text + "\n");
                input.setText("");
           class sendHandler implements ActionListener
                public void actionPerformed(ActionEvent ae)
                     copyText();
           class inputHandler implements ActionListener
                public void actionPerformed(ActionEvent ae)
                     copyText();
      public static void main(String[] args)
        ChatClient c = new ChatClient();
        c.launchFrame();
    }Edited by: Trizi on Mar 25, 2008 11:52 AM

  • ScrollBars are not appearing on my JScrollPane

    I have a JPanel which I add a JScrollPane to -
    controlPanel = new JPanel();
    controlPanel.setPreferredSize(new Dimension(900, 100));
    JScrollPane cpScroll = new JScrollPane(controlPanel);I have added this JScrollPane to my Frame -
    frame.add(cpScroll, BorderLayout.SOUTH);I add a few JLabels to my Jpanel and increase the height of my JPanel as I add labels.
    The problem I am having is that the scroll pane is not appearing even if I make the height of the panel huge. However the strange thing is that it will appear if I make the Panel very wide.
    Does anyone know what could be causing the problem or how I could solve it. Thanks.

    compare yours to this, see if there's anything different
    import javax.swing.*;
    import java.awt.*;
    class Testing extends JFrame
      public Testing()
        setLocation(400,300);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        JPanel controlPanel = new JPanel(new GridLayout(10,1));
        for(int x = 0; x < 10; x++) controlPanel.add(new JLabel(""+x));
        controlPanel.setPreferredSize(new Dimension(400, 300));
        JScrollPane cpScroll = new JScrollPane(controlPanel);
        cpScroll.setPreferredSize(new Dimension(300, 150));
        getContentPane().add(cpScroll, BorderLayout.SOUTH);
        pack();
      public static void main(String[] args){new Testing().setVisible(true);}
    }

  • Buttons not  appearing (setVisible used)

    I have four buttons.
    This is what i want:
    1)At the begining run=debug=visible
    next=exit2=not visible
    2)When i click debug run=debug=invisible
    next=exit2=visible
    But it's not doing (2)correctly.
    instead it does debug run=debug=invisible
    next and exit2 do not appear.
    Any idea why?
    Here is the code:
    public class PC extends JApplet
    { public void init()
      { Executionionhandler a=new Executionionhandler();
        run=new JButton("Run");
        run.setEnabled(false);
        run.setMinimumSize(new Dimension(110,40));
        run.setPreferredSize(new Dimension(110,40));
        run.setMaximumSize(new Dimension(110,40));
        run.addActionListener(a);
        Debughandler b=new Debughandler();
        debug=new JButton("Debug");
        debug.setMinimumSize(new Dimension(100,40));
        debug.setPreferredSize(new Dimension(100,40));
        debug.setMaximumSize(new Dimension(100,40));
        debug.addActionListener(b);
        Right2=new JPanel();
        Right2.setBackground(Color.cyan);
        Right2.setMinimumSize(new Dimension(395,40));
        Right2.setPreferredSize(new Dimension(395,40));
        Right2.setMaximumSize(new Dimension(395,40));
        Right2.setLayout(new BoxLayout(Right2,BoxLayout.X_AXIS));
        Right2.add(run);
        Right2.add(debug);
        Nexthandler Ne=new Nexthandler();
        next=new JButton("NEXT");
        next.setVisible(false);
        next.addActionListener(Ne);
        Exithandler2 EX2=new Exithandler2();
        exit2=new JButton("EXIT DEBUG");
        exit2.setVisible(false);
        exit2.addActionListener(EX2);
        Right3=new JPanel();
        Right3.setBackground(Color.cyan);
        Right3.setMinimumSize(new Dimension(210,40));
        Right3.setPreferredSize(new Dimension(210,40));
        Right3.setMaximumSize(new Dimension(210,40));
        Right3.setLayout(new FlowLayout());
        Right3.add(next);
        Right3.add(exit2);
      private class Debughandler implements ActionListener
      { public void actionPerformed(ActionEvent event)
        { next.setVisible(true);
          exit2.setVisible(true);
          run.setVisible(false);
          debug.setVisible(false);
          boolean check2=false;
          for(int i=0;i<MemorySize&& check2==false ;i++)
          { if(MemoryContent.getText().length()>0)
    { start2=i;
    check2=true;
    private class Exithandler2 implements ActionListener
    { public void actionPerformed(ActionEvent event)
    { next.setVisible(false);
    exit2.setVisible(false);
    run.setVisible(true);
    debug.setVisible(true);

    Here they are
    import javax.swing.*;
    //import javax.swing.table.AbstractTableModel;
    import javax.swing.event.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.util.*;
    //import javax.swing.JTable;
    //import javax.swing.table.TableColumn;
    //import javax.swing.DefaultCellEditor;
    //import javax.swing.table.TableCellRenderer;
    //import javax.swing.table.DefaultTableCellRenderer;
    //import javax.swing.JScrollPane;
    //import javax.swing.JComboBox;
    //import javax.swing.JFrame;
    //import javax.swing.SwingUtilities;
    //import javax.swing.JOptionPane;
    public class PC extends JApplet
    { private int MemorySize=100,km,Limit,copy92,m,start1,start2;
      private boolean Stop=false;
      private JTextField MemoryContent[];
      private JLabel MemoryAddress[],MemoryAdd1,MemoryAdd2,calculator1,calculator2;
      private JPanel Memory1,Memory2,Memory3,Memory,Right,Right1,Right2,
              Right3,mainpane;
      private JScrollPane scrollPane1;
      private JButton load,save,clear,exit,run,debug,next,exit2;
      private File fileName;
      public void init()
      { Executionionhandler a=new Executionionhandler();
        run=new JButton("Run");
        run.setEnabled(false);
        run.setMinimumSize(new Dimension(110,40));
        run.setPreferredSize(new Dimension(110,40));
        run.setMaximumSize(new Dimension(110,40));
        run.addActionListener(a);
        Debughandler b=new Debughandler();
        debug=new JButton("Debug");
        debug.setMinimumSize(new Dimension(100,40));
        debug.setPreferredSize(new Dimension(100,40));
        debug.setMaximumSize(new Dimension(100,40));
        debug.addActionListener(b);
        Right2=new JPanel();
        Right2.setBackground(Color.cyan);
        Right2.setMinimumSize(new Dimension(395,40));
        Right2.setPreferredSize(new Dimension(395,40));
        Right2.setMaximumSize(new Dimension(395,40));
        Right2.setLayout(new BoxLayout(Right2,BoxLayout.X_AXIS));
        Right2.add(run);
        Right2.add(debug);
        Nexthandler Ne=new Nexthandler();
        next=new JButton("NEXT");
        next.setVisible(false);
        next.addActionListener(Ne);
        Exithandler2 EX2=new Exithandler2();
        exit2=new JButton("EXIT DEBUG");
        exit2.setVisible(false);
        exit2.addActionListener(EX2);
        Right3=new JPanel();
        Right3.setBackground(Color.cyan);
        Right3.setMinimumSize(new Dimension(210,40));
        Right3.setPreferredSize(new Dimension(210,40));
        Right3.setMaximumSize(new Dimension(210,40));
        Right3.setLayout(new FlowLayout());
        Right3.add(next);
        Right3.add(exit2);
        private class Debughandler implements ActionListener
      { public void actionPerformed(ActionEvent event)
        { next.setVisible(true);
          exit2.setVisible(true);
          run.setVisible(false);
          debug.setVisible(false);
          boolean check2=false;
          for(int i=0;i<MemorySize&& check2==false ;i++)
          { if(MemoryContent.getText().length()>0)
    { start2=i;
    check2=true;
    private class Exithandler2 implements ActionListener
    { public void actionPerformed(ActionEvent event)
    { next.setVisible(false);
    exit2.setVisible(false);
    run.setVisible(true);
    debug.setVisible(true);

  • Vertical scrollbar does not appear

    Hi all!
    Here is my problem.
    I have a JPanel on a JScrollPane (called: container).
    I have an other class which extends JPanel (called: component).
    I have a JButton which adds a new from component to container on every click. (container's layout is null, and I use the setBounds method on component).
    After every adding I've just set a new height to the container (setSize(container.getWidth(), container.getHeigth()+100)), but vertical scrollBars of JScrollPane does not appear.
    I tried to call the revalidate, repaint methods on the container but it did not work.
    Please help me!

    Which JScrollPane constructor are you using? If you're using one that does not specify scrollbar policy parameters, horizontal and vertical scrollbars only appear if the component's contents are larger than the view. You can of course set a JScrollPane's scollbar policies by calling the appropriate mutator methods: setVerticalScrollBarPolicy and setHorizontalScrollBarPolicy.

  • Problem in panel  - its not appear

    Hi to all;
    Here there is three panels . First Panel has label , when you press it you will go to the second panel . In the second panel there is a boutton when you press it third panel must be appeared but it is not appear .
    why this ? please show me the correct way .
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    public class TestPro extends JFrame {
        private JPanel panel = new JPanel();
        private JLabel label1;
        private MyMouseListener myMouseListener;
        private TestPanel testPanel;
        public TestPro() {
            myMouseListener = new MyMouseListener();
            testPanel = new TestPanel();
            label1 = new JLabel("Start");
            label1.setFont(new Font("Monospaced", Font.BOLD, 30));
            label1.addMouseListener(myMouseListener);
            panel.add(label1);
            getContentPane().add(panel);
        public static void main(String[] args) {
            TestPro testPro = new TestPro();
            testPro.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            testPro.setResizable(false);
            testPro.setSize(new Dimension(600, 600));
            testPro.setLocationRelativeTo(null);
            testPro.setVisible(true);
        private class MyMouseListener extends MouseAdapter {
            @Override
            public void mouseEntered(MouseEvent e) {
                super.mouseEntered(e);
                label1.setForeground(Color.red);
            @Override
            public void mouseExited(MouseEvent e) {
                super.mouseExited(e);
                label1.setBackground(Color.BLACK);
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                panel.setVisible(false);
                getContentPane().add(testPanel);
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.Box;
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    class TestPanel extends JPanel {
        private Box box;
        private JLabel label = new JLabel("New Panel");
        private JButton button = new JButton("Go>>>>>");
        private MyactionListener myactionListener;
        private AnotherPanel anotherPanel;
        public TestPanel() {
            myactionListener = new MyactionListener();
            anotherPanel = new AnotherPanel();
            box = Box.createVerticalBox();
            box.add(label);
            button.addActionListener(myactionListener);
            box.add(Box.createVerticalStrut(5));
            box.add(button);
            add(box);
        private class MyactionListener implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
                anotherPanel.setVisible(true);
                add(anotherPanel);
    import java.awt.Font;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    class AnotherPanel extends JPanel {
        private JLabel label=new JLabel("Another panel");
        public AnotherPanel() {
            label.setFont(new Font("Monospaced", Font.BOLD, 30));
            add(label);
    }Thanks in advance .
    Edited by: 804610 on Oct 22, 2010 6:29 PM

    Thanks for your reply .
    but in class TestPanel i did this :
        private class MyactionListener implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
                anotherPanel.setVisible(true);
                anotherPanel.revalidate();
                anotherPanel.repaint();
                add(anotherPanel);
        }but also the anotherPanel not appear when i press button . why?
    hint: I want when i press button the TestPanel disappear .
    thanks
    Edited by: 804610 on Oct 22, 2010 7:03 PM

  • JComponent not showing in JPanel

    I've tried the add() method but nothing is displayed when I try to add Test to GraphicsTest. How should I be adding it? Can someone show me? I've included the code I'm using.
    This is my way and it's not working. Can someone show me or make me aware of what the problem actually is?
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.geom.Rectangle2D;
    import javax.swing.JPanel;
    public class GraphicsTest extends JPanel
        private Graphics2D g2d;
        private String state;
        private int x, y;
        GraphicsTest()
            Test t = new Test();
            t.setVisible(true);
            add(t);
        @Override
        public void paintComponent(Graphics g)
            super.paintComponent(g);
            g2d = (Graphics2D) g;
            g2d.setColor(Color.BLACK);
            g2d.drawString("STATE: " + state, 5, 15);
            g2d.drawString("Mouse Position: " + x + ", " + y, 5, 30);
            g2d.setColor(Color.red);
            Rectangle2D r2d = new Rectangle2D.Double(x, y, 10, 10);
            g2d.draw(r2d);
            g2d.dispose();
        public void setState(String state) { this.state = state; }
        public String getState() { return state; }
        public void setX(int x) { this.x = x; repaint(); }
        public void setY(int y) { this.y = y; repaint(); }
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import javax.swing.JComponent;
    public class Test extends JComponent
        @Override
        public void paintComponent(Graphics g)
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setColor(Color.red);
            g2d.drawString("Hello", 50, 50);
            g2d.dispose();
    }

    The object in Test ("hello") is not appearing.
    import java.awt.EventQueue;
    //import java.awt.event.MouseEvent;
    //import java.awt.event.MouseListener;
    //import java.awt.event.MouseMotionListener;
    import javax.swing.JFrame;
    public class MainWindow
        public static void main(String[] args)
            new MainWindow();
        JFrame frame;
        GraphicsTest gt = new GraphicsTest();
        MainWindow()
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    frame = new JFrame("Graphics Practice");
                    frame.setSize(680, 420);
                    frame.setVisible(true);
                    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    //                gt.addMouseListener(new MouseListener() {
    //                    public void mouseClicked(MouseEvent e) {}
    //                    public void mousePressed(MouseEvent e) {}
    //                    public void mouseReleased(MouseEvent e) {}
    //                    public void mouseEntered(MouseEvent e) {
    //                        gt.setState("Mouse has entered");
    //                    public void mouseExited(MouseEvent e) {
    //                        gt.setState("Mouse has not entered");
    //                gt.addMouseMotionListener(new MouseMotionListener() {
    //                    public void mouseDragged(MouseEvent e) {}
    //                    public void mouseMoved(MouseEvent e) {
    //                        gt.setX(e.getX());
    //                        gt.setY(e.getY());
                    frame.add(gt);
    }

  • JScrollPane is not appeared.

    Hi,
    I want to use JScrollPane.
    However in my program scrollbar is not appeared.
    What is wrong?
    Thanks.
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class ImageViewFrame extends JFrame {
    public static void main(String[] args) {
         ImageViewFrame frame = new ImageViewFrame();
         frame.show();
    ImageViewFrame() {
         setSize(300, 300);
         addWindowListener(new WindowAdapter() {
              public void windowClosing(WindowEvent evt) {
              System.exit(0);
         JPanel panel = new JPanel();
         panel.setSize(1000, 1000);
         JScrollPane scrollPane = new JScrollPane(panel);
         getContentPane().add(scrollPane, BorderLayout.CENTER);

    I add a line, "panel.setPreferredSize(new Dimension(1000, 1000));"
    It works well.
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class ImageViewFrame extends JFrame {
    public static void main(String[] args) {
         ImageViewFrame frame = new ImageViewFrame();
         frame.show();
    ImageViewFrame() {
         setSize(300, 300);
         addWindowListener(new WindowAdapter() {
              public void windowClosing(WindowEvent evt) {
              System.exit(0);
         JPanel panel = new JPanel();
         panel.setSize(1000, 1000);
         panel.setPreferredSize(new Dimension(1000, 1000));
         JScrollPane scrollPane = new JScrollPane(panel);
         getContentPane().add(scrollPane, BorderLayout.CENTER);

  • Choice list does not appear

    Hi everyone!
    I have added a choice list to my Applet and it compiled without any kind of erros, but once I view the Applet on appletviewer or on my browser I can't see it.
    I have created a Panel to display the choice list so that it didn't interfere with the other elements of the Applet. I used a setLayout(null) to display it on the position I was expecting it to, but it does not appear anywhere. Would anyone be able to give me a hint on this issue? I would be grateful.
    Regards,
    Saulo

    would be easier if you post your codesnippet, but here's some general info.
    1. get the container: Container contentpane = getContentPane();
    2. create a panel: JPanel panel = new JPanel();
    3. create your elements you actually want to appear (example):
    JLabel label = new JLabel("hello world");
    4. add the label to the panel: panel.add(label); //you can use LayoutManagers for layout
    5. add the panel to the contentpane: contentpane.add(panel);
    6 call setContentPane(contentpane);

  • Jlist not displaying correctly when populated by thread

    Hi, I have this problem when a thread attempts to populat a Jlist, it will only populate sometimes. Othertimes, it doesnt display correctly. The model will have the correct objects in it, but the display on screen will not appear. I have some code below that demonstrates the problem. I renger html in the Jlist just so that its easier to see, but doesnt have to be html.
    import javax.swing.*;
    public class listtest extends javax.swing.JFrame {
    private DefaultListModel model = new DefaultListModel();
    public listtest() {
    initComponents();
    jList1.setModel(model);
    private void initComponents() {
    jScrollPane1 = new javax.swing.JScrollPane();
    jList1 = new javax.swing.JList();
    jButton1 = new javax.swing.JButton();
    addWindowListener(new java.awt.event.WindowAdapter() {
    public void windowClosing(java.awt.event.WindowEvent evt) {
    exitForm(evt);
    jScrollPane1.setViewportView(jList1);
    getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);
    jButton1.setText("jButton1");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jButton1ActionPerformed(evt);
    getContentPane().add(jButton1, java.awt.BorderLayout.SOUTH);
    pack();
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    new myThread().start();
    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {
    System.exit(0);
    * @param args the command line arguments
    public static void main(String args[]) {
    new listtest().show();
    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JList jList1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration
    class myThread extends Thread{
    public void run(){
    model.clear();
    String temp;
    for(int i = 0; i < 100; ++i){
    temp = "<html><table><tr><td>" + i + "</td></tr></table></html>";
    model.addElement(temp);
    When you push the button, sometimes the list will come out correctly and sometimes it wont. Anyone know how to get it to display correctly? I tries all sort of things with InvokeLater, validates, repaints, etc. I cant get anything to get it to display properly.

    I think this is closer to what you wanted.
    Your sample code if obviously simple enough not to need it, but this allows you to thread off the work and the use invokeLater only to push the results back to the UI/event thread.
    for( int i = 0; i < 100; ++i )
    final String temp = "<html><table><tr><td>" + i + "</td></tr></table></html>";
    SwingUtilities.invokeLater(
    new Runnable()
        public void run()
            model.addElement(temp);
    }

  • Html page does not appear???

    I am designin a hepl page.I've written the code as follows
    but the html does not appear.Instead the panel holding the
    html is plain white.Why is this so?
    P.S: i am able to view the html in a explorer.
    Font demoFont = new Font("Serif", Font.PLAIN, 14);
        quotePane = null;
        scrollPane3 = null;
        s=null;
           try
        { s="file:\\myjava\\HELP.html";
          URL url = new URL(s);
             quotePane = new JEditorPane(url);
             quotePane.setEditable(false);
          scrollPane3 = new JScrollPane(quotePane);
             scrollPane3.setMinimumSize(new Dimension(390,490));
          scrollPane3.setPreferredSize(new Dimension(390,490));
          scrollPane3.setMaximumSize(new Dimension(390,490));
             if (DEBUG)
          { System.out.println(quotePane.getText());
               quotePane.setText(quotePane.getText());
        catch (java.io.IOException e)
        { scrollPane3 = new JScrollPane(new JTextArea("Can't find HTML file."));
             scrollPane3.setFont(demoFont);
        htmlTextPane = new JPanel();
         htmlTextPane.add(scrollPane3);
            /*htmlTextPane.setBorder(BorderFactory.createCompoundBorder(
                          BorderFactory.createTitledBorder("HTML Text"),
                          BorderFactory.createEmptyBorder(0, 5, 5, 5)
        //helppane=new JPanel();
        //htmlTextPane.setBackground(Color.magenta);
        HelpFrame=new JFrame("Help Index");
        HelpFrame.addWindowListener(new WindowAdapter()
                                 public void windowClosing(WindowEvent e)
                                   HelpFrame.setVisible(false);
        HelpFrame.getContentPane().add(htmlTextPane);
        HelpFrame.pack();
        HelpFrame.setSize(400,500);
        Container c=getContentPane();
        c.add(mainpane);

    More info ...
    Can we see the whole program please.

  • JComboBox - selection down arrow not appearing

    Hi All,
    I am writing a swing application which uses a JComboBox with a Vector<String> as it's constructor arguments. This JComboBox is then added to a JPanel derived class which is in turn added to a JFrame. The JComboBox works fine but the selection down arrow does not appear for some reason.
    So I built a quick test application (in a similar fashion) and the JComboBox also works fine but this time the selection down arrow does appear.
    I can't figure out what I'm doing different. Can anyone tell me the circumstances under which the down arrow will / will not appear in a JComboBox?
    Thanks,
    Dougall

    Hi,
    The reason I had used this approach is that sometime I wish the content pane to hold a JPanel type component (only) and sometimes a JTable type (only). I had (foolishly perhaps) assumed that this would not be possible with a standard layout manager.
    As es5f2000 suggests I shall investigate a CardLayout manager.
    If this does not succeed I shall keep the present system which currently works fine (maybe performance issues?). The bounds and size of the parent container are both work fine so long as I remember to call .validate() on the new component being added to the JFrame.
    The JFrame itself is specifically set to be a fixed non-resizable size, surely this is a case in which although not recommended, it's ok to use absolute positioning?
    Thanks again,
    Dougall

  • I am trying to set up Sharing amongst several computers on my home network.  I have followed all the setup instructions but after completion Sharing does not appear in my iTunes window (on any of the computers I've set up).  There is no explanation why.

    I have been trying to set up Sharing between two computers on my network. I did this once and it worked.  However, when I try to set this up now the Sharing feature does not appear on the left side of the iTunes window (on either computer).  There is no explanation as to why not.  Is this a configuration problem, a network problem or what?   Can it be fixed?  I have tried turning sharing off and back on on both computers but to no avail.  What am I missing?  Thanks for any help you can provide.

    Hey innerev2003,
    Thanks for the question, and welcome to Apple Support Communities.
    If, while searching through your past purchases, items are unavailable to download and show a "Purchased" button, they are still on your computer. It may be best to search your iTunes library by clicking Music, then use the search bar at the top right.
    If you are completely sure the items are no longer in your library, try signing out of the iTunes Store, and then back in.
    iTunes 11 for Windows: Manage your iTunes Store account
    http://support.apple.com/kb/PH12507
    Thanks,
    Matt M.

  • Hi all, I upgraded my MBP to Lion , but on the screen where i need to type my password, click  on my photo and it does not appear the place for me to type my password and it stay stuck there. Can anyone solve this problem for me?

    Hi all, I upgraded my MBP to Lion , but on the screen where i need to type my password, click  on my photo and it does not appear the place for me to type my password and it stay stuck there. Can anyone solve this problem for me?

    Reboot the machine holding Command and r keys down, you'll boot into Lion Recovery Partition
    In there will be Disk Utility, use that to select your Lion OS X Partition and Repair Permissions.
    After that is done reboot the machine and see if you can log in.
    If not repeat the above steps to get into Lion Recovery, get online and reinstall Lion again, it will overwrite the installed version and hopefully after that it wil work.
    Reboot and try again.
    If not follow my steps to create a Snow Leopard Data Recovery drive, then option boot from it and grab a copy of your files off the machine.
    Then reinstall all your programs onto the external drive like setting up a new machine, then use Disk Utility to erase the entire internal boot drive (select the drive media on the far left, not the partiton slightly indented) format Option: GUID , 1 partition OS X Extended and then use Carbon Copy Cloner to clone the external to the newly formatted internal drive. Once that is finished reboot and disconnect the external drive.
    Once you go that, boot into Snow Leopard and update to 10.6.8, use the AppStore and option click on Purchases and download Lion again and install.
    Lots of work, but there is no Lion disks.
    https://discussions.apple.com/message/16276201#16276201

Maybe you are looking for

  • Issue with the Document Viewer for Some Users

    Hi, I have archivelink setup in our system. Users are able to view the documents using this link. But issue is that one user is unable to view the documents while others can view the documents.  The roles for both users are same. Searched the forums

  • Trial install asks for disk to install - CS6 Master Suite

    I installed the Master Suite collection quite a while ago on a previous computer with no problem. Now though, I'm being asked for a disk just to go through with a trial installation. I'll clck on "trial" installation choice. I'll click on "next" and

  • Class not registered error when saving picture from webpage

    I right click to save a picture (jpeg) on a web page and get a pop-up saying "(filename) class not registered. I an using Firefox on a Windows 7 Home Premium Version 6.1,7601 SP 1 Build 7601 -- hp Pavillion g7 notebook.

  • Update Help Needed

    When I try to update my 3GS, I get a window that pops up and says that my "network connection has timed out". I have been on the phone with Apple twice now and they have no idea what to tell me!!! So frustrated! Please help!

  • What happened to default gateway routes?

    I installed Arch on a new computer today. I used the arch-core-install-2008.04-rc-i686.iso image. Everything seems to be working okay apart from the default routing table. There is no default entry for 192.168.0.1.  Instead I have to add it with: # r