JTabbedPane Tab unselected does not fill JPanel while selected does












1














I have custom JTabbedPane, I am having issue with making the tabs the same size as each other.



enter image description here



as you can see in the image, The green tab is selected, while the Red is unselected, I would like the Red Tab (Unselected) to be the same size as the Green Tab (Selected) here is my code



here is the code.



import javax.swing.*;
import javax.swing.plaf.basic.BasicTabbedPaneUI;
import java.awt.*;

public class UITest {

public static void main(String args){
JFrame jFrame = new JFrame();
JTabbedPane jTabbedPane = new JTabbedPane();
jTabbedPane.add(new JPanel(), "test");
jTabbedPane.add(new JPanel(), "test2");
jTabbedPane.setUI(new LynxTabbedPane());
jFrame.setContentPane(jTabbedPane);
jFrame.setSize(200,200);
jFrame.setVisible(true);
}

public static class LynxTabbedPane extends BasicTabbedPaneUI {
private Polygon shape;

@Override
protected void paintTabBackground(Graphics g, int tabPlacement, int tabIndex, int x, int y, int w, int h, boolean isSelected) {
Graphics2D g2D = (Graphics2D) g;
int xp = new int{x, x, x + w, x + w, x};
int yp = new int{y, y + h, y + h, y, y};
shape = new Polygon(xp, yp, xp.length);
if (isSelected) {
g2D.setColor(Color.GREEN);
} else if (tabPane.isEnabled() && tabPane.isEnabledAt(tabIndex)) {
g2D.setColor(Color.RED);
}
g2D.fill(shape);
}
}
}









share|improve this question
























  • @AndrewThompson Sorry, I am still getting used to asking questions, Can you tell me what part of my example was incorrect? :D
    – SamzSakerz
    Nov 17 '18 at 0:10










  • I have edited my question, Is that good?
    – SamzSakerz
    Nov 17 '18 at 0:19










  • Yeah I have no clue what you mean, You are not telling me the exact issue with my question.
    – SamzSakerz
    Nov 17 '18 at 1:33






  • 1




    Yes, Now it makes sense to me.
    – SamzSakerz
    Nov 17 '18 at 1:49






  • 1




    Edited my question, You can now just copy and paste it
    – SamzSakerz
    Nov 17 '18 at 1:54
















1














I have custom JTabbedPane, I am having issue with making the tabs the same size as each other.



enter image description here



as you can see in the image, The green tab is selected, while the Red is unselected, I would like the Red Tab (Unselected) to be the same size as the Green Tab (Selected) here is my code



here is the code.



import javax.swing.*;
import javax.swing.plaf.basic.BasicTabbedPaneUI;
import java.awt.*;

public class UITest {

public static void main(String args){
JFrame jFrame = new JFrame();
JTabbedPane jTabbedPane = new JTabbedPane();
jTabbedPane.add(new JPanel(), "test");
jTabbedPane.add(new JPanel(), "test2");
jTabbedPane.setUI(new LynxTabbedPane());
jFrame.setContentPane(jTabbedPane);
jFrame.setSize(200,200);
jFrame.setVisible(true);
}

public static class LynxTabbedPane extends BasicTabbedPaneUI {
private Polygon shape;

@Override
protected void paintTabBackground(Graphics g, int tabPlacement, int tabIndex, int x, int y, int w, int h, boolean isSelected) {
Graphics2D g2D = (Graphics2D) g;
int xp = new int{x, x, x + w, x + w, x};
int yp = new int{y, y + h, y + h, y, y};
shape = new Polygon(xp, yp, xp.length);
if (isSelected) {
g2D.setColor(Color.GREEN);
} else if (tabPane.isEnabled() && tabPane.isEnabledAt(tabIndex)) {
g2D.setColor(Color.RED);
}
g2D.fill(shape);
}
}
}









share|improve this question
























  • @AndrewThompson Sorry, I am still getting used to asking questions, Can you tell me what part of my example was incorrect? :D
    – SamzSakerz
    Nov 17 '18 at 0:10










  • I have edited my question, Is that good?
    – SamzSakerz
    Nov 17 '18 at 0:19










  • Yeah I have no clue what you mean, You are not telling me the exact issue with my question.
    – SamzSakerz
    Nov 17 '18 at 1:33






  • 1




    Yes, Now it makes sense to me.
    – SamzSakerz
    Nov 17 '18 at 1:49






  • 1




    Edited my question, You can now just copy and paste it
    – SamzSakerz
    Nov 17 '18 at 1:54














1












1








1







I have custom JTabbedPane, I am having issue with making the tabs the same size as each other.



enter image description here



as you can see in the image, The green tab is selected, while the Red is unselected, I would like the Red Tab (Unselected) to be the same size as the Green Tab (Selected) here is my code



here is the code.



import javax.swing.*;
import javax.swing.plaf.basic.BasicTabbedPaneUI;
import java.awt.*;

