0

I am trying to align the position of text within a JTextArea and a JButton, but with everything I tried, either nothing happens, or the alignment is still slightly off.

Here is what is looks like: enter image description here (You can see with the highlighted option that the JButton (center) is slightly lower than the two JTextAreas on either side.)

Here is some code:

                categoryFile[i][j] = tempButton;
                categoryFile[i][j].setBackground(Color.white);
                categoryFile[i][j].setForeground(Color.black);
                categoryFile[i][j].setOpaque(true);
                categoryFile[i][j].setFocusable(false);
                categoryFile[i][j].setBorderPainted(false);;
                categoryFile[i][j].setVerticalAlignment(SwingConstants.TOP);
                categoryFile[i][j].setPreferredSize(new Dimension(500,10));
                categoryFile[i][j].addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        openPDFWithOptions(filePath,fileName);
                    }
                });

                JPanel listRow = new JPanel();
                listRow.setBackground(Color.white);
                listRow.setLayout(new BorderLayout());
                listRow.setPreferredSize(new Dimension(800, 40));

                JTextArea category = new JTextArea(fileElements[0]); 
                category.setEditable(false);
                JTextArea parent = new JTextArea(fileElements[1]);
                parent.setEditable(false);

                listRow.add(parent,BorderLayout.WEST);
                listRow.add(categoryFile[i][j],BorderLayout.CENTER);
                listRow.add(category,BorderLayout.EAST);

                categoryLists[i].add(listRow,c);

Right now I am using categoryFile[i][j].setVerticalAlignment(SwingConstants.TOP) to change the position of the JButton, which ALMOST works. I've also tried changing the vertical alignment of the JTextAreas, but nothing changed.

How can I align the text within these components?

StarSweeper
  • 397
  • 2
  • 4
  • 28
  • It can be quite difficult to align the text of different components when you use a different font in the different components. Also, why are you using a JTextArea? Do you anticipate that component to have multiple lines of text? If not, you should be using a JTextField instead. – FredK Jan 31 '17 at 21:22
  • Yeah, all the different ways to display text confuse me, so I just picked one. I also didn't notice the different fonts, thank you. – StarSweeper Jan 31 '17 at 21:24

1 Answers1

1

Quickest way to fix this would probably be to just add some padding on the 1st and third columns to set all the text to the same height. See Jpanel Padding

Community
  • 1
  • 1
odin
  • 392
  • 1
  • 3
  • 11