How to put a JPanel in a JTable

I have been having problems with using a JPanel in each cell in a column of a JTable. I have a class "myPanel" which extends JPanel and has an overridden paint() function. In each row I want the "myPanel" to be displayed in the cell.
I've read all the documentation(that I could find online), and have searched for a while, but have not found the right way to do this.
I currently have defined myPanel like this:
class myPanel extends JPanel implements TableCellRenderer{
public Component getTableCellRendererComponent( ... ) {
repaint();
public void paint(Graphics g) { ... }
So, In my paint function I am not sure which cell(in the table) needs to be painted, but in the getTableCellRendererComponent, I know the cell to be rendered but I can't actually paint anything(I dont have a Graphics object to paint on).
SO either I get my graphics painted into every cell in the column identically, or I don't see anything in the cells. How do I paint differently in different cells.
Should I be using the object passed into getTableCellRendererComponent directly?
And I don't think there is a default renderer for a panel which would display the panel.
Any hints would be wonderful, as I can't get this to work.

You should use your myPanel with the params row and column as if the panel in that row and that column is the one to paint and return it from the method getCellRendererComponent, like in this code.
public Component getTableCellRendererComponent(...){
//....set the properties of your panel/CellRenderer as needed
//for displaiing the cell located in the row and column you get as //parameters,
//even adding or removing buttons or other stuffs from your panel
return this;
// you return the CEllRenderer itself !!! The table will do the painting work !!
That's it !!!
Do not use paint or repaint, you don't need it !!!
Bye Massimo

Similar Messages

  • How to put a Jpanel always over a JtextArea ?

    I want to have a little Jpanel above a JTextArea.
    At the initialize section, I add first the Jpanel and after the JtextArea (both in a 'parent' Jpanel with null layout)
    When I run the program I see the Jpanel over the JtextArea, but when I write on it and it reaches the area when the Jpanel is placed, the JtexArea brings to front and the Jpanel is no longer visible.
    So, how can I have my Jpanel on top always ?
    And another question , is there a hierarchy that control that I Jpanel is always over a Jbutton (even if the initial z-order of the button is higher than the Jpanel's)
    Thanks
    ( I do not send any code because it is so simple as putting this two components into a Jpanel)

    tonnot wrote:
    And another question , is there a hierarchy that control that I Jpanel is always over a Jbutton (even if the initial z-order of the button is higher than the Jpanel's)JLayeredPane
    ( I do not send any code because it is so simple as putting this two components into a Jpanel)Try it with a JLayeredPane and if you still have problems, post a [SSCCE (Short, Self Contained, Compilable and Executable, Example Program)|http://mindprod.com/jgloss/sscce.html].
    db

  • How to add a jpanel in a jtable in a Jinternal frame

    hi to all,
    I want to add a panel above the table,which has been created in an internal frame,in the internal frame.
    i have created the table within the internal frame but unable to add panel in it..
    thanks

    Read the Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/layout/visual.html]How to Use Layout Managers. Mix and match layout managers to get the desired effect.

  • How to put data in JTable from database

    hi everyone,
    i want to query the databse and put the result in the JTable and if possible i want to edit some information. For example: i have 5 records in the database query them and place them in JTable and each record has its own status. if i want to change the status For Example: the status of the 4 records are "Cleared" and the other one is "RELEASEd" so if i have JCombo Box to set the 4 records in to RELEASED status and then save the changes is that possible?
    thank you
    dhing

    In [url /thread.jsp?forum=54&thread=387565]this thread I describe how to use a result set as a basis for table data. Check some database tutorials for information on getting a result set.

  • How to place a JPanel at the center of another JPanel?

    Can anyone tell me how to place a JPanel inside another JPanel? The first JPanel consists of three JPanels each consisting of a label&textfield and are placed using BoxLayout. The second JPanel is a big blank screen. So, i would like to put the first JPanel at the center of second JPanel(horizontally & vertically). The problem is that i don't have any other component. So,if i try to use FlowLayout, i am able to put it at the center(horizontally, not vertically) only on the top edge. I tried to use BoxLayout. But, i couldn't(as i don't have any other elements). I tried to create some blank elements. But, they are messing up the elements of my JPanel. Any cluesssssssss??????????

    import java.awt.*;
    import javax.swing.*;
    public class CenteredLayout
        private JPanel getPanel()
            GridBagLayout gridbag = new GridBagLayout();
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.insets = new Insets(2,2,2,2);
            JPanel p = new JPanel(gridbag);
            addComponents(new JLabel("label 1"), new JTextField(8), p, gbc);
            addComponents(new JLabel("label 2"), new JTextField(8), p, gbc);
            addComponents(new JLabel("label 3"), new JTextField(8), p, gbc);
            JPanel panel = new JPanel(gridbag);
            panel.setBackground(Color.pink);
            gbc.anchor = GridBagConstraints.CENTER;
            gbc.insets = new Insets(0,0,0,0);
            panel.add(p, gbc);
            return panel;
        private void addComponents(Component c1, Component c2, Container c,
                                   GridBagConstraints gbc)
            gbc.anchor = GridBagConstraints.EAST;
            gbc.gridwidth = GridBagConstraints.RELATIVE;
            c.add(c1, gbc);
            gbc.anchor = GridBagConstraints.WEST;
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            c.add(c2, gbc);
        public static void main(String[] args)
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.getContentPane().add(new CenteredLayout().getPanel());
            f.setSize(400,400);
            f.setLocation(200,200);
            f.setVisible(true);
    }

  • Putting a jpanel into a jframe

    I have a main JFrame which was created by:
    public class ControllerGUI extends javax.swing.JFrameand inside this somewhere is a JPanel called myPanel.
    I have a seperate class which is a JPanel created by:
    public class Calc extends javax.swing.JPanelI am quite new to swing and have been using the GUI builder in Netbeans, so could someone please tell me how to put the seperate Calc JPanel into the JPanel myPanel which is in the JFrame. Is it something to do with creating a container for it? Thanks.
    Thanks.

    Not sure if you're still around, but here's a very simple example of putting a JPanel derived object into another JPanel. The Calc class here extends JPanel. It does nothing but shows a 3 by 3 grid of JLabels:
    import java.awt.Color;
    import java.awt.GridLayout;
    import javax.swing.BorderFactory;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.SwingConstants;
    class Calc extends JPanel
        public Calc()
            setBorder(BorderFactory.createEmptyBorder(15, 0, 15, 0));
            // what the heck, have it show a 3x3 label grid
            setLayout(new GridLayout(0, 3, 10, 10)); // sets layout for the grid
            for (int i = 0; i < 3; i++)
                for (int j = 0; j < 3; j++)
                    String labelString = "[" + (i + 1) + ", " + (j + 1) + "]";
                    JLabel label = new JLabel(labelString);
                    label.setBorder(BorderFactory.createLineBorder(Color.blue));
                    label.setHorizontalAlignment(SwingConstants.CENTER);
                    add(label); // adds each label to the grid
    }Now I'll add it to myPanel which is a JPanel object, but before adding it, I'll set the myPanel's layout to be BorderLayout, and I'll add the Calc class to the CENTER position:
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.GridLayout;
    import javax.swing.BorderFactory;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    public class FooFrame extends JFrame
        // declare and initialize the myPanel JPanel object and
        // the myCalc Calc object:
        private JPanel myPanel = new JPanel();
        private Calc myCalc = new Calc();
        public FooFrame(String s)
            super(s);
            getContentPane().add(myPanel); // add myPanel to the contentPane making it the main panel
            setPreferredSize(new Dimension(500, 400));
            myPanel.setLayout(new BorderLayout());  // here set the layout of mypanel
            myPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
            // create a title panel and add it to the top of myPanel
            JLabel title = new JLabel("Application Title");
            JPanel titlePanel = new JPanel(new FlowLayout()); // redundant since jpanels
                                                            //use flowlayout by default
            titlePanel.add(title);
            myPanel.add(titlePanel, BorderLayout.PAGE_START); // adds title to top of myPanel
            // create a button panel and add it to the bottom of myPanel
            // here we'll use gridlayout
            JPanel buttonPanel = new JPanel(new GridLayout(1, 2, 15, 0));
            JButton okButton = new JButton("OK"); // create buttons
            JButton cancelButton = new JButton("Cancel");
            buttonPanel.add(okButton); // add buttons to button panel
            buttonPanel.add(cancelButton);
            myPanel.add(buttonPanel, BorderLayout.PAGE_END); // adds buttonpanel to bottom of myPanel
            //** add the myCalc object to the center of the myPanel via borderlayout
            myPanel.add(myCalc, BorderLayout.CENTER);
        // initialize and show the JFrame
        private static void createAndShowUI()
            JFrame frame = new FooFrame("FooFrame");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        // but be careful to show the JFrame in a thread-safe manner:
        public static void main(String[] args)
            java.awt.EventQueue.invokeLater(new Runnable()
                public void run()
                    createAndShowUI();
    }

  • How to put Status bar

    How to put a status bar in a JFrame by which some informations that will be done in the application should be displayed in the status bar.

    Add a JPanel to your JFrame and tell the program this is your contentPane
    Next you set the contentPane's Layout a BorderLayout.
    Last you've to add a JLabel to the contentPane.
    Now you can set a statusText or an Icon...
    hope it helps,
    Thof

  • How to put web app to Oracle 8i Jserver ?

    I've developed a web app with JDev3.1 , Oracle8i 8.1.6 on NTServer4.0.
    and want to put it on Oracle Jserver 8.1.6:
    The app works well within JDev3.1.
    I depolyed my Business Components project and test well.
    I would like to know how to put web app to Jserver. Is it possible?

    when you say "web app" i am assuming you mean an applet, servlet or java server page(jsp) ??
    if you mean this, then you need to move to the rdbms 8.1.7 environment for this support.
    rdbms 8.1.7 supports servlets and jsp's in two different ways :
    externally -- on the rdbms 8.1.7 platform
    they can be run in an external apache front end included with the 8.1.7 rdbms.
    internally -- inside the rdbms 8.1.7
    they can be deployed internally to 8.1.7 in the "oracle servlet engine" (ose)
    applets and html static pages still need to be deployed to a web server on the mid-tier OR the external apache front end included with the 8.1.7 rdbms to optimize performance.
    i hope this helps ...
    p.s. if i've misunderstood your question , please restate it.
    null

  • How to scroll a JPanel?

    I have class, which extends JPanel, in which I override
    the paintComponent(Graphics g) method
    I am drawing a graph and, depending on the input values, the graph can be wider than the JPanel. I would like to be able to add a scroll bar so the whole of the graph can be viewed.
    I have tried to make the JPanel implement scrollable
    and I have tried putting the JPanel within a JScrollpane
    but neithor work.
    please help!
    Simon

    You should be able to add the JPanel to the JScrollPane by doing the following (setting the scrollbars as required)
    // create a panel to hols the scrollpane
    JPanel aPanel = new JPanel();
    // create the scrollpane with your graph in it = yourPanel
    // disable the vertical scrollbar and enable the hoziontal
    JScrollPane aScrollPane = new JScrollPane(yourGraph, JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    // set the scrollpane to the same size as the panel
    aScrollPane.setSize(aPanel.getSize());
    // add the scrollpane to the panel
    aPanel.add(aScrollPane);
    // refresh the layout
    aScrollPane.doLayout();
    Hope this helps - I had the same problem a couple of days ago!

  • How to make a JPanel selectable

    When extending a JPanel and overriding the paintComponent() method the custom JPanel can not be selected so that it gets for example KeyEvents.
    But if I make the new Class extend a JButton it gets of course selected and able to receive for example KeyEvents.
    My question is therefore; what does the JButton implement that a JPanel doesn&#8217;t so that a JButton gets selectable? Or in other words; how to make a JPanel selectable?
    Aleksander.

    Try this extended code. Only the first panel added can get the Focus.
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    class Test extends JFrame
      public Test()
        setLocation(400,300);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        JPanel panel1 = new JPanel(new BorderLayout());
        JPanel panel2 = new JPanel(new BorderLayout());
        ImagePanel imgPanel = new ImagePanel();
        panel1.setFocusable(true);
        panel2.setFocusable(true);
        panel1.setPreferredSize(new Dimension(0, 50));
        panel2.setPreferredSize(new Dimension(0, 50));
        panel1.setBorder(BorderFactory.createLineBorder(Color.RED,     4));
        panel2.setBorder(BorderFactory.createLineBorder(Color.CYAN,    4));
        imgPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 4));
        panel1.add(new JLabel("Panel 1"), BorderLayout.CENTER);
        panel2.add(new JLabel("Panel 2"), BorderLayout.CENTER);
        getContentPane().add(panel1, BorderLayout.NORTH);
        getContentPane().add(panel2, BorderLayout.SOUTH);
        getContentPane().add(imgPanel, BorderLayout.CENTER);   
        pack();
        panel1.addKeyListener(new KeyAdapter(){
            public void keyPressed(KeyEvent ke){
                System.out.println("Panel1");}});
        panel2.addKeyListener(new KeyAdapter(){
            public void keyPressed(KeyEvent ke){
                System.out.println("Panel2");}});
      public static void main(String[] args){new Test().setVisible(true);}
    class ImagePanel extends JPanel
      Image img;
      public ImagePanel()
        setFocusable(true);
        setPreferredSize(new Dimension(400,300));
        try{img = javax.imageio.ImageIO.read(new java.net.URL(getClass().getResource("Test.gif"), "Test.gif"));}
        catch(Exception e){/*handled in paintComponent()*/}
        addKeyListener(new KeyAdapter(){
          public void keyPressed(KeyEvent ke){
            System.out.println("ImagePanel");}});
      public void paintComponent(Graphics g)
        if(img != null)
          g.drawImage(img, 0,0,this.getWidth(),this.getHeight(),this);
        else
          g.drawString("This space for rent",50,50);
    }

  • How to put a few pages together as a panel group in the same screen ?

    Dear Friends,
    Hello. I am developing the accounting application of Journal Entry module. I have developed 5 pages for a journal entry: Journal header, Journal Line, Journal total, Journal Error and Journal Approval. I want to put 5 pages together as a panel group in the same screen so that users are convenient to use them. The format is as follows:
    Header | Lines | Total | Error | Approval
    When a user click on Header or Lines or Total or Error or Approval, its related page is displayed below it on the screen. But I don't understand how to put "Header", "Lines", "Total", "Error" and "Approval" together as above format and link them to its related page. Can anybody tell me how to do that ? Thanks.
    Lucy

    Mike,
    It's correct to make 5 pages as a seperate component.
    I am developing an accounting system using PeopleTools by myself.
    Because you understand PeopleSoft financial application, please tell me the 2 questions as follows:
    First, In Journal Line page, when select "edit Journal" and click button "process", the system check the Journal against Budget, if it's Ok, the System assign Journal ID for the Journal and put "valid" into the field of "Budget Status" and "Journal Status" in Total page. If it's not OK, the system send error message into Error page. Do you know how developer do that ?
    Second, In Journal Line Page, when select "Submit Journal" and click button "process", the system send the Journal to Approval page for approval. Do you know how developer do that ?
    Lucy

  • How to put text line just for a moment ?

    Hi,
    Do you know how to put a text line  just  for one photo? I mean I have more photos  in one portfolio but i need the text line to be seen just for one photo for the others no.
    Thank you in advance.

    You can use composition for this , where you can insert text frame for that specific photo where you want to add some description and for rest dont use text frame.
    Thanks,
    Sanjit

  • How to put separator lines in menu items in a gui status ?

    Hi,
    I have created gui status for my z transaction
    I need the separator lines in the menu items like it is seen in std transactions
    For example, in any screen , in the Help menu, we can see the following.
    Application Help
    SAP Library
    Glossary
    (line)
    Release notes
    (line)
    SAP Service Marketplace
    could you please help me how to put the lines ?
    thanks

    Hi,
    Goto Your GUI status.click on your  MENU bar.
    under the menu bar you will have your own menu items.
    where ever you want separator line  do the following way.
    you will have 2 columns under menu bar
    1) code
    2) text.
    under code dont put anything.
    but under text column you put dashes and enter or select right click there and choose separator line.
    you will get separator line.
    activate and run the ztcode.you will get .
    code                                            text
    Application Help                         
    SAP Library
    Glossary
    Release notes
    SAP Service Marketplace
    Thanks
    Parvathi

  • I have an ipod touch and i put cds on itunes on my pc , but i cant figure out how to put the music from the cds on my ipod .. can u help me ?

    i have an ipod touch and i put cds on itunes onmy computer , but i cant figure out how to put the music from the cds on my ipod touch can anyone help me?

    ok i select th e music from my music libary and go to file and click sync to my ipod and when it gets to step 3.. it says waiting for changes to be made.. yes the songs play on the itunes ..  and no it wasnt the same computer i used last time

  • Can't figure out how to PUT

    First off, I'm a Newbie to Dreamweaver. So. I'm in a real
    learning phase.
    I can't figure out how to "Put" my development file to my web
    Server. I have an ftp connection. At least I think I do since I
    have file list that seems to be pointing to the files I have on
    webserver. I manually copied my files from my development folder to
    my web server. Now I want to use Dreamweaver to put my files and
    then I want to have Dreamweaver manage my file so that next time I
    one have to upload my "Changed" files. I searched Dreamweaver Help
    and didn't understand what I read. Sorry..
    Appreciate any help you can give me, or point me to somewhere
    on the web that my a step by step procedure.
    Thanks....

    Please tell me this - when you look at the name of the site
    in the drop-down
    list of defined sites on the left top of the Files panel, do
    you see a
    folder icon, or a little computer icon?
    Murray --- ICQ 71997575
    Adobe Community Expert
    (If you *MUST* email me, don't LAUGH when you do so!)
    ==================
    http://www.dreamweavermx-templates.com
    - Template Triage!
    http://www.projectseven.com/go
    - DW FAQs, Tutorials & Resources
    http://www.dwfaq.com - DW FAQs,
    Tutorials & Resources
    http://www.macromedia.com/support/search/
    - Macromedia (MM) Technotes
    ==================
    "bruceaj" <[email protected]> wrote in
    message
    news:epr3mt$4mu$[email protected]..
    > First off, I'm a Newbie to Dreamweaver. So. I'm in a
    real learning phase.
    >
    > I can't figure out how to "Put" my development file to
    my web Server. I
    > have
    > an ftp connection. At least I think I do since I have
    file list that seems
    > to
    > be pointing to the files I have on webserver. I manually
    copied my files
    > from
    > my development folder to my web server. Now I want to
    use Dreamweaver to
    > put my
    > files and then I want to have Dreamweaver manage my file
    so that next
    > time I
    > one have to upload my "Changed" files. I searched
    Dreamweaver Help and
    > didn't
    > understand what I read. Sorry..
    >
    > Appreciate any help you can give me, or point me to
    somewhere on the web
    > that
    > my a step by step procedure.
    >
    > Thanks....
    >

Maybe you are looking for