Add a panel on another panel

I am working on a program and need to put one panel into another panel . I have been successful at adding the panel
however when i do this i cannot set the size of the 2nd panel. The first panel on the frame is just fine
i have tried adding the panel to the first panels constructor this works the panel shows up but i am unable
to set the size of the panel which is the problem.
I tried calling this.setSize in the second panels constructor to solve this problem no luck there
i have also tried adding the 2nd panel to the frame but this produces the same problem as stated above
im really stumped any help woul be appreciated
here is some code i have
This is the frames class with the first panel added
public ICUFrame () {
        super("ICU");
        canvas= new ICUPanel();
        add(canvas, BorderLayout.CENTER);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(1000,1000);
        setVisible(true);
    } this is the panel on the frame
only the constructor is required here as the rest is just code for drawing etc...
public class ICUPanel extends JPanel {
    DiaDisplay d ;
    SysDisplay s;
    public ICUPanel(){
        super();
        setBackground(Color.BLACK);
        ICUMonitoring monitor = new ICUMonitoring();
        Thread rep = new Thread(new Repainter());
        d = new DiaDisplay(220,20,40,200);
        s = new SysDisplay(420,220,40,200);
        rep.start();
    }// end of constructor
}this is the 2nd panel i am trying to add to the 1st panel
public class ECGPanel extends JPanel {
    ecgDisplay e;
     public ECGPanel(){
     super();
     setBackground(Color.white);
     e = new ecgDisplay();
    }// end of constructor
}

xiaolixx wrote:
I am working on a program and need to put one panel into another panel . I have been successful at adding the panel
however when i do this i cannot set the size of the 2nd panel. The first panel on the frame is just fine
i have tried.. What happened to your shift key? Please apply it once at the start of every sentence, rather than those rare occasions you 'feel like it'. Adding an upper case letter to each sentence makes it easier for people to quickly scan the text, looking for ways to help. You would not want to make it harder for someone to help, would you?
..adding the panel to the first panels constructor this works the panel shows up but i am unable
to set the size of the panel which is the problem.
I tried calling this.setSize in the second panels constructor to solve this problem no luck there
i have also tried adding the 2nd panel to the frame but this produces the same problem as stated above
im really stumped any help woul be appreciated
here is some code i haveAlso known as 'uncompilable code snippets'. For better help sooner, post an SSCCE . But first, go through the tutorial to which Darryl linked.

