How locate a JPanel in a JPanel (SOS !!!)

I add the class affectation (see below) in a JPanel but I can't succeed in locating it in this JPanel..
I have tried to use setLayout(null) and to use setLocate and setBounds but it doesn't work..
Thanks
public class Affectation extends JPanel{
public Affectation(int deb, int fin,Color couleur){
this.setBackground(couleur);
this.setBorder(BorderFactory.createLineBorder(Color.black));
//this.setBounds(new Rectangle(deb,10,fin-deb,20));
this.setLocation(deb, 7);
this.setPreferredSize (new Dimension(fin-deb,20));
this.setMinimumSize (new Dimension(fin-deb,20));
this.setMaximumSize (new Dimension(fin-deb,20));
}

You have to locate Affectation in the container you add it to.
JPanel otherPanel = new JPanel(new NullLayout());
Affectation myAffectation = new Affection( deb, fin, coleur);
otherPanel.add(myAffectation);
myAffectation.setLocation(deb, 7);
other something like that.
Cheers
DB

Similar Messages

  • How to add JPanel in  JComboBox?

    Hi.
    How to add JPanel in JComboBox...?
    Regards
    Bilal

    I do not know much about Cardlayout.
    As far as I know, it is a layout which let you assign a serveral 'card' and exist together. By consider the order of the card, you could change the view of each page by using method- first, last, next etc. However, I would like to know what if I got a number of buttons, say A B C D E F, and having cards called a b c d e f,I know I could change from a to b, but what if I want to change from c to e by pressing the button E (assuming it showing the c card now).
    Actually, I was able to create the code now which adding one JPanel to another now. First create a JPanel with prefer size, min max size.
    then add a JPanel into the CENTER of the above JPanel, both using borderlayout.
    But having a little difficulties, could I add more JPanel into the CENTER such that one overlapping each other, by those Buttons, through action and event, hide those which I do not want to show?
    Like A B C buttons, a b c panels. When press A hide b c and show a, so on.
    Could I use method like movetofront(something like that, forgot detail which read in a book) to do this?
    Cheers

  • Set Location of JPanel in another JPanel

