0

I want to create two non-editable textboxes (each will contain only one line) with a fixed size, but I want them to be scrollable (horizontally only) because I know the text they will contain will be very long. I want them to be below the two buttons I define below, and I want each textbox on their own row.

Problem is, everything shows up and buttons work as expected, but the textbox won't scroll, although I can somehow drag and select the rest of the text in the box that isn't visible. I don't know if labels are scrollable, would they be a better option?

Code:

public static void main(String[] args)
{
    JFrame win = new JFrame("Window");
    win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    win.setSize(400, 300);

    GridBagConstraints c = new GridBagConstraints();
    win.setLayout( new GridBagLayout() );

    JTextArea master = new JTextArea(1,1);
    JTextArea vendor = new JTextArea(1,1);
    master.setEditable(false);
    vendor.setEditable(false);
    master.setPreferredSize( new Dimension(100,20) );
    vendor.setPreferredSize( new Dimension(100,20) );

    master.setText(/*some really long string*/);
    vendor.setText(/*some really long string*/);

    JScrollPane mPane = new JScrollPane(master, JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    JScrollPane vPane = new JScrollPane(vendor, JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); 

    mPane.getHorizontalScrollBar().isVisible();
    vPane.getHorizontalScrollBar().isVisible();

    JButton one = new JButton("Select");
    ActionListener select = new SelectButton(master, vendor);   
    one.addActionListener(select);

    JButton two = new JButton("Run");

    c.gridx = 0;
    c.gridy = 0;
    win.add(one, c);

    c.gridx = 1;
    c.gridy = 0;
    win.add(two, c);

    c.gridx = 0;
    c.gridy = 1;
    win.add(master, c);
    win.add(mPane, c);

    c.gridx = 0;
    c.gridy = 2;
    win.add(vendor, c);
    win.add(vPane, c);

    win.setLocationRelativeTo(null);

    win.setVisible(true);

    return;
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
rawa
  • 315
  • 3
  • 8
  • 20
  • Don't, ever, use `setPreferredSize`! [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi) – MadProgrammer Mar 10 '15 at 03:11

1 Answers1

2
  1. Don't, ever, use setPreferredSize! This is overriding the information that the JScrollPane needs in order to make decisions about how the components should be scrolled. See Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? for more details. Instead use the JTextArea(int, int) constructor to provide hints to the JScrollPane, for example JTextArea master = new JTextArea(1, 20);. Any text longer then 20 characters will cause the JScrollPane to display the horizontal scroll bar...
  2. Don't add both the JTextArea AND the JScrollPane to the container. Adding the JTextArea automatically removes it from the JScrollPane, which isn't what you want.
  3. Use GridBagConstaints#gridwidth to control the number of columns a component might expand across to help fix your layout...

For example...

    c.gridx = 0;
    c.gridy = 1;
    c.gridwidth = GridBagConstraints.REMAINDER;
    win.add(mPane, c);

    c.gridx = 0;
    c.gridy = 2;
    win.add(vPane, c);

I'm hoping this is a really simple example, if not, you should always make sure that your UI is created and modified from within the context of the EDT. See Initial Threads for more details

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Thank you so much! Worked exactly as I intended, thank you for clarifying exactly how JTextArea's constructor worked and how it worked in conjunction with JScrollPane, that article by itself confused me a bit haha. – rawa Mar 10 '15 at 03:35