How do we resize a  JPanel?

Hi,
I have 2 two JPanels and when I stick them onto a JFrame (size fixed to something), the panel on the right hand side has this annoying excess grey background. I cannot seem to remove this excess. After all, the panel on the right only contains a single column of buttons.
How do I remove the excess space??
Help

You should use a more convenient layout manager for your JFrame.
Denis

Similar Messages

  • How do I resize this JPanel withou cutting off content?

    I need to make this JPanel smaller vertically. Every time I do it cuts off the bottom of the graph. How can I make it shorter without losing content?
    TIA
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class Graph extends JFrame implements ActionListener {
         JButton button;
         int []bar = new int[4];
         float []flote = {1395,1296,1402,1522};
         String []valuesInput = {"1395","1296","1402","1522"};
         String str = "", title="Title goes here";
         String []barLabels = {"pp01, Oct. 2003","pp02, Oct. 2003","pp01, Nov. 2003","pp02, Nov. 2003"};
         String []percent = {"","","",""};
         JLabel []JLab = new JLabel[4];
         JTextField titletxt;
         JTextField []Text = new JTextField[5];
         JTextField []labeltxt = new JTextField[5];
         boolean pieChart;
         public Graph() {
              Container c = getContentPane();
              JPanel panel = new JPanel(){
                     public void paintComponent(Graphics g) {
                        Graphics2D g2 = (Graphics2D)g;
                        g2.setColor(new Color(223,222,224));
                        g2.fillRect(0,0,550,400);
                        g2.setColor(Color.orange);
                        if(pieChart) {
                             g2.fillArc(30, 130, 220, 220, 90, -bar[0]);
                             g2.fillRect(270, 170, 30, 20);
                        else g2.fillRect(30, 150, bar[0], 30);
                        g2.setColor(Color.green);
                        if(pieChart) {
                             g2.fillArc(30, 130, 220, 220, 90-bar[0], -bar[1]);
                             g2.fillRect(270, 210, 30, 20);
                        else g2.fillRect(30, 190, bar[1], 30);
                        g2.setColor(Color.red);
                        if(pieChart) {
                             g2.fillArc(30, 130, 220, 220, 90-(bar[0]+bar[1]), -bar[2]);
                             g2.fillRect(270, 250, 30, 20);
                        else g2.fillRect(30, 230, bar[2], 30);
                        g2.setColor(Color.blue);
                        if(pieChart) {
                             g2.fillArc(30, 130, 220, 220, 90-(bar[0]+bar[1]+bar[2]), -bar[3]);
                             g2.fillRect(270, 290, 30, 20);
                        else g2.fillRect(30, 270, bar[3], 30);
                        g2.setColor(Color.black);
                        g2.setFont(new Font("Arial", Font.BOLD, 18));
                        if(pieChart) g2.drawString(title, 220, 142);
                        else g2.drawString(title, 50, 132);
                        g2.setFont(new Font("Arial", Font.PLAIN,16));
                        int temp=0;
                        if(pieChart) temp = 185;
                        else temp = 172;
                        for(int j=0; j <4; j++) {
                        if(pieChart) g2.drawString("$"+valuesInput[j]+" "+barLabels[j]+percent[j], 305, temp);//XXXXXXXXXXXXXXX
                        else g2.drawString("$"+valuesInput[j]+" "+barLabels[j]+percent[j], bar[j]+40, temp);
                        temp += 40;
                        if(!pieChart){
                             g2.drawLine(30, 130, 30, 300);
                             g2.drawLine(30, 300, 430, 300);
                             //g2.drawLine(210, 345, 210, 350);
                             //g2.drawLine(390, 345, 390, 350);
                             g2.setFont(new Font("Arial", Font.PLAIN,12));
                        super.paintComponent(g2);
              panel.setOpaque(false);
              panel.setLayout(new FlowLayout() );
              button = new JButton("Bar Graph");
              button.addActionListener(this);
              for (int k=0; k<4; k++){
                   str = Integer.toString(k+1);
              panel.add(button);
              c.add(panel);
         public void actionPerformed(ActionEvent e) {
              String command = e.getActionCommand();
              if(command.equals("Bar Graph")){
                   button.setText("Pie Chart");
                   pieChart=false;
                   try {
                        int temp =0;
                        java.text.DecimalFormat df = new java.text.DecimalFormat("#0.#");
                        for (int j=0; j<4; j++){
                             //flote[j] = Float.parseFloat(Text[j].getText());//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
                             temp += (int)((flote[j]) +0.5);
                        for (int k=0; k<4; k++){
                        bar[k] = (int)(((flote[k]/temp) * 360)+0.5);
                             //barLabels[k] = labeltxt[k].getText();XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
                             flote[k] = (flote[k]/temp) *100;
                             percent[k] = ": "+df.format(flote[k])+"%";
                   catch(Exception message){
                        title = "Oops! Complete all fields, enter numbers only";
              if(command.equals("Pie Chart")){
                   button.setText("Bar Graph");
                   pieChart=true;
              repaint();
         public static void main(String[] args) {
         Graph frame = new Graph();
         frame.setSize(550,400);
         //frame.setLocation(200, 200);
         frame.setResizable(false);
         frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
         frame.setVisible(true);

    Try with this (it really works!):
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class Graph extends JFrame implements ActionListener {   
        int width;
        int height;
         JButton button;
         int []bar = new int[4];
         float []flote = {1395,1296,1402,1522};
         String []valuesInput = {"1395","1296","1402","1522"};
         String str = "", title="Title goes here";
         String []barLabels = {"pp01, Oct. 2003","pp02, Oct. 2003","pp01, Nov. 2003","pp02, Nov. 2003"};
         String []percent = {"","","",""};
         JLabel []JLab = new JLabel[4];
         JTextField titletxt;
         JTextField []Text = new JTextField[5];
         JTextField []labeltxt = new JTextField[5];
         boolean pieChart;
         public Graph() {
              Container c = getContentPane();
              JPanel panel = new JPanel(){
                     public void paintComponent(Graphics g) {
                         width = getWidth();
                         height = getHeight();
                        Graphics2D g2 = (Graphics2D)g;
                        //g2.scale(.95,.95);
                        g2.setColor(new Color(223,222,224));
                        g2.fillRect(0,0,width,height);
                        g2.setColor(Color.orange);
                        if(pieChart) {
                             g2.fillArc(getW(30), getH(130), getW(220), getH(220), 90, -bar[0]);
                             g2.fillRect(getW(270), getH(170), getW(30), getH(20));
                        else g2.fillRect(getW(30), getH(150), getW(bar[0]), getH(30));
                        g2.setColor(Color.green);
                        if(pieChart) {
                             g2.fillArc(getW(30), getH(130), getW(220), getH(220), 90-bar[0], -bar[1]);
                             g2.fillRect(getW(270), getH(210), getW(30), getH(20));
                        else g2.fillRect(getW(30), getH(190), getW(bar[1]), getH(30));
                        g2.setColor(Color.red);
                        if(pieChart) {
                             g2.fillArc(getW(30), getH(130), getW(220), getH(220), 90-(bar[0]+bar[1]), -bar[2]);
                             g2.fillRect(getW(270), getH(250), getW(30), getH(20));
                        else g2.fillRect(getW(30), getH(230), getW(bar[2]), getH(30));
                        g2.setColor(Color.blue);
                        if(pieChart) {
                             g2.fillArc(getW(30), getH(130), getW(220), getH(220), 90-(bar[0]+bar[1]+bar[2]), -bar[3]);
                             g2.fillRect(getW(270), getH(290), getW(30), getH(20));
                        else g2.fillRect(getW(30), getH(270), getW(bar[3]), getH(30));
                        g2.setColor(Color.black);
                        g2.setFont(new Font("Arial", Font.BOLD, 18));
                        if(pieChart) g2.drawString(title, getW(220), getH(142));
                        else g2.drawString(title, getW(50), getH(132));
                        g2.setFont(new Font("Arial", Font.PLAIN,16));
                        int temp=0;
                        if(pieChart) temp = 185;
                        else temp = 172;
                        for(int j=0; j <4; j++) {
                        if(pieChart) g2.drawString("$"+valuesInput[j]+" "+barLabels[j]+percent[j], getW(305), getH(temp));//XXXXXXXXXXXXXXX
                        else g2.drawString("$"+valuesInput[j]+" "+barLabels[j]+percent[j], getW(bar[j]+40), getH(temp));
                        temp += 40;
                        if(!pieChart){
                             g2.drawLine(getW(30), getH(130), getW(30), getH(300));
                             g2.drawLine(getW(30), getH(300), getW(430), getH(300));
                             //g2.drawLine(210, 345, 210, 350);
                             //g2.drawLine(390, 345, 390, 350);
                             g2.setFont(new Font("Arial", Font.PLAIN,12));
                        super.paintComponent(g2);
              panel.setOpaque(false);
              panel.setLayout(new FlowLayout() );
              button = new JButton("Bar Graph");
              button.addActionListener(this);
              for (int k=0; k<4; k++){
                   str = Integer.toString(k+1);               
              panel.add(button);
              c.add(panel);
         private int getW(int dx) {
             return (dx * width) / 550;
         private int getH(int dy) {
             return (dy * height) / 400;
         public void actionPerformed(ActionEvent e) {
              String command = e.getActionCommand();
              if(command.equals("Bar Graph")){
                   button.setText("Pie Chart");
                   pieChart=false;
                   try {
                        int temp =0;
                        java.text.DecimalFormat df = new java.text.DecimalFormat("#0.#");
                        for (int j=0; j<4; j++){
                             //flote[j] = Float.parseFloat(Text[j].getText());//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
                             temp += (int)((flote[j]) +0.5);
                        for (int k=0; k<4; k++){
                        bar[k] = (int)(((flote[k]/temp) * 360)+0.5);
                             //barLabels[k] = labeltxt[k].getText();XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
                             flote[k] = (flote[k]/temp) *100;
                             percent[k] = ": "+df.format(flote[k])+"%";
                   catch(Exception message){
                        title = "Oops! Complete all fields, enter numbers only";
              if(command.equals("Pie Chart")){
                   button.setText("Bar Graph");
                   pieChart=true;
              repaint();
        public static void main(String[] args) {
            Graph frame = new Graph();
            frame.setSize(550,400);
            //frame.setLocation(200, 200);
            frame.setResizable(true);
            frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
            frame.setVisible(true);

  • Resize a JPAnel (use of scale)

    Hello everybody,
    Here's my problem: I have a panel full of components. This panel
    can be scaled. So its size can vary. I have to modify the size of this panel (to make a zomm).
    In the printing advanced
    tutorial they say that I just have to resize the graphics in the print method with:
    g2.scale(xScaleFactor,yScaleFactor);
    But it only works for Graphics and no for JPanel..
    My questions are:
    how can I resize my Jpanel ?
    Does it scales all the components in the panel?
    Best Regards
    Peio / [email protected]

    You have to overwrite the paint method from your Painter component. For example a JFrame:
    public void paint(Graphics g){
      Graphics2D g2d = (Graphics2D)g;
      int count = getComponentCount();
      g2d.scale(scaleX, scaleY);
      for(int i = 0; i < count; i++){
        Component comp = getComponent(i);
        comp.paint(g2d);
      // Undo all
      g2d.scale(1/scaleX, 1/scaleY);
    public Dimension getPreferredSize(){
      Dimension dim = getPreferredSize();
      return new Dimension(((int)dim.getWidth() * scaleX), ((int)dim.getHeight() * scaly));
    }Good, if you have a scrollpanel... hmmm... you have to work a little more ;)

  • How to dynamically resize JPanel at runtime??

    Hello Sir:
    I met a problem, I need to resize a small JPanel called panel within a main Control JPanel (with null Layout) if I click the mouse then I can Drag and Resize this small JPanel Border to the size I need at runtime, I know I can use panel.setSize() or panel.setPreferredSize() methods at design time,
    But I have no idea how to do it even I search this famous fourm,
    How to dynamically resize JPanel at runtime??
    Can any guru throw some light or good example??
    Thanks

    Why are you using a null layout? Wouldn't having a layout manager help you in this situation?

  • How to drop component on JPanel

    Hi,
    i want to drop some component on JPanel.I'm talking about drag n drop concept, but how to apply it on JPanel.
    For eg how to drag n drop an image from your tool bar to a JPanel.
    Thanks in advance.

    Hi,
    thanks for ur reply.
    But this link is not helping me much...more idea needed....actually my question is to use 'JPanel' to drop component....how to make it work?and also how to resize the component itself by streaching it's border ( something like taht )
    thanks...
    Edited by: DJgpmax on Jun 17, 2008 2:10 AM

  • How can I resize a JFrame ,to fit into the screen size

    I have a Jframe which has JPanel and JPanel contains lot of other components.JPanel size is 980,1400. when i use JFrame.show method jpanel goes beyond the screen size in length and I am not able to see the portion below the screen.How can I resize the JFrame so that JFrame and JPanel shrinks to fit into the screen size.I need this because I have a PRINT button at bottom of the JPanel.Thanks.

    Thank you for your reply.I tried with the following code as you have told.But the frame is still going beyond the screen.Can you please look into it and tell me whats wrong ?
    //public class PlayerRegForm extends javax.swing.JFrame implements Printable
    public static void main(String args[]) {
    PlayerRegForm prf = new PlayerRegForm();
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = prf.getSize();
    if (frameSize.height > screenSize.height)
    frameSize.height = screenSize.height;
    if (frameSize.width > screenSize.width)
    frameSize.width = screenSize.width;
    System.out.println("Screen Size ----------------- " + screenSize);
    System.out.println(" Frame Size ----------------- " + frameSize);
    prf.setSize(frameSize.width, frameSize.height);
    prf.pack();
    prf.show();
    =============================================================================
    Screen Size ----------------- java.awt.Dimension[width=1024,height=768]
    Frame Size ----------------- java.awt.Dimension[width=112,height=28]

  • How do you resize multiple artboards in Illustrator at the same time?

    Hi,
    This is a question that has been bugging me for a while.  I'm currently on CS6 but I'm looking for a solution that can also work on CS5 for a co-worker.
    Having multiple artboards has been established since CS4 and I have yet to see an official way of resizing multiple artboards, if anything there should at least be a plug-in.
    As great of a feature it is, it seems a little short sited.  If one has multiple artboards one should be able to manage multiple artboards, right?
    I totally love having multiple artboards but I should be able to resize and manage multiple artboards as well.
    This is great for building icon sets but how can I resize the set all at once?
    I was considering trying to do an action but odds are I still would have to select an artboard one by one to do which doesn't save much time if you have like 30 artboards to apply this to. 
    I don't even see a batch render for artboards and Photoshop has that for its files.
    I'm well aware of the solution that is displayed here:
    http://forums.adobe.com/thread/1093465
    Which is a script.  I'm not sure where I'm supposed to put it.

    mec_os wrote:
    you can resize the artwork on export. no need to draw it to size.
    Unfotunately when I looked into that does not seem to be the case.  However I can resize when I "Save for Web" but that appears to only work for one artboard at a time.  I can Save As or Export multipe artboards but not adjust the size.  Believe me I would be happy if I could resize multiple artboards upon export but apparently that is not an option.
    mec_os wrote:
    you can put the scripts in illustrator's folder/presets/en_us(or whatever localization you have)/scripts. you can then access them in illustrator from the menubar/File/Scripts/
    there are also applications that will let you execute scripts with assigned shortcuts. i believe sparks is free.
    Thanks for pointing out the location. 
    Do I save it as a txt file or something else?
    (I was guessing that I would copy the script into notepad or something)

  • I am using a doc formatted on excel --- how do i resize the doc to fit on one page?

    I am using a document that was formatted in excel (on a PC) --- how do i resize the document to print on one page?

    Hi phylw,
    Here is one way. View > Show Print View. At the bottom of the window is a slider control called Content Scale. Slide to the left to shrink the document.
    Under File > Print... you will see a print preview that tells you how many pages the document now occupies.
    Regards,
    Ian.

  • How can I resize a photo in iPhoto without cropping it?

    I am taking pictures on my iphone 4s and using iphoto to edit them on macbook pro.  I need to upload these photos at a size much smaller than what i am able to take with my phone.  The only way I see to adjust the size of a photo in iphoto is to crop it.  How can I resize without cropping?
    Thank you

    It's an option in the Export menu.
    File -> Export and under the Size option you can choose the maximum you prefer. Export that and upload from the desktop.

  • How can I resize partition? - New larger hard drive installed

    Hello,
    The hard drive on my MacBook Pro recently died on me (just past my Apple Care warranty - lucky me).  I sent it to an authorised reseller/repair place and they installed a new hard drive for me - replacing my old 250GB with a nice new 500GB.  The problem is when they copied over the disk image from my previous hard drive they did not scale up the image (yeah, not so happy bout that).  Now I find myself with a 500GB hard drive with a 250GB partition called Macintosh HD and 250GB of unused/unusable space.  On top of that I'm just starting to get messages saying boot disk nearly full.  How can I resize the "Macintosh HD" partition to use the entire disk?  I've tried using disk utility, clicking on the main drive, going to the partition tab and dragging the Macintosh HD partition to use the entire disk but after a few seconds of a message saying "waiting for disk to reappear" it returns to the same 250GB size.  Running OSX 10.9.3.  Any suggestions?
    Thanks all!

    See the tips in pounding's section on using Disk Utility:http://pondini.org/OSX/Home.html and you could also take the route of buying an external hard drive (a great idea of maintaining a backup), format that drive for Mac OS X Extended (Journaled), clone the startup disk to that drive, then boot from the external, erase and format the internal, then clone the external back to the internal partitioned as you want.

  • How can I resize a pdf page content with annotations and highlights in Acrobat Pro?

    How can I resize a pdf page content with annotations and highlights in Acrobat Pro while maintaining page size and annotation/highlight interactivity? I've tried to use the crop tool in Acrobat Pro (9) and the page does resize, but the highlights and annotations go all over the place. I specifically need to scale the page content smaller (83%) but maintain page size and annotation/highlight to content connection and interactivity.

    An old trick (not recommended) would be to print to a new PDF with the page size selected but a scaling of the content. Only print the document, not the markup, to a new PDF. Close that PDF when done and reopen the old one (or maybe a copy of the old one for safety) and use Document Replace pages and select the new document to replace your current pages. You will then have to go back through the markup to locate it all correctly. There may be a more elegant way, but this may be the fastest.

  • When I open a file in Photoshop CS4, how can I resize My Open Window File?it is stuck on full screen

    When I Open a File in Photoshop CS4, How can I Resize My Open Window File? It is stuck on Full Screen?

    Which operating system are you using?
    On a windows os, usually one can just double click on the title bar in the open dialog.

  • I have Adobe Photoshop 9.  I am a novice.  How can I resize photos in my files using batch resize?

    I have Adobe Photoshop 9.  I am a novice.  How can I resize photos in my files using batch resize?

    In the Editor, use File...Process Multiple Files.  See the Help for more info.
    Ken

  • How does I resize a pdf file page using Acrobat Pro?

    In Acrobat Pro, how can one resize a pdf file page (21.38'' x 26.75'') into one measuring 6'' x 9'', to be submitted as the trim size? Is there a feature that allows this to be done? How does one set all print options from the Application print dialog box to enable one to adjust the dimensions of any customized file page?

    I want to scale the 21.4'' by 26.8'' content down to one that can be fitted inside a 6'' by 9'' book.
    I have this pdf that was created from the InkFlow app, and I'm using Acrobat Pro to merge three files into one (about 300MB), which I needed to submit to Amazon's CreateSpace, but they needed the trim size of 6'' by 9'', while the pdf is 21.4''' by 26.8'', and I can't amend the source file size of the InkFlow files.
    I tried fiddling with the Crop and Print options, but I'm unable to resize the pdf to match the trim size. The problem on my side isn't so much on printing but to resize the pdf file page to 6'' by 9'' to satisfy Amazon's requirement for their trim size. Could this be done with Acrobat Pro? The message that appears is: Full print options of Acrobat Pro, set all print options from the Application print dialog box. Thank for the help.

  • How can I resize and reposition the Date Created window for All windows?

    When setting view options for a finder window set to list view, I cannot get column resizing or positioning to hold. When making those changes, the option for "All Windows" automatically shifts to "This window only" and it then does not hold.
    How can I resize and reposition the Date Created column for All WIndows?

    Hi Harmz,
    Try dragging this file to the Îesktop & reboot...
    /Users/YourUserName/Library/Preferences/com.apple.finder.plist

Maybe you are looking for

  • I have an Iphone5 .. thought reported asked for help to find to find my iphone.  this deactived my phone.  how do i get the deactivate 'beacon' turned off?

    I have an Iphone 5.  I thouht I'd lost I phone in an Wal-Mart.   A Wal-Mart clerk was trying to be helpful and got her Iphone and reported it lost and deactivated. Eventually, I found the phone.  It was completely cleared and the phone had a message

  • XI 3.0 versus NWA (CEN system)

    Hi, in our company, we have up to 30 productive systems. From Releases 46c to ERP 7.0 (BW, CRM ...)! These SAP systems are all connected (agents) with CEN, the central monitoring system (NW04, double stack, SPS 16)! All is fine and working well! Now

  • MSI N480GTX w/AMD CPU

    got one of these cards but didnt think there might be a compatibility problem using it with an AMD Athlon II X4 2.6ghz CPU/MB. I can't remember ever having to consider what my cpu was before i bought a video card. Is this true? I must use an Intel i7

  • The best quality for ripping a song

    hi, i have some old cds that i would like to rip and upload to my ipod touch. but what's the rate for ripping songs from cds that would give me the best quality of the songs. thanks. ps. now i use 192 bps, but sound quality doesn't seem too good.

  • Populating users and groups - design considerations/best practice

    We are currently running a 4.5 Portal in production. We are doing requirements/design for the 5.0 upgrade. We currently have a stored procedure that assigns users to the appropriate groups based on the domain info and role info from an ERP database a