The JTable when JPanel as TableCellRenderer using custom text alignment issues

Hello

I have some difficulty with the text alignment/border issues when you use a custom TableCellRenderer. I use a JPanel with GroupLayout (although I also tried others like FlowLayout), and I can't get the text of the label in the JPanel to align with other cells in the table. The text within my 'panel' cell is moved down. If I use the code of the DefaultTableCellRenderer allows you to set the border when the cell receives focus, the problem gets worse as the text moves when the new border is applied to the Group of experts on the selection of the cell. Here's a NBS to show:
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import javax.swing.GroupLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.border.Border;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import sun.swing.DefaultLookup;

public class TableCellPanelTest extends JFrame {

  private class PanelRenderer extends JPanel implements TableCellRenderer {

    private JLabel label = new JLabel();

    public PanelRenderer() {
      GroupLayout layout = new GroupLayout(this);
      layout.setHorizontalGroup(layout.createParallelGroup().addComponent(label));
      layout.setVerticalGroup(layout.createParallelGroup().addComponent(label));
      setLayout(layout);
    }

    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {

      if (isSelected) {
        setBackground(table.getSelectionBackground());
      } else {
        setBackground(table.getBackground());
      }
      
      // Border section taken from DefaultTableCellRenderer
      if (hasFocus) {
        Border border = null;
        if (isSelected) {
          border = DefaultLookup.getBorder(this, ui, "Table.focusSelectedCellHighlightBorder");
        }
        if (border == null) {
          border = DefaultLookup.getBorder(this, ui, "Table.focusCellHighlightBorder");
        }
        setBorder(border);

        if (!isSelected && table.isCellEditable(row, column)) {
          Color col;
          col = DefaultLookup.getColor(this, ui, "Table.focusCellForeground");
          if (col != null) {
            super.setForeground(col);
          }
          col = DefaultLookup.getColor(this, ui, "Table.focusCellBackground");
          if (col != null) {
            super.setBackground(col);
          }
        }
      } else {
        setBorder(null /*getNoFocusBorder()*/);
      }

      // Set up our label
      label.setText(value.toString());
      label.setFont(table.getFont());

      return this;
    }
  }

  public TableCellPanelTest() {
    JTable table = new JTable(new Integer[][]{{1, 2, 3}, {4, 5, 6}}, new String[]{"A", "B", "C"});

    // set up a custom renderer on the first column
    TableColumn firstColumn = table.getColumnModel().getColumn(0);
    firstColumn.setCellRenderer(new PanelRenderer());

    getContentPane().add(table);
    pack();
  }

  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {

      public void run() {
        new TableCellPanelTest().setVisible(true);
      }
    });
  }
}
There are essentially two issues:

(1) when the first round, the text in the custom converter column is shifted down a little.
(2) once a column cell is selected, it moves down still further.

I'd appreciate any help to understand what is happening!

Thank you!

(1) LayoutManagers need to take into account the border if the label is placed at (1,1) while the labels just start at (0,0) of the cell Rect. The layout manager also tend not not to reduce the component less than their minimum size. Defining minimum size labels to (0,0) seems to have the same effect in your example. Do the same for the maximum size helps if you set the height of the largest JTable line. Maybe easier to use BorderLayout, which ignores min/max for the Center (and a height min/max for East/West, etc.).
(2) DefaultTableCellRenderer uses a 1px border if the UI update borderless is null, you do not have.
(3) include a setDefaultCloseOperation is a NBS please. I think that I have a test of hundreds of current programs: P.

Tags: Java

