Welcome, java-swing programmers.
I wish to present you my recent issue, as I am chasing my tail again and again. I've created simple JFrame with JPanel container (i tried JList as well). Here i go with BoxLayout and bunch of JButtons, which are vertically aligned, just as i want.
And here comes the problem: the frame needs scroll, but only vertical one. I do wish to see full wrappable names of buttons without horizontal scrolling, just vertical one. Text within them should still wrap, and the vertical scroll should make itself longer as the frame is thinner. SImply adding container to JScrollPane makes buttons loose their title-wrap ability and undesireable horizontal scroll appears. It's worth mentioning that buttons' text will be dynamic and it will be changing according to various actions, so BoxLayout is in my humble opinion simply the best choice here.
What I wish to achieve is similar to effect JTextArea, but instead of text my JScrollPane would scroll down/up buttons.
I did not find any simple solution of this, all hope gone! I count on your skills.
Thank you in advance. Here you go with some code, as my description might not be clear enough:
import java.awt.*;
import javax.swing.*;
public class Main extends JFrame {
public Main() {
setLayout(new GridLayout(0, 1, 10, 10));
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
final JButton tester0 = new JButton("<html>primodial one, the mighty tester of wrapping");
panel.add(tester0);
panel.add(new JButton("one"));
panel.add(new JButton("<html> second one, great knight of the Order of the Didgeridoo"));
panel.add(new JButton("three"));
panel.add(new JButton("four"));
panel.add(new JButton("five"));
panel.add(new JButton("six"));
panel.add(new JButton("seven"));
panel.add(new JButton("eight"));
panel.add(new JButton("nine"));
panel.add(new JButton("ten"));
panel.add(new JButton("eleven"));
panel.add(new JButton("twelve"));
add(panel);
// JScrollPane scroll2 = new JScrollPane(panel);
// add(scroll2);
// final JScrollBar scrollBar1 = scroll2.getVerticalScrollBar();
//// scrollBar1.
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Main frame = new Main();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
//frame.pack();
frame.setSize(270, 200);
frame.setVisible(true);
}
});
}
}
Best regards!
Sounds like you might want the Wrap Layout which will wrap lines to the next row as required.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With