public class UITest {

public static void main(String args){
JFrame jFrame = new JFrame();
JTabbedPane jTabbedPane = new JTabbedPane();
jTabbedPane.add(new JPanel(), "test");
jTabbedPane.add(new JPanel(), "test2");
jTabbedPane.setUI(new LynxTabbedPane());
jFrame.setContentPane(jTabbedPane);
jFrame.setSize(200,200);
jFrame.setVisible(true);
}

public static class LynxTabbedPane extends BasicTabbedPaneUI {
private Polygon shape;

@Override
protected void paintTabBackground(Graphics g, int tabPlacement, int tabIndex, int x, int y, int w, int h, boolean isSelected) {
Graphics2D g2D = (Graphics2D) g;
int xp = new int{x, x, x + w, x + w, x};
int yp = new int{y, y + h, y + h, y, y};
shape = new Polygon(xp, yp, xp.length);
if (isSelected) {
g2D.setColor(Color.GREEN);
} else if (tabPane.isEnabled() && tabPane.isEnabledAt(tabIndex)) {
g2D.setColor(Color.RED);
}
g2D.fill(shape);
}
}
}









share|improve this question















I have custom JTabbedPane, I am having issue with making the tabs the same size as each other.



enter image description here



as you can see in the image, The green tab is selected, while the Red is unselected, I would like the Red Tab (Unselected) to be the same size as the Green Tab (Selected) here is my code



here is the code.



import javax.swing.*;
import javax.swing.plaf.basic.BasicTabbedPaneUI;
import java.awt.*;

public class UITest {

public static void main(String args){
JFrame jFrame = new JFrame();
JTabbedPane jTabbedPane = new JTabbedPane();
jTabbedPane.add(new JPanel(), "test");
jTabbedPane.add(new JPanel(), "test2");
jTabbedPane.setUI(new LynxTabbedPane());
jFrame.setContentPane(jTabbedPane);
jFrame.setSize(200,200);
jFrame.setVisible(true);
}

public static class LynxTabbedPane extends BasicTabbedPaneUI {
private Polygon shape;

@Override
protected void paintTabBackground(Graphics g, int tabPlacement, int tabIndex, int x, int y, int w, int h, boolean isSelected) {
Graphics2D g2D = (Graphics2D) g;
int xp = new int{x, x, x + w, x + w, x};
int yp = new int{y, y + h, y + h, y, y};
shape = new Polygon(xp, yp, xp.length);
if (isSelected) {
g2D.setColor(Color.GREEN);
} else if (tabPane.isEnabled() && tabPane.isEnabledAt(tabIndex)) {
g2D.setColor(Color.RED);
}
g2D.fill(shape);
}
}
}






java swing jtabbedpane






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 17 '18 at 1:55









Andrew Thompson

153k27163338




153k27163338










asked Nov 17 '18 at 0:00









SamzSakerz

1,7611526




1,7611526












  • @AndrewThompson Sorry, I am still getting used to asking questions, Can you tell me what part of my example was incorrect? :D
    – SamzSakerz
    Nov 17 '18 at 0:10










  • I have edited my question, Is that good?
    – SamzSakerz
    Nov 17 '18 at 0:19










  • Yeah I have no clue what you mean, You are not telling me the exact issue with my question.
    – SamzSakerz
    Nov 17 '18 at 1:33






  • 1




    Yes, Now it makes sense to me.
    – SamzSakerz
    Nov 17 '18 at 1:49






  • 1




    Edited my question, You can now just copy and paste it
    – SamzSakerz
    Nov 17 '18 at 1:54


















  • @AndrewThompson Sorry, I am still getting used to asking questions, Can you tell me what part of my example was incorrect? :D
    – SamzSakerz
    Nov 17 '18 at 0:10










  • I have edited my question, Is that good?
    – SamzSakerz
    Nov 17 '18 at 0:19










  • Yeah I have no clue what you mean, You are not telling me the exact issue with my question.
    – SamzSakerz
    Nov 17 '18 at 1:33






  • 1




    Yes, Now it makes sense to me.
    – SamzSakerz
    Nov 17 '18 at 1:49






  • 1




    Edited my question, You can now just copy and paste it
    – SamzSakerz
    Nov 17 '18 at 1:54
















@AndrewThompson Sorry, I am still getting used to asking questions, Can you tell me what part of my example was incorrect? :D
– SamzSakerz
Nov 17 '18 at 0:10




@AndrewThompson Sorry, I am still getting used to asking questions, Can you tell me what part of my example was incorrect? :D
– SamzSakerz
Nov 17 '18 at 0:10












I have edited my question, Is that good?
– SamzSakerz
Nov 17 '18 at 0:19




I have edited my question, Is that good?
– SamzSakerz
Nov 17 '18 at 0:19












Yeah I have no clue what you mean, You are not telling me the exact issue with my question.
– SamzSakerz
Nov 17 '18 at 1:33




