Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Problem with border in program

  1. #1
    Member
    Join Date
    Sep 2011
    Location
    Nanuet, NY USA
    Posts
    33
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Problem with border in program

    Hi,
    I am working on a program that is meant to demonstrate all the different borders that can be applied to a JComponent (a JLabel in this case). The program is an example from the text book I am using. When I compile the program I get the following errors:

    C:\J21work>javac BorderDemo.java
    BorderDemo.java:142: incompatible types
    found : javax.swing.border.EmptyBorder
    required: Border
    Border border = new EmptyBorder(2, 2, 2, 2);
    ^
    BorderDemo.java:145: incompatible types
    found : javax.swing.border.BevelBorder
    required: Border
    border = new BevelBorder(BevelBorder.LOWERED);
    ^
    BorderDemo.java:149: incompatible types
    found : javax.swing.border.BevelBorder
    required: Border
    border = new BevelBorder(BevelBorder.RAISED);
    ^
    BorderDemo.java:153: incompatible types
    found : javax.swing.border.EtchedBorder
    required: Border
    border = new EtchedBorder();
    ^
    BorderDemo.java:157: incompatible types
    found : javax.swing.border.LineBorder
    required: Border
    border = new LineBorder(Color.black, 5);
    ^
    BorderDemo.java:161: incompatible types
    found : javax.swing.border.MatteBorder
    required: Border
    border = new MatteBorder(15, 15, 15, 15,
    ^
    BorderDemo.java:167: incompatible types
    found : javax.swing.border.EmptyBorder
    required: Border
    border = new EmptyBorder(2, 2, 2, 2);
    ^
    BorderDemo.java:197: setBorder(javax.swing.border.Border) in javax.swing.border.
    TitledBorder cannot be applied to (Border)
    jLabel1Border.setBorder(border);
    ^
    BorderDemo.java:203: setBorder(javax.swing.border.Border) in javax.swing.JCompon
    ent cannot be applied to (Border)
    jLabel1.setBorder(border);
    ^
    9 errors

    Here is the code.

    import java.awt.*;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.*;
    import javax.swing.border.*;
     
    public class BorderDemo extends JApplet {
      // Declare a label for displaying message
      private JLabel jLabel1 = new JLabel("Display the border type",
        JLabel.CENTER);
     
      // A check box for selecting a border with or without a title
      private JCheckBox jchkTitled;
     
      // Radio buttons for border styles
      private JRadioButton jrbLoweredBevel, jrbRaisedBevel,
        jrbEtched, jrbLine, jrbMatte, jrbEmpty;
     
      // Radio buttons for titled border options
      private JRadioButton jrbAboveBottom, jrbBottom,
        jrbBelowBottom, jrbAboveTop, jrbTop, jrbBelowTop,
        jrbLeft, jrbCenter, jrbRight;
     
      // TitledBorder for the label
      private TitledBorder jLabel1Border;
     
      /** Constructor */
      public BorderDemo() {
        // Create a JLabel instance and set colors
        jLabel1.setBackground(Color.yellow);
        jLabel1.setBorder(jLabel1Border);
     
        // Place title position radio buttons
        JPanel jpPosition = new JPanel();
        jpPosition.setLayout(new GridLayout(3, 2));
        jpPosition.add(
          jrbAboveBottom = new JRadioButton("ABOVE_BOTTOM"));
        jpPosition.add(jrbAboveTop = new JRadioButton("ABOVE_TOP"));
        jpPosition.add(jrbBottom = new JRadioButton("BOTTOM"));
        jpPosition.add(jrbTop = new JRadioButton("TOP"));
        jpPosition.add(
          jrbBelowBottom = new JRadioButton("BELOW_BOTTOM"));
        jpPosition.add(jrbBelowTop = new JRadioButton("BELOW_TOP"));
        jpPosition.setBorder(new TitledBorder("Position"));
     
        // Place title justification radio buttons
        JPanel jpJustification = new JPanel();
        jpJustification.setLayout(new GridLayout(3,1));
        jpJustification.add(jrbLeft = new JRadioButton("LEFT"));
        jpJustification.add(jrbCenter = new JRadioButton("CENTER"));
        jpJustification.add(jrbRight = new JRadioButton("RIGHT"));
        jpJustification.setBorder(new TitledBorder("Justification"));
     
        // Create panel jpTitleOptions to hold jpPosition and
        // jpJustification
        JPanel jpTitleOptions = new JPanel();
        jpTitleOptions.setLayout(new BorderLayout());
        jpTitleOptions.add(jpPosition, BorderLayout.CENTER);
        jpTitleOptions.add(jpJustification, BorderLayout.EAST);
     
        // Create Panel jpTitle to hold a check box and title position
        // radio buttons, and title justification radio buttons
        JPanel jpTitle = new JPanel();
        jpTitle.setBorder(new TitledBorder("Border Title"));
        jpTitle.setLayout(new BorderLayout());
        jpTitle.add(jchkTitled = new JCheckBox("Titled"),
          BorderLayout.NORTH);
        jpTitle.add(jpTitleOptions, BorderLayout.CENTER);
     
        // Group radio buttons for title position
        ButtonGroup btgTitlePosition = new ButtonGroup();
        btgTitlePosition.add(jrbAboveBottom);
        btgTitlePosition.add(jrbBottom);
        btgTitlePosition.add(jrbBelowBottom);
        btgTitlePosition.add(jrbAboveTop);
        btgTitlePosition.add(jrbTop);
        btgTitlePosition.add(jrbBelowTop);
     
        // Group radio buttons for title justification
        ButtonGroup btgTitleJustification = new ButtonGroup();
        btgTitleJustification.add(jrbLeft);
        btgTitleJustification.add(jrbCenter);
        btgTitleJustification.add(jrbRight);
     
        // Create Panel jpBorderStyle to hold border style radio buttons
        JPanel jpBorderStyle = new JPanel();
        jpBorderStyle.setBorder(new TitledBorder("Border Style"));
        jpBorderStyle.setLayout(new GridLayout(6, 1));
        jpBorderStyle.add(jrbLoweredBevel =
          new JRadioButton("Lowered Bevel"));
        jpBorderStyle.add(jrbRaisedBevel =
          new JRadioButton("Raised Bevel"));
        jpBorderStyle.add(jrbEtched = new JRadioButton("Etched"));
        jpBorderStyle.add(jrbLine = new JRadioButton("Line"));
        jpBorderStyle.add(jrbMatte = new JRadioButton("Matte"));
        jpBorderStyle.add(jrbEmpty = new JRadioButton("Empty"));
     
        // Group radio buttons for border styles
        ButtonGroup btgBorderStyle = new ButtonGroup();
        btgBorderStyle.add(jrbLoweredBevel);
        btgBorderStyle.add(jrbRaisedBevel);
        btgBorderStyle.add(jrbEtched);
        btgBorderStyle.add(jrbLine);
        btgBorderStyle.add(jrbMatte);
        btgBorderStyle.add(jrbEmpty);
     
        // Create Panel jpAllChoices to place jpTitle and jpBorderStyle
        JPanel jpAllChoices = new JPanel();
        jpAllChoices.setLayout(new BorderLayout());
        jpAllChoices.add(jpTitle, BorderLayout.CENTER);
        jpAllChoices.add(jpBorderStyle, BorderLayout.EAST);
     
        // Place panels in the frame
        setLayout(new BorderLayout());
        add(jLabel1, BorderLayout.CENTER);
        add(jpAllChoices, BorderLayout.SOUTH);
     
        // Register listeners
        ActionListener listener = new EventListener();
        jchkTitled.addActionListener(listener);
        jrbAboveBottom.addActionListener(listener);
        jrbBottom.addActionListener(listener);
        jrbBelowBottom.addActionListener(listener);
        jrbAboveTop.addActionListener(listener);
        jrbTop.addActionListener(listener);
        jrbBelowTop.addActionListener(listener);
        jrbLeft.addActionListener(listener);
        jrbCenter.addActionListener(listener);
        jrbRight.addActionListener(listener);
        jrbLoweredBevel.addActionListener(listener);
        jrbRaisedBevel.addActionListener(listener);
        jrbLine.addActionListener(listener);
        jrbEtched.addActionListener(listener);
        jrbMatte.addActionListener(listener);
        jrbEmpty.addActionListener(listener);
      }
     
      private class EventListener implements ActionListener {
        /** Handle ActionEvents on check box and radio buttons */
        public void actionPerformed(ActionEvent e) {
          // Get border style
          Border border =  new EmptyBorder(2, 2, 2, 2);
     
          if (jrbLoweredBevel.isSelected()) {
            border =  new BevelBorder(BevelBorder.LOWERED);
            jLabel1.setText("Lowered Bevel Style");
          }
          else if (jrbRaisedBevel.isSelected()) {
            border = new BevelBorder(BevelBorder.RAISED);
            jLabel1.setText("Raised Bevel Style");
          }
          else if (jrbEtched.isSelected()) {
            border = new EtchedBorder();
            jLabel1.setText("Etched Style");
          }
          else if (jrbLine.isSelected()) {
            border = new LineBorder(Color.black, 5);
            jLabel1.setText("Line Style");
          }
          else if (jrbMatte.isSelected()) {
            border = new MatteBorder(15, 15, 15, 15,
              new ImageIcon(getClass().getResource
                ("/image/caIcon.gif")));
            jLabel1.setText("Matte Style");
          }
          else if (jrbEmpty.isSelected()) {
            border = new EmptyBorder(2, 2, 2, 2);
            jLabel1.setText("Empty Style");
          }
     
          if (jchkTitled.isSelected()) {
            // Get the title position and justification
            int titlePosition = TitledBorder.DEFAULT_POSITION;
            int titleJustification = TitledBorder.DEFAULT_JUSTIFICATION;
     
            if (jrbAboveBottom.isSelected())
              titlePosition = TitledBorder.ABOVE_BOTTOM;
            else if (jrbBottom.isSelected())
              titlePosition = TitledBorder.BOTTOM;
            else if (jrbBelowBottom.isSelected())
              titlePosition = TitledBorder.BELOW_BOTTOM;
            else if (jrbAboveTop.isSelected())
              titlePosition = TitledBorder.ABOVE_TOP;
            else if (jrbTop.isSelected())
              titlePosition = TitledBorder.TOP;
            else if (jrbBelowTop.isSelected())
              titlePosition = TitledBorder.BELOW_TOP;
     
            if (jrbLeft.isSelected())
              titleJustification = TitledBorder.LEFT;
            else if (jrbCenter.isSelected())
              titleJustification = TitledBorder.CENTER;
            else if (jrbRight.isSelected())
              titleJustification = TitledBorder.RIGHT;
     
            jLabel1Border = new TitledBorder("A Title");
            jLabel1Border.setBorder(border);
            jLabel1Border.setTitlePosition(titlePosition);
            jLabel1Border.setTitleJustification(titleJustification);
            jLabel1.setBorder(jLabel1Border);
          }
          else {
            jLabel1.setBorder(border);
          }
        }
      }
     
      public static void main(String[] args) {
        BorderDemo applet = new BorderDemo();
        JFrame frame = new JFrame();
        //EXIT_ON_CLOSE == 3
        frame.setDefaultCloseOperation(3);
        frame.setTitle("BorderDemo");
        frame.getContentPane().add(applet, BorderLayout.CENTER);
        applet.init();
        applet.start();
        frame.setSize(400,320);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    }

    I thought that since the border classes used each extend AbstractBorder which implements Border there shouldn't be a compatibility issue since they are all instances of the border interface. How do I fix this?

    Thanks for any help,
    Casey


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,298
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: Problem with border in program

    Your program works fine for me. Try clearing out any old versions of compiled classes?

    Also, for future reference, you should post your programs in SSCCE form- there's really no need to post your entire program. Narrow it down to as little code as possible- in your case, a single component with a border should do the trick. Although your problem seems to be in your environment, so it's a bit of a moot point here.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    kc120us (January 25th, 2012)

  4. #3
    Member
    Join Date
    Sep 2011
    Location
    Nanuet, NY USA
    Posts
    33
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Problem with border in program

    Quote Originally Posted by KevinWorkman View Post
    Your program works fine for me. Try clearing out any old versions of compiled classes?

    Also, for future reference, you should post your programs in SSCCE form- there's really no need to post your entire program. Narrow it down to as little code as possible- in your case, a single component with a border should do the trick. Although your problem seems to be in your environment, so it's a bit of a moot point here.
    That was it. I had another class named in Border in the same directory as BorderDemo. Thanks.

Similar Threads

  1. JFrame invisible border issue
    By gargamel7 in forum AWT / Java Swing
    Replies: 2
    Last Post: September 25th, 2011, 04:32 PM
  2. [SOLVED] Really simple program problem
    By Jacksontbh in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 29th, 2011, 10:15 PM
  3. how two create two 8*8 button border
    By mr.p4r4d0x in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 26th, 2011, 10:25 AM
  4. Tic-Tac-Toe program problem help
    By MuffinMcFluffin in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 25th, 2009, 09:14 PM
  5. About Title border in jscrollpane
    By subhvi in forum AWT / Java Swing
    Replies: 2
    Last Post: December 14th, 2009, 11:45 PM