    Hello, i want to set the location of a JPanel in another JPanel.
    This is my code:
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    package nemo;
    * @author Administrator
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.*;
    public class BackgroundImage extends JFrame
         JScrollPane scrollPane;
         ImageIcon icon;
            ImageIcon iconDrinken;
         Image image;
         public BackgroundImage() {
              icon = new ImageIcon("startpage.JPG");
                    iconDrinken = new ImageIcon("drankje.JPG");
              JPanel panel = new JPanel() {
                   protected void paintComponent(Graphics g) {
                        //  Dispaly image at at full size
                        g.drawImage(icon.getImage(), 0, 0, null);
                        super.paintComponent(g);
                    JPanel panelDrinks = new JPanel() {
                        protected void paintComponent(Graphics g) {
                            g.drawImage(iconDrinken.getImage(), 0, 0, null);
                            super.paintComponent(g);
                    panelDrinks.setOpaque( false );
              panelDrinks.setPreferredSize( new Dimension(200, 215) );
              panel.setOpaque( false );
              panel.setPreferredSize( new Dimension(400, 400) );
              scrollPane = new JScrollPane( panel );
              getContentPane().add( scrollPane );
                    panel.add(panelDrinks);
         public static void main(String [] args)
              BackgroundImage frame = new BackgroundImage();
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.setSize(800, 600);
              frame.setLocationRelativeTo( null );
              frame.setVisible(true);
    }When i execute this i get this:
    [http://img208.imageshack.us/img208/738/nowml5.jpg]
    And i want the image in the first white box from the left.
    But how to do it???
    I've tried .setLocation and .setBounds
    Edited by: Sven.nl on Apr 17, 2008 2:42 AM
    Edited by: Sven.nl on Apr 17, 2008 2:43 AM

    I already found it. Thanks anyway.
                    JPanel panelDrinks = new JPanel() {
                        protected void paintComponent(Graphics g) {
                            g.drawImage(iconDrinken.getImage(), 100, 200, 200, 215, null);
                            super.paintComponent(g);
                    panelDrinks.setOpaque( false );
              panelDrinks.setPreferredSize( new Dimension(800, 600) );
                    panelDrinks.setLocation(300, 300);
                      Edited by: Sven.nl on Apr 17, 2008 3:05 AM

  • How to resize JPanel at Runtime ??

    Hi,
    Our Project me t a problem as below, we hope to resize JPanel at Runtime , not at design time, ie, when we first click the button called "Move JPanel" , then we click the JPanel, then we can drag it around within main panel, but we hope to resize the size of this JPanel when we point to the border of this JPanel then drag its border to zoom in or zoom out this JPanel.
    Please advice how to do that??
    Thanks in advance.
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    import java.util.Vector;
    import javax.swing.*;
    import javax.swing.border.LineBorder;
    import javax.swing.event.*;
    public class ResizeJPanels extends JPanel
        protected JLabel label1, label2, label3, label4, labeltmp;
        protected JLabel[] labels;
        protected JPanel[] panels;
        protected JPanel selectedJPanel;
        protected JButton btn  = new JButton("Move JPanel");
        int cx, cy;
        protected Vector order = new Vector();      
         public static void main(String[] args)
            JFrame f = new JFrame("Test");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.getContentPane().add(new ResizeJPanels().GroupJLabels());
            f.setSize(600,700);
            f.setLocation(200,200);
            f.setVisible(true);
         private MouseListener ml = new MouseAdapter() {  
              public void mousePressed(MouseEvent e) {   
                   Point p = e.getPoint();      
                   JPanel jp = new JPanel();
                   jp.setLayout(null);
                   Component[] c = ((JPanel)e.getSource()).getComponents();
                   System.out.println("c.length = " + c.length);      
                   for(int j = 0; j < c.length; j++) {        
                        if(c[j].getBounds().contains(p)) {     
                             if(selectedJPanel != null && selectedJPanel != (JPanel)c[j])
                                  selectedJPanel.setBorder(BorderFactory.createEtchedBorder());
                             selectedJPanel = (JPanel)c[j];            
                             selectedJPanel.setBorder(BorderFactory.createLineBorder(Color.green));
                             break;             
                        add(jp);
                        revalidate();
    public JPanel GroupJLabels ()
              setLayout(null);
            addLabels();
            label1.setBounds( 125,  150, 125, 25);
            label2.setBounds(425,  150, 125, 25);
            label3.setBounds( 125, 575, 125, 25);
            label4.setBounds(425, 575, 125, 25);
            //add(btn);
            btn.setBounds(10, 5, 205, 25);
                add(btn);
           determineCenterOfComponents();
            ComponentMover mover = new ComponentMover();
             ActionListener lst = new ActionListener() {
                 public void actionPerformed(ActionEvent e) {
                     ComponentMover mover = new ComponentMover();
                           addMouseListener(mover);
                           addMouseMotionListener(mover);
              btn.addActionListener(lst);
              addMouseListener(ml); 
              return this;
        public void paintComponent(final Graphics g)
             super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
             Point[] p;
            g2.setStroke(new BasicStroke(4f));
           for(int i = 0 ; i < order.size()-1; i++) {
                JPanel l1 = (JPanel)order.elementAt(i);
                JPanel l2 = (JPanel)order.elementAt(i+1);
                p = getCenterPoints(l1, l2);
                g2.setColor(Color.black);
               // g2.draw(new Line2D.Double(p[0], p[1]));            
        private Point[] getCenterPoints(Component c1, Component c2)
            Point
                p1 = new Point(),
                p2 = new Point();
            Rectangle
                r1 = c1.getBounds(),
                r2 = c2.getBounds();
                 p1.x = r1.x + r1.width/2;
                 p1.y = r1.y + r1.height/2;
                 p2.x = r2.x + r2.width/2;
                 p2.y = r2.y + r2.height/2;
            return new Point[] {p1, p2};
        private void determineCenterOfComponents()
            int
                xMin = Integer.MAX_VALUE,
                yMin = Integer.MAX_VALUE,
                xMax = 0,
                yMax = 0;
            for(int i = 0; i < labels.length; i++)
                Rectangle r = labels.getBounds();
    if(r.x < xMin)
    xMin = r.x;
    if(r.y < yMin)
    yMin = r.y;
    if(r.x + r.width > xMax)
    xMax = r.x + r.width;
    if(r.y + r.height > yMax)
    yMax = r.y + r.height;
    cx = xMin + (xMax - xMin)/2;
    cy = yMin + (yMax - yMin)/2;
    private class ComponentMover extends MouseInputAdapter
    Point offsetP = new Point();
    boolean dragging;
    public void mousePressed(MouseEvent e)
    Point p = e.getPoint();
    for(int i = 0; i < panels.length; i++)
    Rectangle r = panels[i].getBounds();
    if(r.contains(p))
    selectedJPanel = panels[i];
    order.addElement(panels[i]);
    offsetP.x = p.x - r.x;
    offsetP.y = p.y - r.y;
    dragging = true;
    repaint(); //added
    break;
    public void mouseReleased(MouseEvent e)
    dragging = false;
    public void mouseDragged(MouseEvent e)
    if(dragging)
    Rectangle r = selectedJPanel.getBounds();
    r.x = e.getX() - offsetP.x;
    r.y = e.getY() - offsetP.y;
    selectedJPanel.setBounds(r.x, r.y, r.width, r.height);
    //determineCenterOfComponents();
    repaint();
    private void addLabels()
    label1 = new JLabel("Label 1");
    label2 = new JLabel("Label 2");
    label3 = new JLabel("Label 3");
    label4 = new JLabel("Label 4");
    labels = new JLabel[] {
    label1, label2, label3, label4
    JLabel jl = new JLabel("This is resizeable JPanel at Runtime");
    jl.setBackground(Color.green);
    jl.setOpaque(true);
              jl.setFont(new Font("Helvetica", Font.BOLD, 18));
    JPanel jp = new JPanel();
    jp.setLayout(new BorderLayout());
    panels = new JPanel[]{jp};
              jp.setBorder(new LineBorder(Color.black, 3, false));
    jp.setPreferredSize(new Dimension(400,200));
    jp.add(jl, BorderLayout.NORTH);
    for(int i = 0; i < labels.length; i++)
    labels[i].setHorizontalAlignment(SwingConstants.CENTER);
    labels[i].setBorder(BorderFactory.createEtchedBorder());
    jp.add(labels[i], BorderLayout.CENTER);
    jp.setBounds(100, 100, 400,200);
    add(jp);

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.*;
    import javax.swing.event.MouseInputAdapter;
    public class Resizing extends JPanel {
        public Resizing() {
            super(null);
            addPanel();
            PanelControlAdapter control = new PanelControlAdapter(this);
            addMouseListener(control);
            addMouseMotionListener(control);
        private void addPanel() {
            JLabel jl = new JLabel("This is resizeable JPanel at Runtime");
            jl.setBackground(Color.green);
            jl.setOpaque(true);
            jl.setFont(new Font("Helvetica", Font.BOLD, 18));
            JPanel jp = new JPanel();
            jp.setLayout(new BorderLayout());
            jp.setBorder(new LineBorder(Color.black, 3, false));
            jp.add(jl, BorderLayout.NORTH);
            jp.setBounds(50,50,400,200);
            add(jp);
        public static void main(String[] args) {
            JFrame f = new JFrame("Test");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.getContentPane().add(new Resizing());
            f.setSize(500,400);
            f.setLocation(100,100);
            f.setVisible(true);
    class PanelControlAdapter extends MouseInputAdapter {
        Resizing host;
        Component selectedComponent;
        LineBorder black;
        LineBorder green;
        Point offset = new Point();
        Point start = new Point();
        boolean dragging = false;
        boolean resizing = false;
        public PanelControlAdapter(Resizing r) {
            host = r;
            black = new LineBorder(Color.black, 3, false);
            green = new LineBorder(Color.green, 3, false);
        public void mouseMoved(MouseEvent e) {
            Point p = e.getPoint();
            boolean hovering = false;
            Component c = host.getComponent(0);
            Rectangle r = c.getBounds();
            if(r.contains(p)) {
                hovering = true;
                if(selectedComponent != c) {
                    if(selectedComponent != null)  // reset
                        ((JComponent)selectedComponent).setBorder(black);
                    selectedComponent = c;
                    ((JComponent)selectedComponent).setBorder(green);
                if(overBorder(p))
                    setCursor(p);
                else if(selectedComponent.getCursor() != Cursor.getDefaultCursor())
                    selectedComponent.setCursor(Cursor.getDefaultCursor());
            if(!hovering && selectedComponent != null) {
                ((JComponent)selectedComponent).setBorder(black);
                selectedComponent = null;
        private boolean overBorder(Point p) {
            Rectangle r = selectedComponent.getBounds();
            JComponent target = (JComponent)selectedComponent;
            Insets insets = target.getBorder().getBorderInsets(target);
            // Assume uniform border insets.
            r.grow(-insets.left, -insets.top);
            return !r.contains(p);
        private void setCursor(Point p) {
            JComponent target = (JComponent)selectedComponent;
            AbstractBorder border = (AbstractBorder)target.getBorder();
            Rectangle r = target.getBounds();
            Rectangle ir = border.getInteriorRectangle(target, r.x, r.y, r.width, r.height);
            int outcode = ir.outcode(p.x, p.y);
            Cursor cursor;
            switch(outcode) {
                case Rectangle.OUT_TOP:
                    cursor = Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR);
                    break;
                case Rectangle.OUT_TOP + Rectangle.OUT_LEFT:
                    cursor = Cursor.getPredefinedCursor(Cursor.NW_RESIZE_CURSOR);
                    break;
                case Rectangle.OUT_LEFT:
                    cursor = Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR);
                    break;
                case Rectangle.OUT_LEFT + Rectangle.OUT_BOTTOM:
                    cursor = Cursor.getPredefinedCursor(Cursor.SW_RESIZE_CURSOR);
                    break;
                case Rectangle.OUT_BOTTOM:
                    cursor = Cursor.getPredefinedCursor(Cursor.S_RESIZE_CURSOR);
                    break;
                case Rectangle.OUT_BOTTOM + Rectangle.OUT_RIGHT:
                    cursor = Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR);
                    break;
                case Rectangle.OUT_RIGHT:
                    cursor = Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR);
                    break;
                case Rectangle.OUT_RIGHT + Rectangle.OUT_TOP:
                    cursor = Cursor.getPredefinedCursor(Cursor.NE_RESIZE_CURSOR);
                    break;
                default:
                    cursor = Cursor.getDefaultCursor();
            selectedComponent.setCursor(cursor);
        public void mousePressed(MouseEvent e) {
            Point p = e.getPoint();
            if(selectedComponent != null) {
                Rectangle r = selectedComponent.getBounds();
                if(selectedComponent.getCursor() == Cursor.getDefaultCursor()) {
                    offset.x = p.x - r.x;
                    offset.y = p.y - r.y;
                    dragging = true;
                } else {
                    start = p;
                    resizing = true;
        public void mouseReleased(MouseEvent e) {
            dragging = false;
            resizing = false;
        public void mouseDragged(MouseEvent e) {
            Point p = e.getPoint();
            if(dragging || resizing) {
                Rectangle r = selectedComponent.getBounds();
                if(dragging) {
                    r.x = p.x - offset.x;
                    r.y = p.y - offset.y;
                    selectedComponent.setLocation(r.x, r.y);
                } else if(resizing) {
                    int type = selectedComponent.getCursor().getType();
                    switch(type) {
                        case Cursor.N_RESIZE_CURSOR:
                            r.height -= p.y - start.y;
                            r.y = p.y;
                            break;
                        case Cursor.NW_RESIZE_CURSOR:
                            r.width -= p.x - start.x;
                            r.x = p.x;
                            r.height -= p.y - start.y;
                            r.y = p.y;
                            break;
                        case Cursor.W_RESIZE_CURSOR:
                            r.width -= p.x - start.x;
                            r.x = p.x;
                            break;
                        case Cursor.SW_RESIZE_CURSOR:
                            r.width -= p.x - start.x;
                            r.x = p.x;
                            r.height += p.y - start.y;
                            break;
                        case Cursor.S_RESIZE_CURSOR:
                            r.height += p.y - start.y;
                            break;
                        case Cursor.SE_RESIZE_CURSOR:
                            r.width += p.x - start.x;
                            r.height += p.y - start.y;
                            break;
                        case Cursor.E_RESIZE_CURSOR:
                            r.width += p.x - start.x;
                            break;
                        case Cursor.NE_RESIZE_CURSOR:
                            r.width += p.x - start.x;
                            r.height -= p.y - start.y;
                            r.y = p.y;
                            break;
                        default:
                            System.out.println("Unexpected resize type: " + type);
                    selectedComponent.setBounds(r.x, r.y, r.width, r.height);
                    start = p;
    }

  • Help with placing the JComponents at a particular location in JPanel

    Hello All,
    I think it is simple in placing JLabel with an icon or just just text at a particular location in the JPanel. But it is notworking. I know it is very simple but i think i am missing something. Here is code please help with this code.
    Thanks
    Smithaimport java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.FlowLayout;
    import java.awt.LayoutManager;
    import javax.swing.*;
    public class DocumentGraphicalRepresentation {
         public void buildDocumentation() {
              JFrame frame = new JFrame("Graphical Representation");
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.setBounds(0, 0, 475, 265);
              JPanel panel = new JPanel();          
              panel.setBounds(0, 0, 475, 265);
              panel.setBackground(Color.lightGray);
              //panel.setLayout(new BorderLayout());          
              JLabel label0 = new JLabel(new ImageIcon("c:/arrow.gif"));
              label0.setLocation(61, 68);
              label0.setOpaque(false);
              panel.add(label0);
              JLabel label1 = new JLabel(new ImageIcon("c:/bulb.gif"));
              label1.setLocation(338, 139);
              label1.setOpaque(false);
              panel.add(label1);
              frame.getContentPane().add(panel);
              frame.setVisible(true);          
         public static void main(String args[]) {
              DocumentGraphicalRepresentation document = new DocumentGraphicalRepresentation();
              document.buildDocumentation();
    }

    When adding components to a null layout you must set both a location and size for them to show up.
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.LayoutManager;
    import javax.swing.*;
    public class DGR {
         public void buildDocumentation() {
              JFrame frame = new JFrame("Graphical Representation");
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.setBounds(0, 0, 475, 265);
              JPanel panel = new JPanel();          
              panel.setBounds(0, 0, 475, 265);
              panel.setBackground(Color.lightGray);
              //panel.setLayout(new BorderLayout());          
              JLabel label0 = new JLabel(new ImageIcon(//"c:/arrow.gif"));
                                         "images/geek/geek-----.gif"));
                    Dimension d = label0.getPreferredSize();
                    label0.setSize(d);
              label0.setLocation(61, 68);
    //          label0.setOpaque(false);
              panel.add(label0);
              JLabel label1 = new JLabel(new ImageIcon(//"c:/bulb.gif"));
                                         "images/hawk.jpg"));
                    d = label1.getPreferredSize();
                    label1.setBounds(338,139,d.width,d.height);
    //          label1.setLocation(338, 139);
              System.out.println("label1.isOpaque = " + label1.isOpaque());
    //          label1.setOpaque(false);
              panel.add(label1);
              frame.getContentPane().add(new JScrollPane(panel));
              frame.setVisible(true);          
         public static void main(String args[]) {
              new DGR().buildDocumentation();
    }

  • Locate a JPanel in an other JPanel (HELP !!!)

    I add the class affectation (see below) in a JPanel but I can't succeed in locating it in this JPanel..
    I have tried to use setLocate and setBounds but it doesn't work
    public class Affectation extends JPanel{
    public Affectation(int deb, int fin,Color couleur){
    this.setBackground(couleur);
    this.setBorder(BorderFactory.createLineBorder(Color.black));
    //this.setBounds(new Rectangle(deb,10,fin-deb,20));
    this.setLocation(deb, 7);
    this.setPreferredSize (new Dimension(fin-deb,20));
              this.setMinimumSize (new Dimension(fin-deb,20));
              this.setMaximumSize (new Dimension(fin-deb,20));
    }

    Here's an example that uses absolute positioning to get a drag effect:import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class Test extends JFrame {
        public Test () {
            Dimension sizeA = new Dimension (300, 300);
            Dimension sizeB = new Dimension (300, 300);
            DragPanel dragPanelA = new DragPanel (sizeA, new Rectangle (100, 100, 100, 100), Color.YELLOW);
            DragPanel dragPanelB = new DragPanel (sizeB, new Rectangle (100, 100, 100, 100), Color.GREEN);
            WrapPanel wrapPanelA = new WrapPanel (sizeA, dragPanelA, Color.GREEN);
            WrapPanel wrapPanelB = new WrapPanel (sizeB, dragPanelB, Color.YELLOW);
            getContentPane ().setLayout (new GridLayout (1, 2));
            getContentPane ().add (wrapPanelA);
            getContentPane ().add (wrapPanelB);
            setDefaultCloseOperation (DISPOSE_ON_CLOSE);
            setResizable (false);
            setTitle ("Drag those panels!");
            pack ();
            setLocationRelativeTo (null);
            show ();
        private class WrapPanel extends JPanel {
            private Dimension size;
            public WrapPanel (Dimension size, Component component, Color background) {
                this.size = size;
                setLayout (null);
                add (component);
                setBackground (background);
            public Dimension getPreferredSize () {
                return size;
        private class DragPanel extends JPanel {
            private Dimension outerSize;
            private Rectangle bounds;
            public DragPanel (Dimension outerSize, Rectangle bounds, Color background) {
                this.outerSize = outerSize;
                this.bounds = bounds;
                setBounds (bounds);
                setBackground (background);
                DragListener dragListener = new DragListener ();
                addMouseListener (dragListener);
                addMouseMotionListener (dragListener);
            private class DragListener implements MouseListener, MouseMotionListener {
                private Point previousPoint;
                public DragListener () {
                    previousPoint = null;
                public void mousePressed (MouseEvent event) {
                    previousPoint = event.getPoint ();
                public void mouseReleased (MouseEvent event) {
                    previousPoint = null;
                public void mouseDragged (MouseEvent event) {
                    Point point = event.getPoint ();
                    int dx = point.x - previousPoint.x;
                    int dy = point.y - previousPoint.y;
                    bounds.x = getBetween (bounds.x + dx, 0, outerSize.width - bounds.width);
                    bounds.y = getBetween (bounds.y + dy, 0, outerSize.height - bounds.height);
                    setBounds (bounds);
                private int getBetween (int value, int lower, int upper) {
                    return (value < lower) ? lower : (value > upper) ? upper : value;
                public void mouseClicked (MouseEvent event) {}
                public void mouseEntered (MouseEvent event) {}
                public void mouseExited (MouseEvent event) {}
                public void mouseMoved (MouseEvent event) {}
        public static void main (String[] parameters) {
            new Test ();
    }Alternatively, you could create a super wrap panel (or something similar) to place the wrap panels in.
    Kind regards,
      Levi

  • How to find JPanel viewable or drawing area size

    hi,
    I have a two small question. First one is, I have a JPanel what I need is to find the viewable area size or the drawing size of the Panel. I don't want to get the size of the Title bar. All I need is how to find the actual drawing area not including the title bar. I know how to get the total size of the JPanel, I just needs to know how to get the drawing area size where you put your components. e.g. if the size of my Panel is (300,300) then it also includes the title bar and the border too. But I need to find or get only the area size where we actually put our components.
    Another question is how can I get the size of my Title bar means the width and height.
    I'll really appreciate for any help
    Thanks

    Er... what's wrong with panel.getSize()?

  • How to make JPanel as JTable Cell Editor (Urgent help needed)

    Hi!
    I want to make JPanel (with a JTextField, 3 JLabels and 1 JTextArea) as cell editor for one column. Can somebody help me on this?
    Does anybody have any sample code? I will greatly appreciate ur help.
    Thanks,
    Snehal

    Okie. In "How to use JTables" page found in the JAVA website (there is a link from the JTable section of the JAVA API documentation), there is a sample code for a sample color chooser. What this does is, adds a button with its action listener set to pop up a color chooser window. In this, rendering is still through a JLabel.
    This example would be of good help to you. Plus, this example gives me a feeling that what u ask is possible.

  • How can make Jpanel response click event?(help!)

    in a program i want to make jpanel response mouseclicked event, but jpanel class does not provide addActionListener(). through which way? i am able to arrive at the purpose! (create a new class extends jpanel, how to do?)
    thank a lot!!!

    Hello,
    have a look:import java.awt.Color;
    import java.awt.event.*;
    import javax.swing.*;
    public class ClickPanel extends JPanel implements MouseListener
         public static void main(String[]args)
              JFrame frame = new JFrame("Click-Panel");
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.getContentPane().setLayout(null);
              frame.getContentPane().add(new ClickPanel());
              frame.setBounds(200,200,400,400);
              frame.setVisible(true);
         public ClickPanel()
              addMouseListener(this);
              setBounds(200,200,100,100);
              setBackground(Color.CYAN);
         public void mouseClicked(MouseEvent e)
              System.out.println("clicked");
         public void mouseEntered(MouseEvent e)
              System.out.println("entered");
              setBackground(Color.ORANGE);
         public void mouseExited(MouseEvent e)
              System.out.println("exited");
              setBackground(Color.CYAN);
         public void mousePressed(MouseEvent e)
              System.out.println("pressed");
         public void mouseReleased(MouseEvent e)
              System.out.println("released");
    }regards,
    Tim

  • [GUI programming] How do two JPanels interact?

    Hi, all
    I've been looking if there is a standard/mostly-used way implementing GUI applications. e.g.,
    under a JFrame, there are 2 MyPanel exntending JPanel, one placed on the leftside, the other on the rightside. they both have got a bunch of JButtons, JLabels, JList, etc. this is the view.
    but for the control part, here comes the question: I'd would like PanelA to change background color if a Button on PanelB is pressed. where should I place this part of code that controls what happen. I've considered some possibilites, but not sure which one is the best.
    1. implement it in main() like: if PanelA.Button.isPressed(), then PanelB.setBackground(Green)
    pros: centrailized control
    cons: could lead to long & messy code if there are many Panels needed to control
    2. PanelA as Observer, PanelB Observable
    pros: doable
    cons: not very convenient
    3. PanelA & PanelB both singleton, so they can reference to each other directly
    pros: easy implementing
    cons: less flexibility
    if none of them is the way you do, how do you do it then?
    any help will be appreciated.
    thanks.

    You missed the obvious one, just let the controlling panel have a reference to the color panel. In your frame:
    ColoredPanel coloredPanel = new ColoredPanel();
    ControlPanel control = new ControlPanel(coloredPanel);
    // add to conten pane, etcThough ultimately your option two is the best one.

  • How to save JPanel in HashMap and then build tree using this HashMap

    Actually I'm not sure I'm on right way... what I need to do is to build a tree of tests.
    I can imagine that this can be done in such way
    1. add all jpanels into hash map HashMap<String, JPanel> or HashMap<Vector, JPanel> ?
    2. build tree from this hash map
    3. each node contains jpanel
    +root
    |
    +suite1
    |
    test1 -------------> jpanel with components
    |
    test2
    |
    +suite2
    |
    test21
    |
    test22
    If somebody has an idea how to cope with this, please help!

    Yes, I've tried, but i didn't find appropriate option. In Easy setup I can only choose presets. I've tried to edit capture presets, but there were no Log and Capture window settings.

  • How to make JPanel to transparent?

    Dear all, anyone here know how to make 2 JPanel to transparent and overlabbing? i try to use opaque(boolean), but i fail to do it, here is my code, i really need ur help.......thankx a lot
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class Main extends JApplet
         public void init()
              DrawBase db=new DrawBase();
              DrawGraph dg=new DrawGraph();
              Container container=getContentPane();
              db.setOpaque(true);
              dg.setOpaque(true);
              container.add(db,BorderLayout.CENTER);
              container.add(dg,BorderLayout.CENTER);
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class DrawBase extends JPanel
         public DrawBase()
         public void paintComponent(Graphics g)
              super.paintComponent(g);
              g.setColor(new Color(0,255,0));
              g.drawOval(50,50,50,50);
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class DrawGraph extends JPanel
         public DrawGraph()
         public void paintComponent(Graphics g)
              super.paintComponent(g);
              g.setColor(new Color(255,0,0));
              g.drawOval(20,10,10,10);

    Hi,
    Dear all, anyone here know how to make 2 JPanel to
    transparent and overlabbing? i try to use
    opaque(boolean), but i fail to do it, here is my code,
    i really need ur help.......thankx a lot
    Well, the transparent part is easy. You should be calling setOpaque(false) rather than setOpaque(true). This will eliminate background painting of the JPanels.
    The overlapping can be accomplished in multiple ways, but NOT using BorderLayout. You'll either want to do as one person suggested and use no layout manager and position things absolutely, or as another suggested and use JLayeredPane.
    Personally, I would suggest a third approach that uses a single JPanel to do all of the painting. Something like this would work nicely:
    public class DrawPanel extends JPanel {
        public DrawPanel() {
            setOpaque(false);
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            drawBase();
            drawGraph();
        public void drawBase() {
            g.setColor(new Color(0,255,0));
            g.drawOval(50,50,50,50);
        public void drawGraph() {
            g.setColor(new Color(255,0,0));
            g.drawOval(20,10,10,10);
    }Regards,
    Shannon Hickey (Swing Team)

  • How to maximize JPanel

    Hi,
    I have some components on JPanel and I must maximize JPanel.
    Is there a method in JPanel similar to setExtendedState of JFrame?
    Thanks

    [url http://java.sun.com/docs/books/tutorial/uiswing/layout/visual.html]How to Use Layout Managers
    [url http://www.physci.org/codes/sscce.jsp]How to Post Demo Code, and don't forget to use the "code" tags to the code retains its formatting.

  • How to refresh JPanel?

    Hello
    I'm writing applet and I got stuck. I have JPanel which is in createAndShowGUI() and it's invoked from init() with invokeAndWait. This JPanel contains array of images. I'd like to
    remove JPanel and paint different configuration of images. All in the same applet window. This would happen after I pushed one of the images.
    How can it be done?.
    Cheers
    Message was edited by:
    macmacmac

    you can use keyword "repaint". you can write that keyword to the method void paintComponent(Graphics g). or you can use remove method from JPanel and create the new JPanel

  • How to convert JPanel code into JApplet?

    i made a graph in JPanel.now i want to call that graph in my jsp page.
    for this purpose i need to convert that JPanel graph into JApplet.
    does anyone knows about this...
    regards

    generate an image from your graphic then embed it into your html code <img src="...">
    example:
    import java.awt.BasicStroke;
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.FontMetrics;
    import java.awt.GradientPaint;
    import java.awt.Graphics2D;
    import java.awt.geom.Ellipse2D;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    public class WriteImageType {
      static public void main(String args[]) throws Exception {
        try {
          int width = 200, height = 200;
          // TYPE_INT_ARGB specifies the image format: 8-bit RGBA packed
          // into integer pixels
          BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
          Graphics2D ig2 = bi.createGraphics();
          Font font = new Font("TimesRoman", Font.BOLD, 20);
          ig2.setFont(font);
          String message = "www.java2s.com!";
          FontMetrics fontMetrics = ig2.getFontMetrics();
          int stringWidth = fontMetrics.stringWidth(message);
          int stringHeight = fontMetrics.getAscent();
          ig2.setPaint(Color.black);
          ig2.drawString(message, (width - stringWidth) / 2, height / 2 + stringHeight / 4);
          ImageIO.write(bi, "PNG", new File("c:\\yourImageName.PNG"));
          ImageIO.write(bi, "JPEG", new File("c:\\yourImageName.JPG"));
          ImageIO.write(bi, "gif", new File("c:\\yourImageName.GIF"));
          ImageIO.write(bi, "BMP", new File("c:\\yourImageName.BMP"));
        } catch (IOException ie) {
          ie.printStackTrace();
    }

Maybe you are looking for

  • Camera lock screen iPhone 5S not working properly after upgrade to iOS 8.1.3

    Actually, i upgrade my iPhone to 8.1.3. What i am trying achieve is, when my iPhone is locked i just need to access my camera without login. When i open camera when my iPhone is locked, camera displays for 2 seconds and then my screen will be black.

  • Best books for a beginner.

    I am in a Java class right now, and the material as it has been presented has been very confusing. I really want to get a good understanding of Java, my question is: What are the best books for someone that is starting to learn Java? I was looking th

  • Can someone help me with a simple duration calculation?

    I created a simple workbook for calculating hours worked. I am entering the start time, end time, and using the DUR2HOURS formula to calculate the duration. For some reason the calculation is wrong. I am sure it is somethign simple, but I could not f

  • Photos from my photo stream show up in a location that pictures were not taken.

    Hey guys, this is sorta creepy.  I took some pitures with my Iphone4S and now when I click on photo stream or camera roll and then click on places, 59 of my photos show up in Algeria.  I have never been to Algeria nor do I have any friends in Algeria

  • Music will not stay in iTunes library

    For some time now, whenever I put music into my iTunes library from my iPod, and then close iTunes and reopen, the music is then gone. This never used to be a problem. Please help me to make the music, that is saved on my computer stay in my iTunes l