Yeah I have no clue what you mean, You are not telling me the exact issue with my question.
– SamzSakerz
Nov 17 '18 at 1:33




1




1




Yes, Now it makes sense to me.
– SamzSakerz
Nov 17 '18 at 1:49




Yes, Now it makes sense to me.
– SamzSakerz
Nov 17 '18 at 1:49




1




1




Edited my question, You can now just copy and paste it
– SamzSakerz
Nov 17 '18 at 1:54




Edited my question, You can now just copy and paste it
– SamzSakerz
Nov 17 '18 at 1:54












1 Answer
1






active

oldest

votes


















0














I have fixed the issue by moving g2D.fill(shape); inside isSelected



@Override
protected void paintTabBackground(Graphics g, int tabPlacement, int tabIndex, int x, int y, int w, int h, boolean isSelected) {
Graphics2D g2D = (Graphics2D) g;
int xp = new int{x, x, x + w, x + w, x};
int yp = new int{y, y + h, y + h, y, y};

shape = new Polygon(xp, yp, xp.length);
if (isSelected) {
g2D.fill(shape);
g2D.setColor(selectColor);
} else if (tabPane.isEnabled() && tabPane.isEnabledAt(tabIndex)) {
g2D.setColor(deSelectColor);
}
}


This will only fill it with the shape if the tab is selected.



Result:



enter image description here






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53346917%2fjtabbedpane-tab-unselected-does-not-fill-jpanel-while-selected-does%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I have fixed the issue by moving g2D.fill(shape); inside isSelected



    @Override
    protected void paintTabBackground(Graphics g, int tabPlacement, int tabIndex, int x, int y, int w, int h, boolean isSelected) {
    Graphics2D g2D = (Graphics2D) g;
    int xp = new int{x, x, x + w, x + w, x};
    int yp = new int{y, y + h, y + h, y, y};

    shape = new Polygon(xp, yp, xp.length);
    if (isSelected) {
    g2D.fill(shape);
    g2D.setColor(selectColor);
    } else if (tabPane.isEnabled() && tabPane.isEnabledAt(tabIndex)) {
    g2D.setColor(deSelectColor);
    }
    }


    This will only fill it with the shape if the tab is selected.



    Result:



    enter image description here






    share|improve this answer


























      0














      I have fixed the issue by moving g2D.fill(shape); inside isSelected



      @Override
      protected void paintTabBackground(Graphics g, int tabPlacement, int tabIndex, int x, int y, int w, int h, boolean isSelected) {
      Graphics2D g2D = (Graphics2D) g;
      int xp = new int{x, x, x + w, x + w, x};
      int yp = new int{y, y + h, y + h, y, y};

      shape = new Polygon(xp, yp, xp.length);
      if (isSelected) {
      g2D.fill(shape);
      g2D.setColor(selectColor);
      } else if (tabPane.isEnabled() && tabPane.isEnabledAt(tabIndex)) {
      g2D.setColor(deSelectColor);
      }
      }


      This will only fill it with the shape if the tab is selected.



      Result:



      enter image description here






      share|improve this answer
























        0












        0








        0






        I have fixed the issue by moving g2D.fill(shape); inside isSelected



        @Override
        protected void paintTabBackground(Graphics g, int tabPlacement, int tabIndex, int x, int y, int w, int h, boolean isSelected) {
        Graphics2D g2D = (Graphics2D) g;
        int xp = new int{x, x, x + w, x + w, x};
        int yp = new int{y, y + h, y + h, y, y};

        shape = new Polygon(xp, yp, xp.length);
        if (isSelected) {
        g2D.fill(shape);
        g2D.setColor(selectColor);
        } else if (tabPane.isEnabled() && tabPane.isEnabledAt(tabIndex)) {
        g2D.setColor(deSelectColor);
        }
        }


        This will only fill it with the shape if the tab is selected.



        Result:



        enter image description here






        share|improve this answer












        I have fixed the issue by moving g2D.fill(shape); inside isSelected



        @Override
        protected void paintTabBackground(Graphics g, int tabPlacement, int tabIndex, int x, int y, int w, int h, boolean isSelected) {
        Graphics2D g2D = (Graphics2D) g;
        int xp = new int{x, x, x + w, x + w, x};
        int yp = new int{y, y + h, y + h, y, y};

        shape = new Polygon(xp, yp, xp.length);
        if (isSelected) {
        g2D.fill(shape);
        g2D.setColor(selectColor);
        } else if (tabPane.isEnabled() && tabPane.isEnabledAt(tabIndex)) {
        g2D.setColor(deSelectColor);
        }
        }


        This will only fill it with the shape if the tab is selected.



        Result:



        enter image description here







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 17 '18 at 3:32









        SamzSakerz

        1,7611526




        1,7611526






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53346917%2fjtabbedpane-tab-unselected-does-not-fill-jpanel-while-selected-does%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

            ComboBox Display Member on multiple fields

            Is it possible to collect Nectar points via Trainline?