Jump to content

GridBagLayout... need some kind of align top I guess

- - - - -

  • Please log in to reply
3 replies to this topic

#1
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Hey, I'm making an app where I use an uneditable JTextPane, and now I want to have line numbers next to it (see http://forum.codecal...-your-data.html for more info if you wanna know what I'm making)

It works fine if I add enough line numbers. Where with "enough" I mean, more than the height can handle.
Added a red border around the lineNumberPanel:
Attached File  Tour de java.png   75.94K   40 downloads

But I don't want to have more line numbers than needed. Partially because it creates an unneeded scrollbar, but also because it's prettier if there are as many line numbers as there are lines.

But when I do that, then there's a vertical-align = center going on...

(I think i'm "stuck" with a gridbaglayout, because my 1st line number needs to be a bit lower than the top, I as well need 1 extra pixel to match the correct height at every line item (or increase font size by 1, which I don't want to do))
Vertical align img:
Attached File  Tour de java_exact_lines.png   73.45K   56 downloads

Anyone has an idea to make it align to the top?

Line number code:

        GridBagConstraints c = new GridBagConstraints();

        c.gridx = 0;


        for(int i=0 ; i<lines ; i++){

            c.gridy = i;

            if(i ==0){

                c.insets = new Insets(11,0,0,0);

            } else {

                c.insets = new Insets(1,0,0,0);

            }


            JLabel number = new JLabel((i + 1) + "");

            number.setFont(mainFont);

            lineNumbers.add(number, c);

        }


Oh, if it matters, the jpanel with gridbaglayout(line number panel) is inside a BorderLayout jpanel at the WEST.

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
:glare: Why do I always find it myself within an hour of posting the question.
Solution: Before adding the lineNumbers panel to the west of the borderlayout panel, add it in a flowlayout panel. (I could've sworn I tried this before... guess not :bad:)

        JPanel flowPanel = new JPanel();

        flowPanel.add(lineNumbers);

        codeAndLinePanel.add(flowPanel, BorderLayout.WEST);



#3
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP

Quote

Why do I always find it myself within an hour of posting the question.
Doh! :)

#4
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
I can't tell you how many questions I've resolved by simply formulating the question and making sure I'm not sounding like an idiot. XD
Wow I changed my sig!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users