How to scroll through buttons in JPanel? - java

So, I have a JPanel, panelWButtons, which contains a list of buttons. panelWButtons has a size of (350, 130). I was wondering how would I add a vertical scrollbar to the right side of the JPanel? So that if the buttons are exceeding the length of the panel, a scrollbar will appear Much help would be appreciated. Here is a snippet of my code:
final JPanel panelWButtons = new JPanel();
panelWButtons.setPreferredSize(new Dimension(350, 130));
JScrollPane scroller = new JScrollPane(panelWButtons); //is this right?
add(scroller, BorderLayout.CENTER);

Related

Can't left align a JLabel within a JPanel when adding to a BorderLayout

This seems like a simple thing to do, but I can't get it to work.
I have a BorderLayout. I want to use the top part for a title bar. I want to add a JPanel with labels, buttons and other components. However, the PAGE_START part of the border layout won't left align the panel. Here's the situation, with comments in where I've tried to set the alignment.
I noticed that when I don't add a panel to the border layout, and just write the JLabel straight in, it has left alignment by default.
This is not what I want, though, because I am planning on putting a BoxLayout.X_AXIS horizontally through the BorderLayout.PAGE_START title area. Seems to be a reasonable thing to do?
The Container pane argument to the static method is just the single panel on the main JFrame.
public static void addComponentsToPane(Container pane)
{
JLabel jlabel = new JLabel("I want to left align this inside a JPanel");
// Doesn't work: jlabel.setAlignmentX(Component.LEFT_ALIGNMENT);
JPanel jpanel = new JPanel();
//Doesn't work: jlabel.setAlignmentX(Component.LEFT_ALIGNMENT);
jpanel.add(jlabel);
pane.add(jpanel, BorderLayout.PAGE_START);
// Other parts of the BoxLayout (works fine)
JButton button = new JButton("Button 2 (CENTER)");
button.setPreferredSize(new Dimension(200, 100));
pane.add(button, BorderLayout.CENTER);
button = new JButton("Button 3 (LINE_START)");
pane.add(button, BorderLayout.LINE_START);
button = new JButton("Long-Named Button 4 (PAGE_END)");
pane.add(button, BorderLayout.PAGE_END);
button = new JButton("5 (LINE_END)");
pane.add(button, BorderLayout.LINE_END);
}
Even when I tell the panel to left align the label, it doesn't appear left aligned.
Does anyone know what I am doing wrong?
By default a JPanel uses a FlowLayout with "center" alignment.
if you want components "left" aligned, then you need to set the layout on the panel to use a FlowLayout with "left" alignment.
Read the FlowLayout API for the proper constructor to use to set the alignment.
Or you can also read the Swing tutorial on How to Use FlowLayut which gives the constructors and valid values to specify the alignment.

JScrollpane does not notice another JScrollpane

I have a JFrame which contains a JSplitPane in a JScrollPane (so the user can scroll if the window is to big). The JSplitPane contains a JTabbedPane as the top component and graphics as the bottom component.
Now i want to read a .csv und display it in my JTabbedPane. I can scroll through the list with a second JScrollPane. Here comes the problem, when i import the .csv in my programm, the first JScrollPane seems not to notice that there is a second JScrollPane for scrolling the list and then my window gets a lot of free space to scroll.
JFrame frame = new JFrame();
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP, JTabbedPane.WRAP_TAB_LAYOUT);
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
splitPane.setTopComponent(tabbedPane);
splitPane.setBottomComponent(graphics());
JScrollPane scrollPane = new JScrollPane(splitPane);
frame.add(scrollPane);
frame.setVisible(true);
When i import the .csv I add a new JPanel to the tabbedPane. The JPanel contains a list from the data from the .csv
JPanel panel = new JPanel();
JScrollPane scrollPane = new JScrollPane(panel);
// here comes the code for the list
tabbedPane.add(scrollPane);
I hope you understand my problem, it is hard to explain.
Edit: Pictures from before and after importing the .csv may help you to understand.
Get rid of the first scroll pane. Add the split pane directly to the CENTER of the BorderLayout used by the frame. As the frame resizes all the space will be allocated to the split pane.
You can then use:
splitPane.setResizeWeight(1.0);
Now all the extra space will go to the second component as the frame is resized. Therefore the scrollbar for that component will appear/disappear as required.

JScrollPane increases its size

