Results 1 to 6 of 6

Thread: How to Use Panels in Java?

  1. #1
    Join Date
    Aug 2006
    Posts
    148

    How to Use Panels in Java?

    Hello friends,
    I am recently started doing Java so I am not having enough knowledge about it. So I thought that some guys over there would be interested in helping me.!! Can anyone explain me how to use Panels in Java.?? Also explain me the types of the Panels that are used in Java. Any other information related to the same topic can also be helpful.!! Please help me as soon as possible.!!
    The Gaming Machine:
    Processor: Core 2 Duo E6400
    Motherboard: Asus P5W DH Deluxe
    RAM: 2GB XMS2 Corsair PC2-6400
    Video: ATI Radeon X1900XT 512MB w/ Arctic Accellero
    Drive: 74GB "WD Raptor" 10000RPM
    Sound: Creative SoundBlaster X-Fi
    Watercooling: *Soon* Zalman Reserator

  2. #2
    Join Date
    Aug 2006
    Posts
    227

    Re: How to Use Panels in Java?

    Since you are new to the topic, I think that you should first know more about the panels before going to the methods and the coding used by the Panel in Java. In Java, Panels don't paint anything except for their background by default. But you can do customization of the colors in the Panels. You can also add the borders and shades to panels. Panels are opaque by default. That means you cannot see the components present behind the panel. But you can make a panel transparent by invoking setOpaque(false). The JPanel class provides general-purpose containers for lightweight components.
    I do to dead flowers what people at morgues do to dead people. Suck all the moisture out, dip them in plastic, paint them up pretty and put them in a nice frame.

  3. #3
    Join Date
    Jul 2006
    Posts
    289

    Re: How to Use Panels in Java?

    I have provided you with an example of the Custom Painting. You can see the following code :
    Code:
    class ImagePanel extends JPanel {
        ...
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
        
            g.drawImage(image, 0, 0, this); 
        
            g.drawImage(image, 75, 20, 420, 74, this);
        }
    }
    After you type the above code, the output would be an image twice, one image would be of natural size and once very wide.
    Signatures reduce available bandwidth

  4. #4
    Join Date
    Jul 2006
    Posts
    442

    Re: How to Use Panels in Java?

    You will also have to set the position and the size of the components of the panel. Because like the other containers, a panel also uses a layout manager to position and size its components. If you have not set the layout manager of the panel it would be FlowLayout because by default, an instance of FlowLayout is a panel's layout manager. You can create a panel by invoking the setLayout method. The following is an example of the same :
    Code:
    JPanel newPanel = new JPanel();
    newPanel.setLayout(new BorderLayout());
    Following is an example of setting the layout manager at instantiation :
    Code:
    JPanelnewPanel = new JPanel(new BorderLayout());
    "When they give you ruled paper, write the other way..." J.R.J.

  5. #5
    Join Date
    Jul 2006
    Posts
    286

    Re: How to Use Panels in Java?

    You will also need to add the components in a panel. You will have to use the add method, for adding components to a panel. You can use following add method, when the layout manager is FlowLayout, BoxLayout, GridLayout, or GridBagLayout :
    Code:
    aFlowPanel.add(aComponent);
    aFlowPanel.add(anotherComponent);
    You will need to provide a second argument specifying the added component's position within the panel, if the layout manager is BorderLayout. You can check the following example :
    Code:
    aBorderPanel.add(aComponent, BorderLayout.CENTER);
    aBorderPanel.add(anotherComponent, BorderLayout.NORTH);
    IF you hate me, please don't mention it. I know who hates me and who doesn't. You really do not have to make fun of people....

  6. #6
    Join Date
    Aug 2006
    Posts
    235

    Re: How to Use Panels in Java?

    The following is a code for BorderLayout, which is very lengthy but you can get the idea of using the panels in Java. The code is :
    Code:
    package layout;
    import javax.swing.*;
    import java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.Dimension;
    
    public class BorderLayoutDemo {
        public static boolean RIGHT_TO_LEFT = false;
        
        public static void addComponentsToPane(Container pane) {
            
            if (!(pane.getLayout() instanceof BorderLayout)) {
                pane.add(new JLabel("Not using the BorderLayout!"));
                return;
            }
            
            if (RIGHT_TO_LEFT) {
                pane.setComponentOrientation(
                        java.awt.ComponentOrientation.RIGHT_TO_LEFT);
            }
            
            JButton button = new JButton("Button 1 (PAGE_START)");
            pane.add(button, BorderLayout.PAGE_START);
            
            button = new JButton("Button 2 (CENTER)");
            button.setPreferredSize(new Dimension(430, 280));
            pane.add(button, BorderLayout.CENTER);
            
            button = new JButton("Button 3 (LINE_START)");
            pane.add(button, BorderLayout.LINE_START);
            
            button = new JButton("Long-Named Button 4 (PAGE_END)");
            pane.add(button, BorderLayout.PAGE_END);
            
            button = new JButton("5 (LINE_END)");
            pane.add(button, BorderLayout.LINE_END);
        }
        
        private static void createAndShowGUI() {
            
            JFrame frame = new JFrame("BorderLayoutDemo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            addComponentsToPane(frame.getContentPane());
            frame.pack();
            frame.setVisible(true);
        }
        
        public static void main(String[] args) {
            try {
                UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
            } catch (UnsupportedLookAndFeelException ex) {
                ex.printStackTrace();
            } catch (IllegalAccessException ex) {
                ex.printStackTrace();
            } catch (InstantiationException ex) {
                ex.printStackTrace();
            } catch (ClassNotFoundException ex) {
                ex.printStackTrace();
            }
            UIManager.put("swing.boldMetal", Boolean.FALSE);
            
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }
    3.2 (northwood)
    2gig ram
    ATI AIW X800xt 256mb
    Gigabyte GA-8knxp 875p Chipset
    Optiwrite 8X DVD Burner
    Win XP PRO Sp2 (Works Perfectly)
    2 SATA Raptor 74gig Raid 0
    2 7200 IDE 320gig HD

Similar Threads

  1. Does a 3D 120 Hz LED IPS panels exists?
    By Life spring in forum Monitor & Video Cards
    Replies: 5
    Last Post: 28-03-2011, 10:38 AM
  2. MSI VGA control panels & Nvidia drivers
    By AxelF in forum Motherboard Processor & RAM
    Replies: 4
    Last Post: 14-03-2011, 12:12 AM
  3. Can't export to Panels folder
    By Delgado in forum Windows Software
    Replies: 5
    Last Post: 17-07-2010, 05:15 AM
  4. HP W2558hc Monitor TN Panels
    By Shreevats in forum Portable Devices
    Replies: 3
    Last Post: 29-05-2009, 05:06 PM
  5. Still down the prices of LCD panels
    By Philbert in forum Monitor & Video Cards
    Replies: 3
    Last Post: 09-12-2008, 06:34 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,715,883,055.33589 seconds with 17 queries