JTable on JPanel on JFrame, not appearing

I can't see the JTable that I was expecting to between the two labels, any ideas why?
public class Workout extends JFrame {
        final String[] exercisesDoneColumnNames = {"Exercise", "# Sets", "Set 1", "Set 2", "Set 3", "Set 4", "Set 5", "Set 6", "Set 7", "Set 8", "Set 9", "Set 10"};
        private ExercisesDoneTable myModel = new ExercisesDoneTable exercisesDoneColumnNames,0);
        private JTable table = new JTable(myModel);
        public Workout() {
            super.setTitle("Workout");
            buildExerciseDoneTable();
            Toolkit tk = Toolkit.getDefaultToolkit();
            Dimension d = new Dimension();
            d = tk.getScreenSize();
            setSize((int)d.getWidth(), (int)d.getHeight());
            setLocation(0,0);
            super.setName("Workout");
            addWindowListener(new java.awt.event.WindowAdapter() {
                public void windowClosing(java.awt.event.WindowEvent evt) {   
                    System.exit(0);
            JPanel p = new JPanel();
            p.setBackground(java.awt.Color.white);
            p.add(new JLabel("left of"));
            p.add(table);
            p.add(new JLabel("right of"));
            getContentPane().add(p);
            setVisible(true);
    //helper method just splits the code to make it more readable
    private void buildExerciseDoneTable(){
        table.setPreferredScrollableViewportSize(new Dimension(500,100));
        myModel.addTableModelListener(myModel);
        table.setVisible(true);
    }

JPanel has a default FlowLayout.
I think you table is there, but is not displaying anything as you have specified 0 rows when creating your table model. Change that number to 1 and you will see it appear.

Similar Messages

  • Combo Box in a JTable not appearing

    Hi Swing,
    I have a snippet of code that I took from the Swing guide for JTables that adds a JComboBox into the 3rd column in the table. However, the ComboBox doesnt appear?
    Can anyone see what is going wrong? Code is below:- I can post more if needed:-
         public void addTable() {
              tableModel = new MyTableModel(rows,columns);
              relationTable = new JTable(tableModel);
           //Set up the editor for the sport cells.
            TableColumn sportColumn = table.getColumnModel().getColumn(2);                                              
            JComboBox allStaff = new JComboBox();
            allStaff.addItem("Snowboarding");
            allStaff.addItem("Rowing");
            allStaff.addItem("Knitting");
            allStaff.addItem("Speed reading");
            allStaff.addItem("Pool");
            allStaff.addItem("None of the above");
            sportColumn.setCellEditor(new DefaultCellEditor(allStaff));
              // set so only one row can be selected at once
              relationTable.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );
              relationTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
              // add to pane:-
              JScrollPane scrollPane3 = new JScrollPane(relationTable);
              scrollPane3.setBounds(X,Y,Z,W);
              getContentPane().add(scrollPane3 );
         }Cheers

    hi
    look I will give u a simple code I created based on your combo box
    enjoy (:
    import java.util.*;
    import javax.swing.*;
    import javax.swing.table.*;
    import java.awt.*;
    public class TablCo extends JPanel {
    static JFrame frame;
    JTable table;
    DefaultTableModel tm;
    public TablCo() {
    super(new BorderLayout());
    JPanel leftPanel = createVerticalBoxPanel();
    //Create a table model.
    tm = new DefaultTableModel();
    tm.addColumn("Column 0");
    tm.addColumn("Column 1");
    tm.addColumn("Column 2");
    tm.addColumn("Column 3");
    tm.addRow(new String[]{"01", "02", "03", "Snowboarding"});
    tm.addRow(new String[]{"04 ", "05", "06", "Snowboarding"});
    tm.addRow(new String[]{"07", "08", "09", "Snowboarding"});
    tm.addRow(new String[]{"10", "11", "12", "Snowboarding"});
    //Use the table model to create a table.
    table = new JTable(tm);
              //insert a combo box in table
              TableColumn sportColumn = table.getColumnModel().getColumn(3);
              JComboBox allStaff = new JComboBox();
                   allStaff.addItem("Snowboarding");
                   allStaff.addItem("Rowing");
                   allStaff.addItem("Knitting");
                   allStaff.addItem("Speed reading");
                   allStaff.addItem("Pool");
                   allStaff.addItem("None of the above");
              //DefaultCellEditor dce = new DefaultCellEditor(allStaff);
              sportColumn.setCellEditor(new DefaultCellEditor(allStaff));
              JScrollPane tableView = new JScrollPane(table);
    tableView.setPreferredSize(new Dimension(300, 100));
              leftPanel.add(createPanelForComponent(tableView, "JTable"));
              JFrame.setDefaultLookAndFeelDecorated(true);
              //Create and set up the window.
              frame = new JFrame("BasicDnD");
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              //Create and set up the content pane.
              frame.setContentPane(leftPanel);
              //Display the window.
              frame.pack();
              frame.setVisible(true);
              protected JPanel createVerticalBoxPanel() {
              JPanel p = new JPanel();
              p.setLayout(new BoxLayout(p, BoxLayout.PAGE_AXIS));
              p.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
              return p;
              public JPanel createPanelForComponent(JComponent comp,
              String title) {
              JPanel panel = new JPanel(new BorderLayout());
              panel.add(comp, BorderLayout.CENTER);
              if (title != null) {
              panel.setBorder(BorderFactory.createTitledBorder(title));
              return panel;
    public static void main(String[] args) {
         new TablCo();
    }

  • 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);

  • 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

  • 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);}
    }

  • 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

  • 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);

  • 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.

  • Problem with JPanel in JFrame

    hai ashrivastava..
    thank u for sending this one..now i got some more problems with that screen .. actually i am added one JPanel to JFrame with BorderLayout at south..the problem is when i am drawing diagram..the part of diagram bellow JPanel is now not visible...and one more problem is ,after adding 6 ro 7 buttons remaing buttons are not vissible..how to increase the size of that JPanel...to add that JPanel i used bellow code
    JFrame f = new JFrame();
    JPanel panel = new JPanel();
    f.getContentPane().add(BorderLayout.SOUTH, panel);

    Hi
    JFrame f = new JFrame();
    JPanel panel = new JPanel();
    // Add this line to ur code with ur requiredWidth and requiredHeight
    panel.setPreferredSize(new Dimension(requiredWidth,requiredHeight));
    f.getContentPane().add(BorderLayout.SOUTH, panel);
    This should solve ur problem
    Ashish

  • Adding Canvas3D image to a JPanel or JFrame

    My team has developed a 3D game board and we want to add it to a JPanel. The test code below works fine but we want to add this to a JPanel. Can you put a Canvas3D in a JPanel?
    import java.applet.Applet;
    import java.awt.BorderLayout;
    import java.awt.Frame;
    import java.awt.event.*;
    import com.sun.j3d.utils.applet.MainFrame;
    import com.sun.j3d.utils.universe.*;
    import com.sun.j3d.utils.geometry.*;
    import javax.media.j3d.*;
    import javax.vecmath.*;
    import java.awt.event.*;
    import java.util.Enumeration;
    import com.sun.j3d.utils.behaviors.mouse.*;
    import com.sun.j3d.utils.behaviors.keyboard.*;
    public class ProxyBoard extends Applet
         public class SimpleBehave extends Behavior
              private TransformGroup targetTG;
              private Transform3D rotation = new Transform3D();
              private double angle = 0.0;
              SimpleBehave(TransformGroup targetTG)
                   this.targetTG = targetTG;
              public void initialize()
                   this.wakeupOn(new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED));
              public void processStimulus(Enumeration criteria)
                   angle +=0.05;
                   rotation.rotX(angle);
                   targetTG.setTransform(rotation);
                   this.wakeupOn(new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED));
         public ProxyBoard()
              setLayout(new BorderLayout());
              Canvas3D canvas3D = new Canvas3D(null);
              add("Center", canvas3D);
              SimpleUniverse simpleU = new SimpleUniverse(canvas3D);
              simpleU.getViewingPlatform().setNominalViewingTransform();
              BranchGroup scene = createSceneGraph(simpleU);
              scene.compile();
              simpleU.addBranchGraph(scene);
         public BranchGroup createSceneGraph(SimpleUniverse su)
              BranchGroup boardBG = new BranchGroup();
              TransformGroup vpTrans = null;
              BoundingSphere mouseBounds = null;
              vpTrans = su.getViewingPlatform().getViewPlatformTransform();
              mouseBounds = new BoundingSphere(new Point3d(), 1000.0);
              KeyNavigatorBehavior keyNavBeh = new KeyNavigatorBehavior(vpTrans);
              keyNavBeh.setSchedulingBounds(mouseBounds);
              boardBG.addChild(keyNavBeh);
              MouseRotate myMouseRotate = new MouseRotate(MouseBehavior.INVERT_INPUT);
              myMouseRotate.setTransformGroup(vpTrans);
              myMouseRotate.setSchedulingBounds(mouseBounds);
              boardBG.addChild(myMouseRotate);
              MouseTranslate myMouseTranslate = new MouseTranslate(MouseBehavior.INVERT_INPUT);
              myMouseTranslate.setTransformGroup(vpTrans);
              myMouseTranslate.setSchedulingBounds(mouseBounds);
              boardBG.addChild(myMouseTranslate);
              MouseZoom myMouseZoom = new MouseZoom(MouseBehavior.INVERT_INPUT);
              myMouseZoom.setTransformGroup(vpTrans);
              myMouseZoom.setSchedulingBounds(mouseBounds);
              boardBG.addChild(myMouseZoom);
              Board board = new Board();
              Transform3D pegPositions[] = new Transform3D[8];
              TransformGroup pegPositionsTG[] = new TransformGroup[8];
              Pegs pegs[] = new Pegs[8];
              Transform3D translate = new Transform3D();
              translate.set(new Vector3f(0.0f, -1.0f, -5.0f));
              TransformGroup boardTGT1 = new TransformGroup(translate);
              TransformGroup boardTGR1 = new TransformGroup();
              boardTGR1.setCapability(boardTGR1.ALLOW_TRANSFORM_WRITE);
              for(int i = 0; i < 8; i++)
                   pegs[i] = new Pegs();
                   pegPositions[i] = new Transform3D();
                   pegPositions.set(new Vector3f(-2.5f, 0, (float)(-i/4.0)));
                   pegPositionsTG[i] = new TransformGroup(pegPositions[i]);
                   pegPositionsTG[i].addChild(pegs[i].getTransformGroup());
                   boardTGT1.addChild(pegPositionsTG[i]);               
              boardTGR1.addChild(board.getBoard());
              SimpleBehave myRotate = new SimpleBehave(boardTGR1);
              myRotate.setSchedulingBounds(new BoundingSphere());
              boardBG.addChild(myRotate);
              boardTGT1.addChild(boardTGR1);
              boardBG.addChild(boardTGT1);                    
              boardBG.compile();
              return boardBG;
         public static void main(String[] args)
              Frame frame = new MainFrame(new ProxyBoard(), 800, 600);
    /*-------------Main Source Container------------------*/
    public class Proxy extends JFrame
         implements MouseMotionListener
         private JDesktopPane myDesktop;
         private JPanel panel;
         private JLabel statusBar, position;
         private JSlider zSlide, zSlide1;
         private ImageIcon test;
         private int i;
         public Proxy()
              super("Proxy Board Prototype 1.2.2");
              i=0;
              statusBar = new JLabel();
              getContentPane();
              myDesktop = new JDesktopPane();
              getContentPane().add(myDesktop);
    public static void main(String args[])
              Proxy app = new Proxy();
              //new MainFrame( new Proxy(), 800, 600 );
    //          app.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    /*-------------JPanel I want to hold to the 3D Graphics--------*/
    class Session extends JPanel
         implements MouseMotionListener
         private ImageIcon test;
         private JLabel position;
         private JSlider zSlide;
         private JPanel panel, gPanel;
         static int openFrameCount = 0;
         public Session()
              //super("", true, true, true, true);
              setLayout(new FlowLayout());
              openFrameCount++;
         //setTitle("Untitled Message " + openFrameCount);
    ProxyBoard pb = new ProxyBoard();//<-Graphic Class
    gPanel = new JPanel();
    gPanel.add(pb);//<-I want the 3D Graphic here
              test = new ImageIcon("Layout.jpg");
              JLabel pic = new JLabel(test);
              addMouseMotionListener( this );
              zSlide = new JSlider(SwingConstants.VERTICAL, 600, 10);
              zSlide.setMajorTickSpacing(25);
              zSlide.setPaintTicks(true);
              zSlide.setToolTipText("Zoom");
              panel.add(zSlide, BorderLayout.NORTH);
              panel.add(gPanel, BorderLayout.CENTER);//<-Graphic Here
              panel.add(position, BorderLayout.SOUTH);
              add(panel);
              //pack();
              setSize(200,200);
              setVisible (true);

    Yes you can. Just use the add method of JPanel. However note this
    http://www.j3d.org/faq/swing.html (specially read this: http://java.sun.com/products/jfc/tsc/articles/mixing/)
    when mixing Lightweight (JPanel) and Heavyweight (Canvas3D) components.

  • 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.

  • Beginners Questions about Multiple JPanels in JFrame and event handling

    I am a newbie with SWING, and even a newerbie in Event Handling. So here goes.
    I am writing a maze program. I am placing a maze JPanel (MazePanel) at the center of a JFrame, and a JPanel of buttons (ButtonPanel) on the SOUTH pane. I want the buttons to be able to re-randomize the maze, solve the maze, and also ouput statistics (for percolation theory purposes). I have the backbone all done already, I am just creating the GUI now. I am just figuring out EventHandlers and such through the tutorials, but I have a question. I am adding an ActionListener to the buttons which are on the ButtonPanel which are on JFrame (SOUTH) Panel. But who do I make the ActionListener--Basically the one doing the work when the button is pressed. Do I make the JFrame the ActionListener or the MazePanel the ActionListener. I need something which has access to the maze data (the backbone), and which can call the Maze.randomize() function. I'm trying to make a good design and not just slop too.
    Also I was wondering if I do this
    JButton.addActionListener(MazePanel), and lets say public MazePanel implments ActionListenerdoesn't adding this whole big object to another object (namely the button actionlistener) seem really inefficient? And how does something that is nested in a JPanel on JFrame x get information from something nested in another JPanel on a JFrame x.
    Basically how is the Buttons going to talk to the maze when the maze is so far away?

    I'm not an expert, but here's what I'd do....
    You already have your business logic (the Maze classes), you said. I'm assuming you have some kind of public interface to this business logic. I would create a new class like "MazeGui" that extends JFrame, and then create the GUI using this class. Add buttons and panels as needed to get it to look the way you want. Then for each button that does a specific thing, add an anonymous ActionListener class to it and put whatever code you need inside the ActionListener that accesses the business logic classes and does what it needs to.
    This is the idea, though my code is totally unchecked and won't compile:
    import deadseasquirrels.mazestuff.*;
    public class MazeGui extends JFrame {
      JPanel buttonPanel = new JPanel();
      JPanel mazePanel = new JPanel();
      JButton randomizeB = new JButton();
      JButton solveB = new JButton();
      JButton statsB = new JButton();
      // create instanc(es) of your Maze business logic class(es)
      myMaze = new MazeClass();
      // add the components to the MazeGui content pane
      Component cp = getContentPane();
      cp.add(); // this doesn't do anything, but in your code you'd add
                // all of your components to the MazeGui's contentpane
      randomizeB.addActionListener(new ActionListener {
        void actionPerformed() {
          Maze newMaze = myMaze.getRandomMazeLayout();
          mazePanel.setContents(newMaze); // this is not a real method!
                                          // it's just to give you the idea
                                          // of how to manipulate the JPanel
                                          // representing your Maze diagram,
                                          // you will probably be changing a
                                          // subcomponent of the JPanel
      solveB.addActionListener(new ActionListener {
        void actionPerformed() {
          Solution mySolution = myMaze.getSolution();
          mazePanel.setContents(mySolution); // again, this is not a real
                                             // method but it shows you how
                                             // the ActionListener can
                                             // access your GUI
      // repeat with any other buttons you need
      public static void main(String[] args) {
        MazeGui mg = new MazeGui();
        mg.setVisible(true);
        // etc...
    }

  • 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);

  • 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.

Maybe you are looking for