I have a panel which is divided by two parts with BoxLayout.X_AXIS:
public TabsPanel() {
setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
add(createLeftPanel());
add(createRightPanel());
}
Each left and right panels have the following structure: an outer panel with BorderLayout, and an inner panel in BorderLayout.CENTER of the outer panel, which in its turn has BoxLayout.Y_AXIS and several components from top to bottom. The right panel has JTextArea with JScrollPane as one of its components:
protected JPanel createRightPanel() {
JPanel pane = new JPanel();
pane.setLayout(new BorderLayout());
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JTextArea label = createLabel();
JScrollPane scroll = new JScrollPane(label);
scroll.setMaximumSize(new Dimension(500, 200));
panel.add(Box.createRigidArea(new Dimension(0,106)));
panel.add(scroll);
JPanel panel_buttons = new JPanel();
panel_buttons.setLayout(new BoxLayout(panel_buttons, BoxLayout.LINE_AXIS));
panel_buttons.setAlignmentX(Component.CENTER_ALIGNMENT);
Font font_text = new Font("Georgia", Font.PLAIN, 20);
JButton[] buttons = new JButton[2];
buttons[0] = new JButton("Clear");
buttons[1] = new JButton("Exit");
for (int i = 0; i < buttons.length; i++) {
buttons[i].setMaximumSize(new Dimension(120, 40));
buttons[i].setFont(font_text);
panel_buttons.add(buttons[i]);
if (i == 0)
panel_buttons.add(Box.createRigidArea(new Dimension(40, 0)));
buttons[i].addActionListener(new TextActionListener(label));
}
panel.add(Box.createRigidArea(new Dimension(0,20)));
panel.add(panel_buttons);
pane.add(panel, BorderLayout.CENTER);
return pane;
}
When text goes beyond the borders, scroll bars appear and I can move them and read the text. Looks like everything is ok, but when I either click any place outside the scroll pane or even just move the pointer, the scroll pane moves to the left and grows down. It doesn't change its width, but it shifts to the left because the area between it and the right panel's borders increases. Accordingly, size of the left panel shrinks. When I clear the text area and again either click or move the pointer, it is back to its normal size.
What is the reason its height grows and its left and right margins increase? What am I doing wrong?
UPDATE. I've found the problem. The thing is that I didn't create JTextArea correctly. I initialized it without parameters:
JTextArea text = new JTextArea("Some initial text");
Now I have rewritten:
JTextArea text = new JTextArea(5,10);
It is now shifted to the left by about 5 mm and do not changes its height. Still not perfect, but looks like I am on the right track.
Thank you everybody for your help!
BoxLayout accepting Min, Max and PreferredSize override those methods for JPanel
use JSPlitPane, there you can to hide Divider
2 steps to correct:
Set the size of the JTextArea: JTextArea text = new JTextArea(row, col);
Still shifts to the left by the size of the vertical bar:
either add ChangeListener to adjust the size of the JScrollPane
scroll.getViewport().addChangeListener(new ChangeListener() {
#Override
public void stateChanged(ChangeEvent e) {
if (scroll.getVerticalScrollBar().isVisible())
scroll.setPreferredSize(480, 200);
}
}
});
or add scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

Java. Swing. Multiline labels in a JScrollPane

I have a JFrame with JScrollPane in it. I have JPanel inside a scrollPane. And add multiline labels in it.
Everything is ok with multiline labels. I enclose my text in <HTML>..</HTML> tags.
And labels display its wrapped text.
"..." means long multiline text.
The problem is that useless area is displayed in the bottom.
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(300, 300));
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JLabel label1 = new JLabel();
JLabel label2 = new JLabel();
label1.setText("<html>" + "..." + "</html>");
panel.add(label1);
label2.setText("<html>" + "..." + "</html>");
panel.add(label2);
JScrollPane scroll = new JScrollPane(panel);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
frame.setContentPane(scroll);
frame.pack();
frame.setVisible(true);
EDIT.
So I have to set preferred size for inner JPanel. After that scrollPane draws its content(shows scrollbars) as its content has this fixed "inner panel preffered size".
If I won't set preferred size for the panel, JLabels wouldn't wrap the text.
After being layed out by the layout manager inner panel's size grows and became larger than previously set preferred size. Panel grows itself, its ok, I see wrapped text of labels in it. But scrollpane behaves incorrectly. It paints scroll as inner panel is still of prefferred size size. So I need correct resizing behaviour for JScrollPane.
use JTextPane or JEditorPane instead of JPanel contains bunch of JLabels
JTextPane or JEditorPane supporting stylled text or Html <= 3.2 for Java6
theoretically you can use JList, instead of Jlabels, but in this case you have to call for setPreferredScrollableViewportSize(new Dimension) same as for JPanel in the JScrollPane
EDIT
then use Highlighter
use built-in reader/writer for JTextComponents

scrollpane blocks buttons in BorderLayout?

I have a really weird problem with a JScrollPane and a BorderLayout. For short explaination: i have a JTable which is inside the JScrollPane and this is with a JPanel and the JTableHeader on a JTabbedPane. Very Simple Layout. If i add just the JTable to my JPanel, the buttons are working. If i add the JScrollPane, the Buttons are not working anymore, so i cant click them! The ActionLister is never reached and i cant see the click-animation.
Some Sample code to explain:
d_pane = new JPanel();
d_button = new JPanel();
d_pane.add(table.getTableHeader(), BorderLayout.PAGE_START);
dl_scroll = new JScrollPane(table);
d_pane.add(dl_scroll, BorderLayout.CENTER);
// d_button is ridLayouted with 3 Buttons in there
d_pane.add(d_button, BorderLayout.PAGE_END);
1) The JScrollPane takes care of the table header itself. Don't add it to the pane.
2) the button does not seem to get the mouse events, probably because another component is above it - do you have other components/code in the setup?

Categories