Similar Questions

  • Is there a need or a way to turn the pencil off the power when you do not use?

    Is there a need or a way to turn the pencil off the power when you do not use?

    Unfortunately not. The best thing is to turn off bluetooth. However, you will need to re - pair the iPad and the pencil again by plugging in the port of lightning. When not used the pencil of apple do not lose a lot of power.

  • Im running ADOBE CC 2015 on an iMac and Illustrator plant all the time when I try to use the save for web. I need to use this feature, all day and am frustrated of all accidents. Help, please.

    Im running ADOBE CC 2015 on an iMac and Illustrator plant all the time when I try to use the save for web. I need to use this feature, all day and am frustrated of all accidents. Help, please.

    Hello

    We have published a 19.0.1 hotfix that resolves this problem in discussion on this topic. Please apply the patch to your creative cloud application. The creative cloud application checks the update once a day, so you may see the update immediately.  If you want to force the check for an update at the beginning please logout and log back into the creative Cloud application.

    Link to 19.0.1 release notes: https://helpx.adobe.com/illustrator/kb/illustrator-cc-2015-crash-bug-fixes-19-0-1.html

    If you continue to see any questions send us an e-mail to [email protected]

    Kind regards

    Ashutosh

    The name of the Illustrator team engineering

  • "Internet Explorer cannot display the webpage" when you try to use the CAC reader

    When I try to open a session on the site of the army AKO with my CAC reader and card chip that I allowed to choose my credientials.  When I hit enter I get "Internet Explorer cannot display the webpage".  My internet provider is Mediacom.  I can log on with the same drive ACC when I use a Web service provided by a hotel close to my arsenal.  Where is the conflict?  Mediacom IT did not.

    Hi TommyZB,

    Welcome to the Microsoft answers site.

    ·         What is the version of Internet Explorer installed on the computer?

    ·         What is the operating system installed on the computer?

    ·         Is this the same computer \laptop that you use to connect to the site?

    ·         The problem is specific to this Web site or with other sites as well?

    You can try to add this site to the trusted sites and check.

    1. open Internet Explorer.

    2. click on tools.

    3. click on Internet Options.

    4. click on the Security tab.

    5. click on Trusted Sites

    6. click on Sites.

    7. Add Web site.

    8. click on OK.

    Now, check if you are able to connect to the site.

    This could be a problem with the settings of Internet Explorer on the computer; You can try to optimize internet Explorer settings and check, follow the procedure described in the link below:

    How to optimize Internet Explorer: http://support.microsoft.com/kb/936213

    Try disabling the security software and firewall temporarily on the computer and check. Make sure that enable the security software on the computer.

    Understanding Windows Firewall settings: http://windows.microsoft.com/en-US/windows7/Understanding-Windows-Firewall-settings#section_4

    Disable the anti-virus software: http://windows.microsoft.com/en-US/windows7/Disable-antivirus-software

    Swathi B - Microsoft technical support.
    Visit our Microsoft answers feedback Forum and let us know what you think.

  • question about the battery of the laptop when you do not use

    Hello

    I see that if you take the battery laptop out when you are not using and you're wotking with adapter suply, it wil extend the life of the battery.

    But what if I do not use this battery for several days and I put it away wil he unload his omnipotence?

    Thank you

    Johan

    Hi Johan,.

    I don't think that it helps extend the life of the battery if remove you it from the laptop. It will also pay power when you are not using it.  For more information on the functioning of the battery, I would say that you see the battery user guide provided by the manufacturer of your laptop.

    Hope this information helps. If you have any other questions, we will be happy to help you.

  • Saving ink by powering off the power when you are not using it?

    I have a HP Officjet 6700 running under Windows 7 64-bit and read that turning off your printer when you don't use to save on ink. Is this true?

    Hello

    Right, but turn off will save a bit of power. Inkjet printers need ink more when you turn very often.

    I hope this helps.

  • Disable the button Delete in the toolbar on EBS (Apps) Oracle using Custom pll.

    Hello

    I am trying to disable the button remove on Oracle Apps, I tried the following code, but it does not work. If the a person who has been able to do it please share.

    I tried this.
    begin
           
            if (event_name = 'WHEN-NEW-FORM-INSTANCE') then
                      app_special.enable('EDIT.DELETE', PROPERTY_OFF);
                       fnd_message.set_string('After APP_SPECIAL.ENABLE');
              
            end if;
      end;
    Thank you

    Hello

    Take a look at the following links.

    Custom.PLL
    Custom.PLL

    How can note: 561676.1 - I use SQL to disable a menu item?
    https://metalink2.Oracle.com/MetaLink/PLSQL/ml2_documents.showDocument?p_database_id=not&P_ID=561676.1

    Kind regards
    Hussein

  • Warning it can influence on the computer when you try to use the Flash Player

    Original title: Flash Drive case

    I just started using the external drive and flash drive to increase the PC space.  I tried to remove some files of music from the flash drive. Got a warning box that if I continue I can have an impact on the computer or the software. If I continue. If I'm stuck with a flash drive with unusable area?

    Hi Russ Alan,

    You will get a message to Scan Flash and continue when the Flash drive is infected.

    When you get the message scanner the Flash disk, you can try to scan and continue.

    You can also run a scan on your computer.

    Check out the link:

    http://OneCare.live.com/site/en-us/default.htm?s_cid=SAH

    Hope this information is useful.

    Jeremy K
    Microsoft Answers Support Engineer
    Visit our Microsoft answers feedback Forum and let us know what you think

    If this post can help solve your problem, please click the 'Mark as answer' or 'Useful' at the top of this message. Marking a post as answer, or relatively useful, you help others find the answer more quickly.

  • Navigate in the photo when zoomed in and using the mask tool

    Hey guys, I tried to figure out how to move photo when I'm zooming in and using the mask and I can not understand. It's a pain to have clear the Brush tool can pan around, especially when I am trying to add a mask. If someone could help, it would be great. Thank you!

    Space bar.

  • EU from the mistakes when creating homepage by using site studio-

    I'm creating a home page of my Web site using studio site to the oracle content server, but when I enter the required information, I get the following alert message

    "Happy ID 'HP_Xalco03' has invalid values of content info. Value not found in list of options for the types of field web site object.


    1. I created a Web site name "Xalco city".
    2. I have a section and below has 4 subsections.
    3. now, when I right click on the root and try to create a home page receives the question.

    Can someone help me with what the real prob. I want to know if I missed any configuration or my not are wrong.

    I am using emergency. Thank you

    Thank you
    Abhijit

    [email protected]

    Send me the tutorial doc. Will try in my CA.

    Kind regards
    Deepak

  • How can I fix the problem when another computer is using the same IP address with me?

    My computer has proven to me that there is another computer using my IP address.

    He said I need to contact the administrator.
    I don't know how to do in this regard. Help, please.

    Since it's your own home network, it means you are the network administrator. Which basically means that you should have total control on who can and can not connect to internet through this network.

    Then, you should have a router somewhere in your House. This is the device that sends the wireless signal. Most likely, it will be connected to a phone jack or cable from the wall, or a modem, depending on which company you get your internet service.

    You need to find this device, the router and restart for it. Do the same on your computer. Once both have restarted, try to connect to the internet. The router must have an ID written somewhere on it. This same ID should appear in your network and sharing Center , if you click on "Connect To A Network.

    The homegroup is something a little different. A homegroup is really just a collection of your own computers, or in your home. For example, if you had a desktop and a laptop, and I wanted to share the files from one to the other, you should join them in the same homegroup. It has nothing to do with putting on the Internet, just the connection of two computers together. It of good that you set up a homegroup with a password, but won't really help you here.

  • Why my index.html file is not changing on the server when I download it using the option at the level of the site to synchronize.

    DW CS6 using, Windows and sometimes try DW CC. I say just put the files on the server.   All, I repeat all, times, I download, the file index.html and sometimes other files, does not update.  I use iPage as my host, but others have tried with the same results.  If I go to iPage file manager and look at the file on the server, this is the old version, before the updates to the file.  Physically, I delete the files from any problem and put the file on the server individually.  Because the way it happened with different hosts, I'm on #4, I think it's a problem of DW.  Any ideas?

    As a un travail work around download, tell him rather than the button synchronize and see if the results are different...

    1 right-click the root directory of local site in the files window

    2. choose the selection command > recently changed

    3. set ' files created or modified in the last: ' for 1 day

    Which will highlight everything that has received a change in any of the site for the last day. Then, you click on the button Put standard to place these files on the server.

    If it doesn't, check your FTP logs (access the Site > advanced > FTP Log) and see if there is anything that is mentioned on the index page.

  • How can I stop automatically going into the tab when I right click on a text of google search?

    before the update every time that I would be a highlighted text and right click to perform a google search would not automatically to it. However, he does now and getting on my nerves. How can we change it?

    Hey kevindigg, you can enter Subject: config in the address bar of firefox, confirm notification of information (where it appears), search for the preference named browser.search.context.loadInBackground & switch true by double-clicking it. This should bring back the old behavior...

  • How to define the format of a field drop-down list text align at the top of the field instead of the Center?

    I have a form with the fields of data entry simple Acrobat 11. I have, for the first time inserted drop-down lists and most of them is single-line fields. However, there are a few examples where my field must be larger to mask unwanted background. (The field will define the colur to white background are data entry, but I did not write the script for this.) When a selection is made, it appears centered in the field and I need to be aligned to the top.

    I guess I can put a script on the Format tab, in the custom format script, but I don't know enough about Javascript to run on the syntax. I guess it would be a simple line or two of code. Can anyone help?

    You have no control over this. They are always vertically centered. You can use a separate field in the drop down menu, however, and control its background. I suggest to use a read-only button.

  • BCC works not when using custom property accessors

    Hello

    In the integration of the ATG-short, I try to use "custom property accessors" in my "product-sku-output - config.xml". I'm including the sub modules in the classpath of build.xml.

    < dir = "${dynamo.home}" / fileset... / DAF/Research/Base/lib ' > "

    < name = "" * / *.jar "/ >"

    < / fileset >

    < dir = "${dynamo.home}" / fileset... / DAF/Search/Index/lib ' > "

    < name = "" * / *.jar "/ >"

    < / fileset >

    When I Isaiah to compile the project, these modules have been included in my atg_bootstrap.war of the file .ear as '/atg_bootstrap.war/WEB-INF/ATG-INF/DAF/Search/Index/'. However, when I start the server, in the instance server log merchandising get the below error and for this reason the server has been stopped and cannot access the BCC.

    I try to use the custom property accessor class - PropertyAccessorImpl

    2013-07-03 11:04:46, 418 INFO [STDOUT] Configuration directory (principal) or the /var/jboss/versions/jboss-eap-5.1/jboss-as/server/merch/deploy/ulta.ear/atg_bootstrap.war/WEB-INF/ATG-INF/DAF/Search/Index/configlayers/stagingandprod/config.jar file does not exist or is not readable

    2013-07-03 11:04:46, 903 ERROR [STDERR] java.util.zip.ZipException (principal): problem opening /var/jboss/versions/jboss-eap-5.1/jboss-as/server/merch/deploy/ulta.ear/atg_bootstrap.war/WEB-INF/ATG-INF/DAF/Search/Index/configlayers/stagingandprod/config.jar zip file: error in opening zip file

    2013-07-03 11:04:46, 904 ERROR [STDERR] (principal) at atg.vfs.zip.ZipFileSystem. < init > (ZipFileSystem.java:124)

    2013-07-03 11:04:46, 904 ERROR [STDERR] (principal) at atg.vfs.zip.ZipFileSystem. < init > (ZipFileSystem.java:97)

    2013-07-03 11:04:46, 904 ERROR [STDERR] (principal) at atg.vfs.zip.ZipFileSystem. < init > (ZipFileSystem.java:142)

    2013-07-03 11:04:46, 904 ERROR [STDERR] (principal) at atg.nucleus.ConfigurationFileSystems. < init > (ConfigurationFileSystems.java:124)

    2013-07-03 11:04:46, 904 ERROR [STDERR] (principal) at atg.nucleus.ConfigurationFileSystems. < init > (ConfigurationFileSystems.java:244)

    2013-07-03 11:04:46, 905 ERROR [STDERR] (principal) at atg.nucleus.Nucleus.createConfigFileSystems(Nucleus.java:1827)

    2013-07-03 11:04:46, 905 ERROR [STDERR] (principal) at atg.nucleus.Nucleus. < init > (Nucleus.java:842)

    2013-07-03 11:04:46, 905 ERROR [STDERR] (principal) at atg.nucleus.Nucleus. < init > (Nucleus.java:766)

    2013-07-03 11:04:46, 905 ERROR [STDERR] (principal) at atg.nucleus.Nucleus. < init > (Nucleus.java:747)

    Please help me to solve this error.

    Thank you

    There is typo in the ATG installation that creates this problem.

    For example, change the file C:\\DAF\Search\Index\META-INF\MANIFEST. MF

    ATG-StagingConfig-path: configlayers/stagingandprod/config.jar

    Peace

    Shaik

Maybe you are looking for

  • Satellite A100-369 - BIOS password was needed all of a sudden

    Hello world!I have a Satellite A100-369 psaare04m00dit. After a few tries to install windows 7, (sth has not worked since the restoration of DVD Toshiba) bios password was necessary. In fact, no password has been before and... have no idea what would

  • Error code: 80070002 (cannot install updates)

    How the error code 80070002 can be cleared for the installation of the windows updates?

  • I can't open a file THM ext XP

    I imported the photos from my camera and I can't see some of them because they have a THM ext file name.

  • Could not import the blackberry project

    Hello I downloaded a blackberry project open source from github. Link: https://github.com/williamrjribeiro/wallpaper-ruler-bb10 I imported my momentics IDE, but can't compile or build as he founded errors. The error is: Active entry point is required

  • Photo Story 3 install and run on Windows 7?

    Welcome to Windows 7 my party on Thursday will be be moistened with a little to have to say goodbye to Photo Story 3. I activated my Windows 7 laptop last night and tried again the download/installation of the PS3. This time it let me download, but w