Line in a JPanel

Hi,
How do you draw a line in a JPanel.?

using paintComponent(Graphics g)
public void paintComponent(Graphics g)
super.paintComponent(g);     
g.drawLine(x1,y1,x2,y2);
******************************************************

Similar Messages

  • Howto: Display text in the border line of the JPanel

    I create a JPanel and set it's border, later i add a textField at the border line of the JPanel, but when the words i type in, the line of border still at the center of the textField. Pls Help

    try this:
    import javax.swing.border.*;
    TitledBorder myBorder = new TitledBorder(" my Title ");
    myPanel.setBorder(myBorder);
    null

  • Add line as Component JPanel

    hai,
    I need to add a line as a Component to a JPanel
    and the line shd listen to mouse events like
         mouseDrag : line move etc.
    can anyone help?
    anu

    Run this sample, 2 lines that can be dragged.
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    public class Lines extends JFrame
         JPanel  pan  = new JPanel();
    public Lines()
         addWindowListener(new WindowAdapter()
        {     public void windowClosing(WindowEvent ev)
              {     dispose();
                   System.exit(0);}});
         setBounds(10,10,640,440);
         getContentPane().add(pan,BorderLayout.CENTER);
         pan.setLayout(null);
         myLine p1 = new myLine();
         p1.setBounds(20,20,400,3);
         p1.setBackground(Color.red);
         pan.add(p1);
         myLine p2 = new myLine();
         p2.setBounds(110,10,2,300);
         p2.setBackground(Color.blue);
         pan.add(p2);
         setVisible(true);
    public class myLine extends JComponent
           int mx;
         int my;
    public myLine()
         addMouseListener(new MouseAdapter()     
         {     public void mousePressed(MouseEvent m)
                   mx = m.getX();
                   my = m.getY();
         addMouseMotionListener(new MouseMotionAdapter()          
         {     public void mouseDragged(MouseEvent m)
                   int x = getX() + m.getX() - mx;
                   int y = getY() + m.getY() - my;
                   setLocation(x,y);
    public void paint(Graphics g)
         g.setColor(getBackground());
         g.fillRect(0,0,getWidth(),getHeight());
    public static void main (String[] args) 
         new Lines();
    }Noah

  • Adding simple shapes and lines to a JPanel()

    If anybody out there knows how to add simple filled circles and then lines woth arrowheads connecting the 2 circles into a JPanel then please help me out???
    I have included my code below so you have an indication of what im doing:
    import java.awt.*;
    import java.awt.Graphics;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import java.util.Vector;
    import java.io.*;
    import java.util.Collections;
    public class rightSplitPane {
    private JSplitPane splitPane;
    private JPanel p1, p2;
         private String tran, res, type;
         private Graphics gr;
    public rightSplitPane() {         
    p2 = new JPanel();
    p2.setLayout(new BorderLayout());
    //gr = p2.getGraphics();
    //Graphics2D g2d = (Graphics2D) gr;
    //gr.drawLine(30, 40, 100, 100);
         //gr.drawRect(24, 10, 60, 60);
    //p2.repaint();
    JScrollPane topPane = new JScrollPane();
    topPane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
    topPane.getBorder();
              JScrollPane bottomPane = new JScrollPane(p2);
              bottomPane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
    bottomPane.getBorder();
    //Provide minimum sizes for the two components in the split pane
    Dimension size = new Dimension(625, 350);
    topPane.setMinimumSize(size);
    bottomPane.setMinimumSize(size);
    //Create a split pane with the two scroll panes in it
    splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, topPane, bottomPane);
    splitPane.setOneTouchExpandable(true);
    splitPane.setDividerLocation(350);
    topPane.getViewport().add(trTable, BorderLayout.CENTER);
    public JSplitPane getSplitPane() {
    return splitPane;
         public static void main(String args[]) {
              rightSplitPane test = new rightSplitPane();
    //HOW DO I GET THE SPLITPANE TO BE DISPLAYED TOO??
    } // end class

    I took out few lines:
    import java.awt.*;
    import java.awt.Graphics;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import java.util.Vector;
    import java.io.*;
    import java.util.Collections;
    public class rightSplitPane
         private JSplitPane splitPane;
         private JPanel     p1, p2;
         private String     tran, res, type;
         private Graphics   gr;
    public rightSplitPane()
         JScrollPane topPane = new JScrollPane();
         topPane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
    //     topPane.getBorder();
         p2 = new JPanel();
         p2.setLayout(new BorderLayout());
         JScrollPane bottomPane = new JScrollPane(p2);
         bottomPane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
    //     bottomPane.getBorder();
    //Provide minimum sizes for the two components in the split pane
         Dimension size = new Dimension(625, 350);
    //     topPane.setMinimumSize(size);
    //     bottomPane.setMinimumSize(size);
    //Create a split pane with the two scroll panes in it
         splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, topPane, bottomPane);
         splitPane.setOneTouchExpandable(true);
         splitPane.setDividerLocation(350);
    //     topPane.getViewport().add(trTable, BorderLayout.CENTER);
    public JSplitPane getSplitPane()
         return splitPane;
    public static void main(String args[])
         rightSplitPane test = new rightSplitPane();
         JFrame frame = new JFrame();
         frame.setSize(700,500);
         frame.setContentPane(test.getSplitPane());
         frame.setVisible(true);
    /code]
    Noah                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • Disappearing lines in a JPanel

    I have some buttons in a panel and the panel is placed in another panel which contains some lines and these two panels are added to a scrollpane. The problem is that, if I scroll the frame or place another window on the frame the lines disappear. I have not used getGraphics() method. All the painting has been done in the paintComponent().What might be the reason.

    What might be the reason. You are doing something wrong.
    If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.
    And don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags so the code retains its original formatting.

  • How do I draw on a Jpanel

    I've spent around 15 hours trying to work out how to draw something as simple as a line onto a JPanel!
    I kind of understand about using paint, set color etc. if I was writing code in notepad I'm sure that it wouldn't be a problem but as I'm using the JDK SE form editor I'm very confused as to what should be done.
    Basically i have a frame with 2 panels in gridLayout position and filled top to bottom left to right equally spaced.
    I've added a button to the left panel. When this button is clicked I want to draw a line on the right panel. It's that simple! If I can do that then I can move on.
    All the examples i've read don't show what to do when using the JDK SE form editor. Please can someone tell me exactly what to do...thanks

    see if you can follow this
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    class Testing extends JFrame
      public Testing()
        setSize(400,300);
        setLocation(200,100);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        getContentPane().add(new DrawPanel());
        pack();
      public static void main(String[] args){new Testing().setVisible(true);}
    class DrawPanel extends JPanel
      java.util.List points = new java.util.ArrayList();
      public DrawPanel()
        setPreferredSize(new Dimension(400,300));
        setBackground(Color.WHITE);
        addMouseListener(new MouseAdapter(){
          public void mousePressed(MouseEvent me){
            points = new java.util.ArrayList();
            points.add(me.getPoint());}});
        addMouseMotionListener(new MouseMotionListener(){
          public void mouseMoved(MouseEvent me){}
          public void mouseDragged(MouseEvent me){
            points.add(me.getPoint());
            DrawPanel.this.repaint();}});
      public void paintComponent(Graphics g)
        super.paintComponent(g);
        if(points.size() > 0)
          Point sPt = (Point)points.get(0);
          for(int x = 1, y = points.size(); x < y; x++)
            Point ePt = (Point)points.get(x);
            g.drawLine(sPt.x,sPt.y,ePt.x,ePt.y);
            sPt = ePt;
    }if you want a bit of advice - dump the gui builder

  • Weird JPanel Resizing Problem

    This program works as I want it to with one exception: sometimes when resizing the frame, the contained JPanels do not fully cover the frame's BorderLayout center region. If you run the program you'll discover that a light gray area often appears on the right and bottom edges of the frame when it's resized. Any ideas? Thanks. I'm using version 1.3.1._01 on Windows 98.
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class Grid extends JPanel implements ComponentListener
    private int rows, columns;
    private Block[][] block;
    public Grid()
    rows = 20;
    columns = 10;
    block = new Block[rows][columns];
    setLayout(new GridLayout(rows,columns,0,0));
    for (int i = 0; i < rows; i++)
    for (int j = 0; j < columns; j++)
    block[j] = new Block();
    if ((i % 4) >= 1)
    block[j].turnOn();
    else
    block[j].turnOff();
    add(block[j]);
    }//end inner for loop
    }//end outer for loop
    setBorder(BorderFactory.createLineBorder(Color.black,4));
    addComponentListener(this);
    }//end default constructor
    public Dimension getPreferredSize()
    int horizontalInsets = getInsets().left + getInsets().right;
    int verticalInsets = getInsets().top + getInsets().bottom;
    return new Dimension((block[0][0].getBlockWidth() * columns) + horizontalInsets,
    (block[0][0].getBlockHeight() * rows) + verticalInsets);
    }//end getPreferredSize
    public void componentHidden(ComponentEvent ce)
    //Invoked when the component has been made invisible.
    public void componentMoved(ComponentEvent ce)
    //Invoked when the component's position changes.
    public void componentResized(ComponentEvent ce)
    //Invoked when the component's size changes.
    int horizontalInsets = getInsets().left + getInsets().right;
    int verticalInsets = getInsets().top + getInsets().bottom;
    setPreferredSize(new Dimension(getWidth() + horizontalInsets,getHeight() + verticalInsets));
    setMinimumSize(new Dimension(getWidth() + horizontalInsets,getHeight() + verticalInsets));
    public void componentShown(ComponentEvent ce)
    //Invoked when the component has been made visible.
    public static void main(String[] args)
    JFrame frame = new JFrame();
    frame.getContentPane().add(new Grid(),BorderLayout.CENTER);
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.show();
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class Block extends JPanel implements ComponentListener
    private int blockWidth, blockHeight;
    private Color blockBackgroundColor, blockBorderColor, onColor;
    private String blockOwner;
    private boolean isOn;
    public Block()
    blockWidth = 20;
    blockHeight = 20;
    //setSize(20,20);
    //setPreferredSize(new Dimension(20,20));
    blockBackgroundColor = Color.gray;
    blockBorderColor = Color.black;
    setLayout(new BorderLayout(0,0));
    setBorder(BorderFactory.createLineBorder(blockBorderColor,1));
    setBackground(blockBackgroundColor);
    isOn = false;
    blockOwner = "1";
    onColor = Color.orange;
    addComponentListener(this);
    setVisible(true);
    }//end default constructor
    public Block(int blockWidth, int blockHeight)
    this();
    this.blockWidth = blockWidth;
    this.blockHeight = blockHeight;
    }//end two-arg constructor
    public void setBlockWidth(int blockWidth)
    this.blockWidth = blockWidth;
    public int getBlockWidth()
    return blockWidth;
    public void setBlockHeight(int blockHeight)
    this.blockHeight = blockHeight;
    public int getBlockHeight()
    return blockHeight;
    public void setBlockBorderColor(Color blockBorderColor)
    this.blockBorderColor = blockBorderColor;
    public Color getBlockBorderColor()
    return blockBorderColor;
    public void setBlockBackgroundColor(Color blockBackgroundColor)
    this.blockBackgroundColor = blockBackgroundColor;
    public Color getBlockBackgroundColor()
    return blockBackgroundColor;
    public void setOnColor(Color onColor)
    this.onColor = onColor;
    public Color getOnColor()
    return onColor;
    public void turnOn()
    setBackground(onColor);
    isOn = true;
    public void turnOff()
    setBackground(blockBackgroundColor);
    isOn = false;
    public void setBlockOwner(String blockOwner)
    this.blockOwner = blockOwner;
    public String getBlockOwner()
    return blockOwner;
    public Dimension getPreferredSize()
    int horizontalInsets = getInsets().left + getInsets().right;
    int verticalInsets = getInsets().top + getInsets().bottom;
    return new Dimension(blockWidth + horizontalInsets,
    blockHeight + verticalInsets);
    }//end getPreferredSize
    public void paintComponent(Graphics g)
    super.paintComponent(g);
    if (isOn)
    FontMetrics fm = g.getFontMetrics();
    int blockOwnerWidth = fm.stringWidth(blockOwner);
    int blockOwnerAscent = fm.getAscent();
    int x = (blockWidth / 2) - (blockOwnerWidth / 2);
    int y = (blockHeight / 2) + (blockOwnerAscent / 2);
    g.drawString(blockOwner,x,y);
    }//end paintComponent
    public void componentHidden(ComponentEvent ce)
    //Invoked when the component has been made invisible.
    public void componentMoved(ComponentEvent ce)
    //Invoked when the component's position changes.
    public void componentResized(ComponentEvent ce)
    //Invoked when the component's size changes.
    int horizontalInsets = getInsets().left + getInsets().right;
    int verticalInsets = getInsets().top + getInsets().bottom;
    blockWidth = this.getWidth();
    blockHeight = this.getHeight();
    setPreferredSize(new Dimension(blockWidth + horizontalInsets,blockHeight + verticalInsets));
    setMinimumSize(new Dimension(blockWidth + horizontalInsets,blockHeight + verticalInsets));
    public void componentShown(ComponentEvent ce)
    //Invoked when the component has been made visible.

    Your problem is due to the use of a GridLayout.
    With a GridLayout, all components must have the same dimension. And the extra space has to be divisible by the number of components to be distributed to each component :
    For example : In a component with a GridLayout, you have a line with 10 JPanels. The width of the component is initially 100. In this case, each JPanel will have a width of 10 (10*10=100).
    If you resize the main component to a width of 110, each JPanel will have a size of 11 (11*10=110).
    But in the case where the width is not divisible by the number of JPanels, there will be an extra-space :
    if the width of the component is 109, the width of each JPanel will be 10 and there will be an extra-space of 9 (10*10+9 = 109).
    I hope this helps,
    Denis

  • Detecting mouse click on a line

    I'm learning making GUI apps in Java.
    Here is my problem. Suppose I draw a line on a JPanel. If I want to find out if the line is clicked or not, what is the best way to do it?
    I did some research, looked at API. If I suppose my line is Line2D, then since its a line it doesn't have an area, so its contains method always returns false (..from API). So i dig around some more and came up with the solution of getting the shape from the stroke and calling its contains method.
    This is a code for what i mean..
    private Line2D.Double testLine;
    //mouse clicked event
    public void mouseClicked(MouseEvent e) {
            int x = e.getX();
            int y = e.getY();
            BasicStroke stroke = new BasicStroke(4);
            Shape testShape = stroke.createStrokedShape(testLine);
            if(testShape.contains(x,y)){
                System.out.println("this will be printed if clicked on line");
        }Well, the above solution works fine.
    Is it the right way of doing it or is there a better way around for this?
    Any help will be appreciated.
    Thanks.

    When trying to test if a line was pressed I usually test if the distance between the mouse coordinates and the line is smaller than some constant (usually 5 pixels).
    For this you have to write your own code to calculate the distance between a point and a line.

  • Draw a line over a JLabel

    Hello,
    Does anyone know if there is a way to draw a line over a JLabel. The scenario is that I have a Jpanel that uses a Gridbaglayout and has an array of JLabels(to display images). I have the need to connect labels and to have the line draw over any labels that it intersects.
    I know this can be done if you use the same graphics context to draw the image and the line, but since I need to use JLabels and the graphics context that draws the line is a JPanel's, I have a problem.
    Is there a way to draw a line over a JLabel?
    Thanks
    lon

    Easiest thing to do would be to override paintChildren like
    public void paintChildren(Graphics g) {
      super.paintChildren(g);
      //paint your line here
    }That should do what you want - paint the children first and then draw over them

  • JPanel doesn't paint graphics

    Hi ! I've been writing a program that should graph a linear function, the problem is that I'm trying to draw a line in a JPanel but this doesn't work XD...The program has 2 windows. The first one is the windows from where i get data for the function, after having the data I press a button that I want to open a new window and draw the function...
    This is the part of the code in the button on the first window that certainly opens a new window
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
       JFrame frm_grafica = new JFrame("Grafica - B-Rabbit");
       frm_grafica.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       Grafica graph = new Grafica();   
       frm_grafica.getContentPane().add(graph);
       frm_grafica.setSize(250,250);
       frm_grafica.setLocation(this.getLocation().x+this.getWidth()+5, this.getLocation().y);
       frm_grafica.setVisible(true);
    }but in the new window [that is located beside the first window] this is the code, that doesn't work...yes maybe there's something i don't do or something that i am passing over..
    public class Grafica extends JPanel{
        Grafica(){
            this.setBackground(Color.WHITE);
        protected void PaintComponent(Graphics g){
            super.paint(g);
            g.setColor(Color.BLACK);
            g.drawLine(50,50, 20, 20);
    }So Hope someone helps me...

    //protected void PaintComponent(Graphics g){
    protected void paintComponent(Graphics g){//<-- it's 2.00am, small p
            //super.paint(g);
            super.paintComponent(g);

  • Scroll does not work in JPanel 's JScrollPane

    Hello,
    I have a JPanel and I am adding some components to this panel. The problem is that the Panel goes beyond the visible area of the monitor so some of the components cannot be seen. Therefore, I add a JScrollPane onto the panel.
    Some code:
    JScrollPane scroll_panel_inputs = new JScrollPane(panel_inputs);
    getContentPane().add(scroll_panel_inputs);       
    scroll_panel_inputs.validate();where "panel_inputs" is a JPanel.
    The problem is that despite adding too many components in the JPanel to be visible, I cannot scroll down , and the scroll bar doesn't change its length.

    A possible guess:
    1) If you don't specify a layout for the JPanel, it defaults to the FlowLayout.
    2) If you don't specify a preferred size of the JPanel (via setPreferredSize), it will extend itself to fit all of your components.
    So my guess is that your jpanel is extending way to the right to fit all the components one right after the other all on the same line, and that none of the components are being placed on the next line of the jpanel.
    If this is correct, a solution is to either set a preferred size of the panel so that the components will wrap to the next line when they run up against the right wall of the jpanel, or change the Layout manager for your jpanel. Even better would be to use several nested JPanels each with a different layout.
    For instance:
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.GridLayout;
    import javax.swing.BorderFactory;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    public class StuffedPanels extends JPanel
        public StuffedPanels()
            setLayout(new GridLayout(0, 1, 5, 5));
            setPreferredSize(new Dimension(1000, 800));
            JPanel panel1 = createStuffedPanel1();
            JPanel panel2 = createStuffedPanel1();
            // set only panel2's preferred size
            panel2.setPreferredSize(new Dimension(2000, 4000));
            JScrollPane stuffedScrollPane1 = new JScrollPane(panel1);
            JScrollPane stuffedScrollPane2 = new JScrollPane(panel2);
            stuffedScrollPane1.setBorder(BorderFactory.
                createTitledBorder("without setting preferred size"));
            stuffedScrollPane2.setBorder(BorderFactory.
                createTitledBorder("with setting preferred size"));
            add(stuffedScrollPane1);
            add(stuffedScrollPane2);
         * create a jpanel and stuff it with 1000 jlabels
         * @return the jpanel
        private JPanel createStuffedPanel1()
            JPanel stuffedPane = new JPanel();
            for (int i = 0; i < 1000; i++)
                JLabel label = new JLabel(" Hello Genius " + String.format("%4d", i));
                label.setFont(new Font(Font.MONOSPACED, Font.BOLD, 18));
                stuffedPane.add(label);
            return stuffedPane;
        private static void createAndShowGUI()
            JFrame frame = new JFrame("StuffedPanel Application");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add(new StuffedPanels());
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        public static void main(String[] args)
            javax.swing.SwingUtilities.invokeLater(new Runnable()
                public void run()
                    createAndShowGUI();
    }

  • How to make L-shaped lines?

    Hi all,
    as I'm rather new to Java I'm facing the follwing problem:
    I've managed somehow to draw some lines on a JPanel. Now I'd like to click on any line and after the click a little point should appear on the chosen line. Then, dragging that point, the straight line should become an L-shaped line.
    Again, as I'm new to Java I really don't know how to perform this task.
    Any pieces of code, suggestions or whatever are highly appreciated.
    Thanks in advance,
    Hannes

    Hi Hannes,
    I am still not sure about what you want but this should help you with the techniques you might need. Please look at this applet and let me know.
    Duke dollars are highly appreciated... :)
    Berk Can Celebisoy
    public class ForumTest extends JApplet {
      public void init() {   
        getContentPane().add(new CustomPanel());
    class CustomPanel extends JPanel {
      int x1, y1, x2, y2, x3, y3;
      Graphics g_;
      public CustomPanel() {
        x1 = 10;   y1 = 10;
        x2 = 10;   y2 = 90;   
        addMouseMotionListener(new MouseMotionAdapter() {
          public void mouseDragged(MouseEvent e) {       
            x3 = e.getX();
            y3 = e.getY();                       
        addMouseListener(new MouseAdapter() {
          public void mouseReleased(MouseEvent e) {        
            g_ = getGraphics();
            //draw the conecting lines
            g_.drawLine(x1, y1, x3, y3);
            g_.drawLine(x2, y2, x3, y3);
            //draw the rectangle
            g_.drawRect(x3-2, y3-2, 4, 4);                     
          public void mousePressed(MouseEvent e) {       
            repaint();        
      public void paintComponent(Graphics g) {
        //draw the initial line.   
        g.drawLine(x1, y1, x2, y2);    
        //draw the rectangles
        g.drawRect(x1-2, y1-2, 4, 4);
        g.drawRect(x2-2, y2-2, 4, 4);
    }

  • Drawing a straight line

    Hello,
    I haven't been able to draw a straight line in a JPanel as a separator. I mean, I have several other componentes in the JPanel and I would like to use straight lines as separators between some of the componentes. Besides drawing the line, how could I set its length?
    Thank you,
    Alfredo

    You could use a JPanel and set its border to a LineBorder (or whatever border you desire. Then set the preferred size of the panel and all that gets painted is the border.

  • Draw lines

    Hi,
    How can I draw a line in a JPanel ???

    Check out this thread: http://forum.java.sun.com/thread.jsp?forum=31&thread=243028
    It includes a simple program that draws a face on a JFrame. The idea would be the same, you just override the paint() method of your JPanel instead of the JFrame.
    The Swing tutorial also has a section on Painting:
    http://java.sun.com/docs/books/tutorial/uiswing/painting/index.html

  • Can't use BoxLayout more than once in a class?

    I have a JPanel inside of a JPanel. I am using a BoxLayout
    manager on the main JPanel, and I used to use a GridLayout on the
    sub JPanel. Yesterday I changed the Layout Manager of the subPanel
    to BoxLayout, and now I get this strange error at runtime. Why can't I use
    2 Box Layouts? Why is Java trying to share them?
    Exception in thread "main" java.awt.AWTError: BoxLayout can't be shared
            at javax.swing.BoxLayout.checkContainer(BoxLayout.java:408)
            at javax.swing.BoxLayout.invalidateLayout(BoxLayout.java:195)
            at java.awt.Container.invalidate(Container.java:851)
            at java.awt.Component.addNotify(Component.java:5398)
            at java.awt.Container.addNotify(Container.java:1852)
            at javax.swing.JComponent.addNotify(JComponent.java:4270)
            at java.awt.Container.addNotify(Container.java:1859)
            at javax.swing.JComponent.addNotify(JComponent.java:4270)
            at java.awt.Container.addNotify(Container.java:1859)
            at javax.swing.JComponent.addNotify(JComponent.java:4270)
            at java.awt.Container.addNotify(Container.java:1859)
            at javax.swing.JComponent.addNotify(JComponent.java:4270)
            at java.awt.Container.addNotify(Container.java:1859)
            at javax.swing.JComponent.addNotify(JComponent.java:4270)
            at java.awt.Container.addNotify(Container.java:1859)
            at javax.swing.JComponent.addNotify(JComponent.java:4270)
            at java.awt.Container.addNotify(Container.java:1859)
            at javax.swing.JComponent.addNotify(JComponent.java:4270)
            at javax.swing.JRootPane.addNotify(JRootPane.java:658)
            at java.awt.Container.addNotify(Container.java:1859)
            at java.awt.Window.addNotify(Window.java:395)
            at java.awt.Frame.addNotify(Frame.java:479)
            at java.awt.Window.pack(Window.java:413)
            at CDBTest.main(CDBTest.java:246)I need help.
    Thanks
    Josh

    hmm, the following's my code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    public class PreferencesPane extends JPanel {
    //Main pane to be added to the class
    private JTabbedPane mainPane;
    //the options pane
    private JPanel optionsPane;
    //the accounts list
    private JPanel accountsPane;
    //the personal information display
    private JPanel perInfoPane1;
    private JPanel perInfoPane2;
    //labels
    private JLabel usernameLabel;
    private JLabel newPassLabel;
    private JLabel confPassLabel;
    private JPanel separatorLabel;
    private JLabel nameLabel;
    private JLabel emailLabel;
    private JLabel titleLabel;
    private JLabel companyLabel;
    private JLabel homePhoneLabel;
    private JLabel workPhoneLabel;
    private JLabel cellPhoneLabel;
    private JLabel faxLabel;
    private JLabel homeAddLabel;
    private JLabel workAddLabel;
    private JLabel notesLabel;
    //Textfields
    private JTextField usernameField;
    private JPasswordField newPassField;
    private JPasswordField confPassField;
    private JPanel separatorField;
    private JTextField nameField;
    private JTextField emailField;
    private JTextField titleField;
    private JTextField companyField;
    private JTextField homePhoneField;
    private JTextField workPhoneField;
    private JTextField cellPhoneField;
    private JTextField faxField;
    private JTextArea homeAddField;
    private JTextArea workAddField;
    private JTextArea notesField;
    //Save information button
    private JButton saveInfoButton;
    private JPanel perInfoPane;
    public PreferencesPane() {
    optionsPane = new JPanel();
    accountsPane = new JPanel();
    //labels
    usernameLabel = new JLabel("Username:");
    newPassLabel = new JLabel("New Password:");
    confPassLabel = new JLabel("Confirm New Password:");
    separatorLabel = new JPanel();
    nameLabel = new JLabel("Name:");
    emailLabel = new JLabel("Email:");
    titleLabel = new JLabel("Title:");
    companyLabel = new JLabel("Company:");
    homePhoneLabel = new JLabel("Home Phone:");
    workPhoneLabel = new JLabel("Work Phone:");
    cellPhoneLabel = new JLabel("Cell Phone:");
    faxLabel = new JLabel("Fax:");
    homeAddLabel = new JLabel("Home Address:");
    workAddLabel = new JLabel("Work Address:");
    notesLabel = new JLabel("Notes:");
    //Textfields
    usernameField = new JTextField(20);
    newPassField = new JPasswordField(20);
    confPassField = new JPasswordField(20);
    separatorField = new JPanel();
    nameField = new JTextField(20);
    emailField = new JTextField(20);
    titleField = new JTextField(20);
    companyField = new JTextField(20);
    homePhoneField = new JTextField(20);
    workPhoneField = new JTextField(20);
    cellPhoneField = new JTextField(20);
    faxField = new JTextField(20);
    homeAddField = new JTextArea(10,20);
    workAddField = new JTextArea(10,20);
    notesField = new JTextArea(10,20);
    //Panel to contain the textfield and labels
    perInfoPane1 = new JPanel(new GridLayout(12,2));
    perInfoPane1.add(usernameLabel);
    perInfoPane1.add(usernameField);
    perInfoPane1.add(newPassLabel);
    perInfoPane1.add(newPassField);
    perInfoPane1.add(confPassLabel);
    perInfoPane1.add(confPassField);
    perInfoPane1.add(separatorLabel);
    perInfoPane1.add(separatorField);
    perInfoPane1.add(nameLabel);
    perInfoPane1.add(nameField);
    perInfoPane1.add(emailLabel);
    perInfoPane1.add(emailField);
    perInfoPane1.add(titleLabel);
    perInfoPane1.add(titleField);
    perInfoPane1.add(companyLabel);
    perInfoPane1.add(companyField);
    perInfoPane1.add(homePhoneLabel);
    perInfoPane1.add(homePhoneField);
    perInfoPane1.add(workPhoneLabel);
    perInfoPane1.add(workPhoneField);
    perInfoPane1.add(cellPhoneLabel);
    perInfoPane1.add(cellPhoneField);
    perInfoPane1.add(faxLabel);
    perInfoPane1.add(faxField);
    perInfoPane2 = new JPanel(new GridLayout(3,2));
    perInfoPane2.add(homeAddLabel);
    perInfoPane2.add(homeAddField);
    perInfoPane2.add(workAddLabel);
    perInfoPane2.add(workAddField);
    perInfoPane2.add(notesLabel);
    perInfoPane2.add(notesField);
    //save button
    saveInfoButton = new JButton("Save information");
    perInfoPane = new JPanel(new BoxLayout(perInfoPane,BoxLayout.Y_AXIS));
    perInfoPane1.setAlignmentX(Component.LEFT_ALIGNMENT);
    perInfoPane2.setAlignmentX(Component.LEFT_ALIGNMENT);
    saveInfoButton.setAlignmentX(Component.LEFT_ALIGNMENT);
    perInfoPane.add(perInfoPane1);
    perInfoPane.add(perInfoPane2);
    perInfoPane.add(saveInfoButton);
    //adds components to the TabbedPane
    mainPane = new JTabbedPane(JTabbedPane.LEFT);
    mainPane.add("Options",optionsPane);
    mainPane.add("Accounts",accountsPane);
    mainPane.add("Personal Information",perInfoPane);
    setLayout(new BorderLayout());
    add(mainPane,BorderLayout.CENTER);
    As you can see, the only time I use BoxLayout is the following line:
    perInfoPane = new JPanel(new BoxLayout(perInfoPane,BoxLayout.Y_AXIS));
    As far as I can tell, that's correct, but I'm still getting this at runtime:
    Exception in thread "main" java.awt.AWTError: BoxLayout can't be shared
    Anyone have any ideas?

Maybe you are looking for

  • Multiple Electronic Signatures using Signature PAD

    Hello Experts, We have a requirement where we need to capture the electronic signature on thee PDF form at different time. This means I fill the PDF form get the 1st electronic signature using signature PAD and then saved the form. I open the same fo

  • DIFF: Field string ,Structure and Internal table declaration

    Hai,        what is the diference between  Field string ,Structure in ABAP program and Internal table declaration and how it will work ? Thank you ASHOK KUMAR.

  • G4-dual + G4-dual connection

    I have a G4-dual processor and now have another one available. Can I? if so, How can I connect the 2 G4 computers together to get the max processing output from the 2 G4-Dual? Both have latest Tiger OS. I remembering using the firewire connection. Th

  • LR5 Cannot find any Channel Pop up menu in Tone Curve?

    Am reading Kelby's LR 5 book page 162 +  Cannot see any place where I can click to see the individual channels adjustment pop up to select chanel. He even has it circled in the illustration but I don't have anything like that in the Tone Curve panel

  • Web form spam

    Hi I am a novice CF dude. I have a web registration form that gets bogus registrations and I am trying get my users to type in a word to help limit spam. I am not sure how to do this and google and the forums do not show any cf examples. I have tried