Similar Messages

  • How to merge the Panel with another Panel to One Panel?

    As Title!!

    i will give it a shot. by no means do i say it is correct being a beginner myself
    Panel aPanel = new Panel();
    Panel anotherPanel = new Panel();
    Panel mythirdPanel = new Panel();
    aPanel.add(anotherPanel);
    aPanel.add(myThirdPanel);
    this should create one panel called aPanel with two panels inside - another Panel first, followed by mythirdPanel second. This should be in FlowLayout pattern as that is default unless specified.
    That is my guess and my shot.
    8)

  • Center a panel in another panel

    Good day:
    I am trying to center align (horizontal & vertical centering) a panel in another panel.
    How would I go about doing this?
    I have tried a few different layout managers, but they have not produced the results I am looking for.
    The parent panel is fixed sized, but the child panel is dynamic.
    Please help!!

    Thank you for your post, I greatly appreciate it. However, I tried both of those methods, and my child JPanel is always being placed in the upper left corner of the parent JPanel.
    Here is some code showing my initialization of the JPanel, and the adding of the child JPanel:
    Creating the parent JPanel:
            imagePreview = new JPanel();
            imagePreview.setBackground(Color.white);
            imagePreview.setBorder(new LineBorder(Color.black,1,true));
            getContentPane().add(imagePreview);Adding the child JPanel
    void displayThumbnail()
                  wallpaperChanger.imagePreview.removeAll();
                  wallpaperChanger.imagePreview.add(new viewer(THUMB_LOCATION));
                  wallpaperChanger.imagePreview.repaint();
             }If it helps, the viewer class:
    class viewer extends JPanel
         private Image image;
         public viewer(String fileName)
              Toolkit toolkit = Toolkit.getDefaultToolkit();
              image = toolkit.createImage(fileName);
              MediaTracker mediaTracker = new MediaTracker(this);
              mediaTracker.addImage(image, 0);
              try
                   mediaTracker.waitForID(0);
              catch (InterruptedException ie)
                   System.err.println(ie);
              setSize(image.getWidth(null), image.getHeight(null));
              //show();
         public void paint(Graphics graphics)
              graphics.drawImage(image, 0, 0, null);
    }

  • Check a checkbox in a jtable in a panel from another panel

    I have a panel-> Panel A
    where I have some conditions and on that condition I want to check a checkbox in a jtable in another panel ->PanelB
    So Panel B has a jtable with one column as checkbox,I want to check/uncheck this checkbox based on some conditions in some other panel A
    I have no idea how to do this.Please help.
    Thanks

    885522 wrote:
    How will the change be reflected dynamically if the value changes in some other panel.By code that you write. If you want component X to change based on some changes to component Y, then first you set up a listener which will notify you when changes to component Y take place. In that listener you put code which makes the appropriate changes to component X, based on what happened to component Y.

  • Moving canvas3d from one panel to another panel is causing problems

    Hi,
    I'm new to java3d and at present struggling to find a solution for the following problem:
    In my application there are 4 canvas3ds and there are no relation between them except they all share the same screen3d. added in a Jpanel using grid layout(2,2). all have different universe. when user double click s any one of them the clicked canvas is supposed to get maximized and again doing the same restores everything before double click.
    What i'm doing is using card layout adding two panels one for normal view(4 at a time) and one for maximized view. The first one uses grid and the other one uses border.
    In case of maiximizing i just remove the one that's clicked from the former jpanel and add it to the other panel.But some times it garbles the screen in a way such that on rotating any node or element moves other nodes that are not supposed to be.
    Any suggestions???

    well you should have posted in java3d forum, but at this point i'd say post code so we can see what you mean by "garbles"

  • Switching a panel to another panel in the same frame.Is there a better way?

        public void actionPerformed(ActionEvent e) {
            if (e.getSource() instanceof toolButton){
                westPanel.remove(optionPanel);
                westPanel.setVisible(false);
                toolButton tb = (toolButton)e.getSource();
                optionPanel = tb.getPanel();
                westPanel.add(optionPanel);
                westPanel.setVisible(true);
        }This code above is what I used to execute when one of several JButtons is being pressed.
    In this program, one of the JPanel will be automatically switched to another JPanel when you press the respective JButtons.
    Here are my questions:
    1. Right now, I use westPanel.setVisible(false) and then change some stuff and then invoke westPanel.setVisible(true) to make it visible again. Although this works, I have a feeling that this is not quite right. I feel that there should be some better way to do this, switching the panel and request the program to redraw the replacing JPanel. Is there a better way for this?
    2. Most of the time, the JPanel changes the size according to the components on it. I have tried several LayoutManager, but it seems that those components have more priority. Is there a way to completely fix the JPanel so that they stay the same size?

    Look into using a Card Layout rather than manually swapping the panels: http://java.sun.com/docs/books/tutorial/uiswing/layout/card.html

  • How to call repaint() from one panel for another panel

    I have two classes (leftPanel and rightPanel), which extends JPanel. I have added these panels to the container of the JFrame. I have a JButton in my left leftPanel. When I click the button I want that the repaint method of rightPanel is called. Please tell me how to do that.

    Ok, the reason it wasn't working is because the two classes don't have a reference to each other. They don't have anything in common at all. The only class hooking them together is the myFrame. Therefore, myFrame is the class that should be the communication link. I've added a few lines of code to myFrame, but didn't change the other classes. Now myFrame has an actionListener and it listens for the leftPanel.leftButton. When the leftButton fires, myFrame tells rightPanel to repaint. Another cool way would be to have an actionListener on rightPanel and register that with leftPanel when you instantiate them in myFrame.
    GL
    kimoS
    import javax.swing.*;
    import java.awt.*;
    import java.awt.geom.*;
    import java.awt.event.*;
    import java.util.*;
    public class myFrame extends JFrame implements ActionListener{
    leftPanel left;
    rightPanel right;
    static int st=0;
    myFrame(){
    left=new leftPanel();
    left.leftButton.addActionListener(this);
    right=new rightPanel();
    this.getContentPane().add(left,BorderLayout.WEST);
    this.getContentPane().add(right,BorderLayout.CENTER);
    public static void main(String args[]){
    myFrame mf=new myFrame();
    mf.setSize(500,500);
    mf.setVisible(true);
    public void actionPerformed(ActionEvent e){
    right.repaint();
    class leftPanel extends JPanel {
    JButton leftButton;
    leftPanel(){
    leftButton=new JButton("leftButton");
    this.add(leftButton);
    leftButton.addActionListener(new leftButtonListener());
    class leftButtonListener implements ActionListener{
    public void actionPerformed(ActionEvent e){
    myFrame.st=myFrame.st+1;
    System.out.println("st value inside listener="+myFrame.st);
    // ??? here I want to call repaint method of rightPanel
    class rightPanel extends JPanel{
    int flag=-1;
    public void paintComponent(Graphics g) {
    update(g);
    public void update(Graphics g){
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D)g;
    g2.drawString("If " myFrame.st" increases then repaint() called on click of button",30,100);
    flag=flag*(-1);
    if(flag==1){
    g2.drawString(" painting is working",100,200);
    }

  • Panel in another panel

    i have a Jpanel form class called panel2, a frame which contains a panel called mainPanel.
    i want to display panel2 in mainPanel on button click..
    panel2 contains a textbox.
    i have tried doing
    panel3 pn=new panel3();
            mainPanel.add(pn);
            mainPanel.revalidate();
            mainPanel.repaint();but it is not working

    i dont know how to create a SSCCE.
    i am giving you the complete code which contains 3 classes-
    main - to start the application
    form1 - the form which contains mainpanel and button event to add panel3 object in mainPanel
    panel3 - this extends JPanel class and contains a textBox
    (i hope this will help)
    MAIN CLASS
    package javaapplication2;
    public class Main {
        public static void main(String[] args) {
            // TODO code application logic here
            *form1 fm=new form1();*
            *fm.setVisible(true);*
    }Form1 - this class contains the mainPanel
    package javaapplication2;
    public class form1 extends javax.swing.JFrame {
        /** Creates new form form1 */
        public form1() {
            initComponents();
        private void initComponents() {
            mainPanel = new javax.swing.JPanel();
            jButton1 = new javax.swing.JButton();
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            mainPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
            javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
            mainPanel.setLayout(mainPanelLayout);
            mainPanelLayout.setHorizontalGroup(
                mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 378, Short.MAX_VALUE)
            mainPanelLayout.setVerticalGroup(
                mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 217, Short.MAX_VALUE)
            jButton1.setText("Add Panel");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(mainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton1))
                    .addContainerGap())
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap(36, Short.MAX_VALUE)
                    .addComponent(jButton1)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(mainPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap())
            pack();
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
            *panel3 pn=new panel3();*
            *mainPanel.add(pn);*
            *mainPanel.revalidate();*
            *mainPanel.repaint();*
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new form1().setVisible(true);
        private javax.swing.JButton jButton1;
        private javax.swing.JPanel mainPanel;
    }Panel3 - this is to be added in the mainPanel
    package javaapplication2;
    public class panel3 extends javax.swing.JPanel {
        public panel3() {
            initComponents();
        private void initComponents() {
            jTextField1 = new javax.swing.JTextField();
            jTextField1.setText("This should be displayed");
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
            this.setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                    .addContainerGap())
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(184, Short.MAX_VALUE))
        private javax.swing.JTextField jTextField1;
    }

  • How do I drag an image icon or image from one panel to another panel?

    Please help.
    I know to need how to drag an image icon from one panel to the other. For example, the first panel would shows the image files that is inside my folder. How can i code it so that I can drag the image that appear on the first panel into the second panel which will hold the images that I want to embed my barcode inside?
    The thumbnail size of the image and showing all the image files in my folder was done already, I only need to know how can I make the image icon to be able to drag from one panel to the other.
    Thanks.

    I found this code in some websites:
    public class ImageSelection extends TransferHandler {
         private static final DataFlavor flavors[] = {DataFlavor.imageFlavor};
         public int getSourceActions(JComponent c) {
              return TransferHandler.COPY;
         public boolean canImport(JComponent comp, DataFlavor flavor[]){
              if (!(comp instanceof JLabel)){
                   return false;
              for (int i=0, n=flavor.length; i<n; i++){
                   for (int j=0, m=flavors.length; j<m; j++){
                        if (flavor.equals(flavors[j])){
                             return true;
              return false;
         public Transferable createTransferable(JComponent comp) {
              if (comp instanceof JLabel) {
                   JLabel label = (JLabel)comp;
                   Icon icon = label.getIcon();
                   if (icon instanceof ImageIcon){
                        final Image image = ((ImageIcon)icon).getImage();
                        final JLabel source = label;
                        Transferable transferable = new Transferable(){
                             public Object getTransferData(DataFlavor flavor){
                                  if (isDataFlavorSupported(flavor)){
                                       return image;
                                  return null;
                             public DataFlavor[] getTransferDataFlavors(){
                                  return flavors;
                             public boolean isDataFlavorSupported(DataFlavor flavor){
                                  return flavor.equals(DataFlavor.imageFlavor);
                        return transferable;
              return null;
         public boolean importData(JComponent comp, Transferable t){
              if (comp instanceof JLabel){
                   JLabel label = (JLabel)comp;
                   if (t.isDataFlavorSupported(flavors[0])){
                        try {
                             Image image = (Image)t.getTransferData(flavors[0]);
                             ImageIcon icon = new ImageIcon(image);
                             label.setIcon(icon);
                             return true;
                        catch (UnsupportedFlavorException ignored){
                        catch (IOException ignored) {
              return false;
    What this codes does is to get the image from the imageicon and replace the image to the imageicon that you drag the source from. However, I had no clue how I can get the source's file name. Anyone can teach me how?
    Thanks.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • Child panel in parent panel- strange top value

    Hi!
    I want to write application where I have menu with buttons. Depending on the button pressed the buttons should hide and another design bring on front.
    I don't have any ideas except make that on different panels. I made one parent panel, and a few child panels. When I click button the panel with buttons hides and I'm displaing another panel with width and height equal to parent panel. I can't understand why I have to always have value equal to 30 in top property of child panel. Left property is set o zero and it's displaing OK. When I set 0 to top property the child panel is partially hidden in parent window.
    Why the top property have to be always set to 30 to display exactly panel in panel? I tried to switch on and off titlebar, but it seems taht is no problem. Anyone can explain me that behaviour? Maybe any other ideas to make that kind of application? I don't want to use tabs.

    I'm attaching some pictures so that there's no confusion over what I'm talking about. I'm using the userint/panels.cws, but I modified childPanel1 to be the exact height of the parent panel as you can see in this picture taken from the UI Editor:
    If I don't programmatically set the top of the panel and auto-center vertically is not set to true, then I get this when the panel is loaded. You can see that the panel is being cut off because it loads inside the parent panel using the top value set in the UIEditor.
    If I set the child panel to auto-center vertically it looks like this. You can see that the child panel's title bar is partially obscured due to the auto-centering, and the splitter at the bottom of the child panel is not visible.
    If I then programmatically set the top and left to (0,0) I get this. The frame of the child panel (including the title bar) is hidden, but you can see that everything on the panel is visible:
    If I programmatically set the top to 30 it looks like this. The title bar is visible but you can see that the control at the bottom of the panel is now no longer visible:
    I'm assuming that you're seeing the behavior in the 4th screen shot when you set the top to 0 and something similar to the 5th screen shot when you set the child panel top to 30. I'm guessing that due to the font being used on your machine, the thickness of the child panel title bar is actually 30 so it doesn't stand out quite as much as mine.
    When setting the position of a panel inside another panel, the frame (which includes the titlebar) is ignored, so that (0,0) cooresponds to the very first pixel that is actually visible in the panel. I'm guessing that what you want to do is either:
    A) Disable the titlebar in the child panel and set the frame thickness to 0, or
    B) If you need the titlebar of the child panel to visible, change the height of the of the child panel to be = (height of parent panel - 30).
    Kevin B.
    National Instruments

  • How to add objects to panel from another class?

    Hi this is what i am trying to do. I have a drag and adrop tool working where the users and select objects on a small panel and drag them to another panel called the tpan. What i want to do is create another class, which creates objects and now i want to display these objects on the tpan. So say i have a class DisplayTpan(), this class is used to display the objects which have been dragged from the small panel, and objects on this panel have mouselisteners attached, so that these objects can be moved around on the tpan. I have created another class called creatObj(), and from this class i want to add objects to the tpan. The DisplayTpan class extends a Jpanel, would this be he case for the CreateObj() class? In the CreateClass i have made a call to DisplayTpan t = new DisplayTPan();
    t.add(object);
    But this does not add the object to the panel, any ideas on how it should be done?
    Problem number two i have is say, I have two objects created on that oanel, i now want to draw a line t connect the two objects, this is just simply a call to the drawLine function but how would it be possible to add a ,mouselistener to that line, so it can be extended moved around etc? Any help much appreciated thanks.

    As for your first problem...too confusing...too tired...mb tomorrow it will make sense :)
    Fer the second...no need to add a mouse listener to each line. There are a couple options that spring to mind, the easiest I can think of is just check to see if the mouse click intersects with any of the lines (bit of geometry).
    The second, ugly but a hella allot more accurate and better (me thinks), is to create a bounding box around the line, so the user doesn't have to click right right on the line. I created this bounding box by painting the pixels with a special key to correspond to that line. The other nice thing about this key is the fact that the lookup is quick.
    The first step was the create a array of integers the size of the surface. Whenever a line is drawn on the graphical surface, do a corresponding line in the integer array, and create the bounding box inside this invisible array. Now whenever the user clicks just do a lookup into this array and check to see which line was selected...then go to town.
    If you want a more detailed explination, i'll post some code later.

  • How can transfer data from a dialog to another panel?

    Will anyone help me?Thanks.
    In our project,When the user need to fill some data into the textfield in a panel,he can simplify this action by click a button,when the button is clicked,then a dialog which contains a table of all the imformation of all customers will show,then the user can select one customer's information from the table,after click a button such as "OK",the selected information will show in corresponding textfiled in another panel.Because the dialog and the panel are two
    different object,so how can I transfer the data selected from the dialog to the panel and show the updated information timely??

    try this program. it exchanges some data between s adialog and a frame.
    akz
    * @version 1.20 01 Sep 1998
    * @author Cay Horstmann
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class DataExchangeTest extends JFrame
    implements ActionListener
    {  public DataExchangeTest()
    {  setTitle("DataExchangeTest");
    setSize(300, 300);
    JMenuBar mbar = new JMenuBar();
    setJMenuBar(mbar);
    JMenu fileMenu = new JMenu("File");
    mbar.add(fileMenu);
    connectItem = new JMenuItem("Connect");
    connectItem.addActionListener(this);
    fileMenu.add(connectItem);
    exitItem = new JMenuItem("Exit");
    exitItem.addActionListener(this);
    fileMenu.add(exitItem);
    public void actionPerformed(ActionEvent evt)
    {  Object source = evt.getSource();
    if (source == connectItem)
    {  ConnectInfo transfer= new ConnectInfo("yourname", "pw");
    if (dialog == null)
    dialog = new ConnectDialog(this);
    if (dialog.showDialog(transfer))
    {  String uname = transfer.username;
    String pwd = transfer.password;
    Container contentPane = getContentPane();
    contentPane.add(new JLabel("username=" + uname + ", password=" + pwd),"South");
    validate();
    else if(source == exitItem)
    System.exit(0);
    public static void main(String[] args)
    {  JFrame f = new DataExchangeTest();
    f.show();
    private ConnectDialog dialog = null;
    private JMenuItem connectItem;
    private JMenuItem exitItem;
    class ConnectInfo
    {  public String username;
    public String password;
    public ConnectInfo(String u, String p)
    { username = u; password = p; }
    class ConnectDialog extends JDialog implements ActionListener
    {  public ConnectDialog(JFrame parent)
    {  super(parent, "Connect", true);
    Container contentPane = getContentPane();
    JPanel p1 = new JPanel();
    p1.setLayout(new GridLayout(2, 2));
    p1.add(new JLabel("User name:"));
    p1.add(username = new JTextField(""));
    p1.add(new JLabel("Password:"));
    p1.add(password = new JPasswordField(""));
    contentPane.add("Center", p1);
    Panel p2 = new Panel();
    okButton = addButton(p2, "Ok");
    cancelButton = addButton(p2, "Cancel");
    contentPane.add("South", p2);
    setSize(240, 120);
         //custom method to create and buttons to a container
    JButton addButton(Container c, String name)
    {  JButton button = new JButton(name);
    button.addActionListener(this);
    c.add(button);
    return button;
    public void actionPerformed(ActionEvent evt)
    {  Object source = evt.getSource();
    if(source == okButton)
    {  ok = true;
    setVisible(false);
    else if (source == cancelButton)
    setVisible(false);
    public boolean showDialog(ConnectInfo transfer)
    {  username.setText(transfer.username);
    password.setText(transfer.password);
    ok = false;
    show();
    if (ok)
    {  transfer.username = username.getText();
    transfer.password = password.getText();
    return ok;
    private JTextField username;
    private JTextField password;
    private boolean ok;
    private JButton okButton;
    private JButton cancelButton;

  • How to send images into another panel

    hi i got two panels
    and one panel creates a couple of images
    and the confusing thing is that i don't want that panel to display my images
    but i want the other side panel to display these images..
    but i don't know how to do it.
    actually i can create two panels in one class and just add them up, but
    my code is quite long and i need to separate these two panels..

    sorry i mean JLabel
    i want to send my JLabel to another panel....

  • JPanel not repainting if embedded in another panel

    I have a reusable class called as DuplicatorPanel. This class has two add/ remove buttons like in Mac if you are familiar.
    The problem is that the screen gets updated with an extra row in case i have a main method in Duplicator itself. However if i have that in another panel as in HyperlinkBuilder then it does not repaint itself.
    Please can someone help ?
    package sam.dnd;
    import java.awt.event.ActionEvent;
    import java.util.Iterator;
    import java.util.LinkedHashMap;
    import java.util.Map;
    import javax.swing.AbstractAction;
    import javax.swing.Action;
    import javax.swing.Box;
    import javax.swing.BoxLayout;
    import javax.swing.JButton;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    public abstract class DuplicatorPanel extends Box {
        private static final long serialVersionUID = 1L;
        private static int oneup = 0;
        private Map<String, JComponent> panelMap = new LinkedHashMap<String, JComponent>();
        private Action addAction;
        private Action removeAction;
        public DuplicatorPanel() {
            super(BoxLayout.Y_AXIS);
            this.initComponents();
            this.placeComponents();
        private void initComponents() {
            addAction = new AddAction();
            removeAction = new RemoveAction();
            addDuplicatorPanel();
        private void placeComponents() {
            Iterator<JComponent> itr = this.panelMap.values().iterator();
            while(itr.hasNext()) {
                JComponent comp = (JComponent) itr.next();
                this.add(comp);
            this.add(Box.createVerticalGlue());
        private void addDuplicatorPanel() {
            int index = oneup++;
            JComponent component = this.createDuplicate();
            JButton btnAdd = new JButton("Add");
            btnAdd.setActionCommand("" + index);
            btnAdd.addActionListener(this.addAction);
            JButton btnRemove = new JButton("Remove");
            btnRemove.setActionCommand("" + index);
            btnRemove.addActionListener(this.removeAction);
            Box box = Box.createHorizontalBox();
            box.add(component);
            box.add(Box.createHorizontalGlue());
            box.add(btnAdd);
            box.add(btnRemove);
            this.panelMap.put("" + index, box);
            fireValueChanged();
        private void fireValueChanged() {
            this.removeAll();
            this.placeComponents();
            this.repaint();
        protected void doAdd() {
            addDuplicatorPanel();
            fireValueChanged();
        protected void doRemove(String key) {
            if(this.panelMap.size() == 1) {
                return;
            JComponent comp = this.panelMap.remove(key);
            if(comp != null) {
                this.fireValueChanged();
        protected abstract JComponent createDuplicate();
        class AddAction extends AbstractAction {
            private static final long serialVersionUID = 1L;
            public void actionPerformed(ActionEvent evt) {
                doAdd();
        class RemoveAction extends AbstractAction {
            private static final long serialVersionUID = 1L;
            public void actionPerformed(ActionEvent evt) {
                JButton source = (JButton) evt.getSource();
                doRemove(source.getActionCommand());
         * @param args
        public static void main(String[] args) {
            JFrame f = new JFrame();
            f.add(new MyDuplicatorPanel());
            f.setSize(200, 200);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setVisible(true);
        static class MyDuplicatorPanel extends DuplicatorPanel {
            private static final long serialVersionUID = 1L;
            public JComponent createDuplicate() {
                return new JLabel("Test");
    }This is another class that calls the above
    package sam.dnd;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    import java.util.HashMap;
    import javax.swing.JComboBox;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    public class HyperLinkBuilder extends JPanel {
        private static final long serialVersionUID = 1L;
         * @param args
        public static void main(String[] args) {
            JFrame f = new JFrame();
            f.add(new HyperLinkBuilder());
            f.setSize(200, 200);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setVisible(true);
        private static HashMap<String, String> linkNameValueMap = new HashMap<String, String>();
        private static HashMap<String, String> paramNameValueMap = new HashMap<String, String>();
        private static HashMap<String, String> valueNameValueMap = new HashMap<String, String>();
        static {
            linkNameValueMap.put("Name1", "Value1");
            linkNameValueMap.put("Name2", "Value2");
            linkNameValueMap.put("Name3", "Value3");
            paramNameValueMap.put("Name1", "Value1");
            paramNameValueMap.put("Name2", "Value2");
            paramNameValueMap.put("Name3", "Value3");
            valueNameValueMap.put("Name1", "Value1");
            valueNameValueMap.put("Name2", "Value2");
            valueNameValueMap.put("Name3", "Value3");
        private String link;
        private JComboBox linkCombo;
        private DuplicatorPanel panel;
        public HyperLinkBuilder() {
            this.initComponents();
            this.placeComponents();
            this.addListeners();
        private void initComponents() {
            linkCombo = new JComboBox(linkNameValueMap.keySet().toArray());
            panel = new MyDuplicatorPanel();
        private void placeComponents() {
            this.setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            this.add(linkCombo, gbc);
            gbc.gridy++;
            gbc.fill = GridBagConstraints.BOTH;
            this.add(panel, gbc);
        private void addListeners() {
            this.linkCombo.addItemListener(new LinkSelector());
        private JPanel createNameValuePanel() {
            JComboBox nameCombo = new JComboBox(paramNameValueMap.keySet().toArray());
            JComboBox valueCombo = new JComboBox(valueNameValueMap.keySet().toArray());
            JPanel panel = new JPanel();
            panel.add(nameCombo);
            panel.add(valueCombo);
            return panel;
        private void doLinkSelected() {
            String name = (String) linkCombo.getSelectedItem();
            link = linkNameValueMap.get(name);
        public String build() {
            //TODO:
            return this.link;
        class LinkSelector implements ItemListener {
            public void itemStateChanged(ItemEvent e) {
                doLinkSelected();
        class MyDuplicatorPanel extends DuplicatorPanel {
            private static final long serialVersionUID = 1L;
            @Override
            protected JComponent createDuplicate() {
                return HyperLinkBuilder.this.createNameValuePanel();
    }

    Hey Xeon,
    Replace that repaint() with revalidate. That is the trick in there.
    -Sam

  • Adding a panel on top of another panel

    Hi all,
    I am using NetBeans and have created a JApplet with a JPanel inside (given the default name of panel), and I want to add another panel on top of that.
    I have another class, MyPanel that extends JPanel, and I have put some simple graphics in and tested this with a JFrame. In the JFrame, the myPanel is showing up with its graphics.
    I tried to add myPanel to panel, but I cannot see myPanel.MyPanel myPanel = new MyPanel();
    panel.add(myPanel);Can somebody please point me in the right direction to get myPanel on top of another panel?
    Thanks,
    Jeff
    Edited by: jeff_cia on Nov 20, 2008 6:54 PM

    You would do well to read the section on the [layout managers|http://java.sun.com/docs/books/tutorial/uiswing/layout/index.html] at the Sun tutorial site.

Maybe you are looking for

  • Missing CD/DVD drive after windows 8.1 installation

    Hi, Late last year I updated to Windows 8.1 (from 8.0, I only purchsed my notebook in October last year).Since the update I have had numerous problems, but I have managed to work my way through all the issues & resolve them. But a week ago I started

  • PSE 9: Organizer crashes

    When I run Organizer I get the following error message.  "Elements 9 Organizer has encountered a problem and needs to close.  We are sorry for the inconvenience." Organizer will start, but this message will appear when I attempt a command like facial

  • Membership question

    Hi, My name is Pascal De Munck. I have a single app membership for 1 yr for Photoshop cc and pay approx 25 euro a month for that.  I also have a full lightroom 5 version for 129 euro. I now see that there is an adobe photoshop program including Photo

  • How to get what's app for Nokia C2-03.

    There is no what's app for Nokia C2-03. Is Nokia planning to develop it for this model? I need it, so please let me know.

  • My IMac just stopped recognizing my Iphone and Itouch when I connect them?  Any idea why?

    My IMac just stopped recognizing my Iphone and Itouch when I connect them?  Any idea why or how to fix this?