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.
Related
Everytime I click a button in my program, I add 5 new rows to the JPanel. Eventually, the rows overflow and I would like to add a JScrollPane so I can scroll down and see the new rows.
I know how to get it working for a TextArea but I can't seem to figure out how to make it work when I have a GridBagLayout. Below, I will attach the code for setting up my panels.
JPanel panelMain = new JPanel();
getContentPane().add(panelMain);
JPanel panelForm = new JPanel(new GridBagLayout());
panelMain.add(panelForm);
JScrollPane scrollpane = new JScrollPane(panelForm);
panelMain.add(scrollpane);
When I run, my code I get a box enclosing the GridBagLayout, but the scrollpane is nowhere to be seen.
JPanel panelMain = new JPanel();
getContentPane().add(panelMain);
You would typically add the scroll pane directly to the frame. There is no need for the "panelMain".
panelMain.add(panelForm);
That line is not needed. A component can only belong to a single parent. You later add "panelForm" to the scrollpane.
So the basic code would be:
JPanel panelForm = new JPanel(new GridBagLayout());
JScrollPane scrollpane = new JScrollPane(panelForm);
frame.add(scrollpane);
but the scrollpane is nowhere to be seen.
The scrollbars only appear when needed by default. Adding empty panels will not cause the scrollbar to be displayed. Click on your button a few times and add your child components to the "panelForm" using the appropriate GridBagConstraints. You will then need to use:
panelForm.revalidate();
panelForm.repaint();
The revalidate() causes the layout manager to be invoked so the scrollpane can determine is scrollbars are required or not.
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);
This is the code for my GUI:
public TLGUI(){
final int x=500,y=600;
JFrame frame=new JFrame();
frame.setSize(x, y);
frame.setVisible(true);
frame.setResizable(false);
JLabel labelTL=new JLabel("This is a test label");
JScrollPane pane = new JScrollPane(labelTL,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
JPanel panel=new JPanel();
panel.add(labelTL);
frame.add(panel);
frame.add(pane);
}
I have a huge problem right now regarding the line marked with the ** **.
This code does indeed add a scrollbar to my window, but the problem is that if I place it in front of the TLFrame.add(panel), I wont see it at all (I guess the panel is covering it in this order), and as soon as I turn it around, I can see a scrollbar but the whole frame other than the scrollbar is grey (I suppose the scrollbar is covering the panels contents here).
However, I want both of them to be visible at once, of course. Because my Label is bigger than the Frame, I want to be able to scroll down at least.
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
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?