JTabbedPane


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: JTabbedPane

  1. #1
    Join Date
    Sep 2003
    Posts
    21

    JTabbedPane

    I am starting to learn about Swing and different
    classes in Swing. I typed out the following code pretty much straight from my text book, though after piecing together the different classes in different chapters. I am getting one Error after compiling and cannot seem find the reason for it. If I ever do get this compiled, I would like to discuss further the JTabbedPane class because it is what I think I want to use in my class project.

    So first, if someone could guide me to the solution for the following composition error I would be greatly appreciative. I mean guide me, not give the direct solution.

    tabDemo.java [24:1] cannot resolve symbol
    symbol : method setSeletedIndex (int)
    location: class javax.swing.JTabbedPane
    tabPane.setSeletedIndex(0);
    ^
    1 error
    Here's the code, with the snippet of code in Red that is part of the compilation error. I am guessing that the value of the Index for the statement tabPane.setSelectedIndex(0);
    is what is causing the error, would this be the 1st Tab on the JTabbedPane? I know in previous instruction in arrays the index of the first element is always 0, right?
    What is wrong here?

    Code:
    /*
     * tabDemo.java
     *
     * Created on October 26, 2003, 8:19 PM
     */
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.event.*;
    
    public class tabDemo extends JFrame
    {
        public tabDemo(String title) 
        {
            super(title);
            JTabbedPane tabPane = new JTabbedPane();
            Container c = getContentPane();
            c.add(tabPane,"Center");
            SelectItem item = new SelectItem();
            tabPane.addTab("Item",item);
            SelectMessage message = new SelectMessage();
            tabPane.addTab("Message",message);
            tabPane.setSeletedIndex(0);
            addWindowListener(new CloseWindow());
        }
        
        public static void main (String [] args)
        {
         tabDemo t = new tabDemo("Tab display");
         t.pack();
         t.show();
        }
        
        class CloseWindow extends WindowAdapter
        {
         public void windowClosing(WindowEvent event)
         {
             System.exit(0);
         }
        }
        
        public class SelectItem extends JPanel
        {
         private DrawOn canvas = new DrawOn();
         private ButtonGroup group = new ButtonGroup();
         private JRadioButton square = new JRadioButton("Square");
         private JRadioButton circle = new JRadioButton("Circle", true);
         private Color[] theColor = {Color.red,Color.green,Color.blue};
         private String[] colorName = {"Red","Green","Blue"};
         private JComboBox color = new JComboBox(colorName);
         
         public void init()
         {
          Container c = getContentPane();
          c.setLayout(new FlowLayout());
          c.add(color);
          group.add(square);
          group.add(circle);
          c.add(square);
          c.add(circle);
          c.add(canvas);
          canvas.setPreferredSize(new Dimension(150,150));
          color.addItemListener(canvas);
          square.addItemListener(canvas);
          circle.addItemListener(canvas);
          
         }
         
         class DrawOn extends JPanel implements ItemListener
         {
            boolean isCircle = true;
            public void itemStateChanged(ItemEvent event)
            {
             Object source = event.getItem();
             if (source == circle)
                 isCircle = true;
             else if (source == square)
                 isCircle = false;
             repaint();
            }
        
         
            public void paintComponent(Graphics g)
            {
              super.paintComponent(g);
              g.setColor(theColor[color.getSelectedIndex()]);
              if (isCircle)
                  g.fillOval(20, 20, 100, 100);
              else
                  g.fillRect(20,20,100,100);
            }
           }
        }
        
        class SelectMessage extends JPanel
        {
         private DrawOn canvas = new DrawOn();
         private String[] colorName = {"Black","Blue","Cyan","Dark Gray","Gray",
                "Green","Light Gray","Magenta","Orange","Pink","Red","White","Yellow"};
         private JList names = new JList(colorName);
         private JScrollPane color = new JScrollPane(names);
         private JCheckBox italic = new JCheckBox("Italic");
         private JCheckBox bold = new JCheckBox ("Bold");
         private Color [] theColor = {Color.black,Color.blue,Color.cyan,
                  Color.darkGray,Color.gray,Color.green,Color.lightGray,
                  Color.magenta,Color.orange,Color.pink,Color.red,Color.white,
                  Color.yellow};
         private String message = "Hi there";
         
         public void init()
         {
           Container c = getContentPane();
           c.setLayout(new FlowLayout());
           c.add(color);
           c.add(italic);
           c.add(bold);
           c.add(canvas);
           names.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
           names.setSelectedIndex(0);
           names.addListSelectionListener(canvas);
           canvas.setPreferredSize(new Dimension(150,150));
           italic.addItemListener(canvas);
           bold.addItemListener(canvas);
           
         }
         
         class DrawOn extends JPanel implements 
                             ItemListener, ListSelectionListener
         {
          int style = Font.PLAIN;
          public void itemStateChanged(ItemEvent event)
          {
           Object source = event.getItem();
           int change = event.getStateChange();
           if (source == italic)
               if (change ==ItemEvent.SELECTED)
                   style += Font.ITALIC;
               else
                   style -= Font.ITALIC;
           if (source == bold)
               if (change ==ItemEvent.SELECTED)
                   style += Font.BOLD;
               else
                   style -= Font.BOLD;
           repaint();
          }
          
          public void valueChanged(ListSelectionEvent e)
          {
           if (e.getValueIsAdjusting()==false)
               repaint();
           
          }
          
          public void paintComponent(Graphics g)
          {
           super.paintComponent(g);
           g.setFont(new Font("Serif", style,24));
           g.setColor(theColor[names.getSelectedIndex()]);
           g.drawString(message, 50,50);
          }
         }
        }
    }
    Sue

  2. #2
    Join Date
    Sep 2002
    Posts
    124
    Hi.

    I hate to be the author of an anti-climax, but it looks like that dreaded enemy of programmers... the typing error.

    Your line of code in red says "seleted" rather than "selected". Cannot reslove symbol errors often point to a slight, but deadly typing error.

    Regards,
    Joyous Monkey

  3. #3
    Join Date
    Mar 2003
    Posts
    834

    Thumbs up

    ...but excellently presented problem. I can't stand the "it doesn't work" post. Nice one.

    (Oh, and put any new questions on a new thread).
    ArchAngel.
    O:-)

  4. #4
    Join Date
    Sep 2003
    Posts
    21
    Thanks Joyouse Monkey and Arch Angel.
    I swear I wish there were a "spelling" checker and maybe a drop down menu that popped up when keying in variable names so you know what to use and how you originally spelled it when each one was initialized.

    Those little errors "bug" me, pun intended.

    When I get home to recompile my code, I will surely
    post my new questions. This is a neat type of
    panel but I have a lot of questions about it.

    thanks,
    Sue

  5. #5
    Join Date
    Mar 2003
    Posts
    834
    I swear I wish there were a "spelling" checker and maybe a drop down menu that popped up when keying in variable names so you know what to use and how you originally spelled it when each one was initialized.
    That's what IDEs are for....
    ArchAngel.
    O:-)

  6. #6
    Join Date
    Sep 2003
    Posts
    21
    You wouldn't happen to know how to set up the
    NetBeans IDE to do what I mentioned in my wish list
    [spell checker and variable list]?
    I cannot find what I want in the Help section in NetBeans. I have version 3.5.1.
    Sue

  7. #7
    Join Date
    Mar 2003
    Posts
    834
    Sorry, i've never really liked NetBeans. I prefer to use JCreator, IntelliJIDEA or JBuilder.

    (but having said that, normally what you can do in IDEs is to start typing a variable name e.g. "my" and then press ctrl + Space and it will auto-complete the variable e.g. "myIntegerValueWithAReallyLongName". I'd assume NetBeans to have something like this)
    ArchAngel.
    O:-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center