How can you add several elements to a JScrollPane - java

So I am trying to add more than one element to a JScrollPane element but so far I haven't been able to pull it of.
I can make it so that the first element shows up ,which in my case is a picture. But after adding in an extra panel to the JScrollPane ,the first element disappears and even the second element ,the new panel , doesnt show on my JScrollPane.
JFrame scherm = new JFrame("t?");
scherm.setVisible(true);
scherm.setSize(300, 300);
scherm.setLocationRelativeTo(null);
scherm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//
String path = "C:\\Users\\Bernard\\Documents\\Paradox Interactive\\Crusader Kings II\\mod\\viking\\map\\provinces.bmp";
Image image = ImageIO.read(new File(path));
ImageIcon icon = new ImageIcon(image);
JLabel label = new JLabel(icon);
JScrollPane scroll = new JScrollPane(label);
JPanel paneel2= new JPanel();
paneel2.setSize(new Dimension(400,400));
scroll.getViewport().add(paneel2,null);
scherm.add(scroll);
Thank you for your time!

By doing this:
scroll.getViewport().add(paneel2,null);
You're trying to add a component to the scroll pane's JViewPort shown in the picture below:
This makes no sense. As stated in How to Use Scroll Panes trial:
A
JScrollPane
provides a scrollable view of a component.
This single component is the view port's view. So if you want to have more than a single component in your scroll pane you must to wrap all those components in a lightweight component such as JPanel and set this one as the scroll pane's view port view:
JPanel content = new JPanel();
content.add(label);
content.add(paneel2);
scroll.setViewportView(content);

Related

Swing JScrollPane resizing to fit the viewport panel

I tried a lot of layout managers but none could solve my problem:
I want the items in a scrollPane to keep their size (preferred or minimum) and not being resized (reduced) to fit the viewport Panel. Since if it is a JTextArea, and if the text area has blank space and it is bigger then the viewport, it would reduce it so the blank text area won't be shown. I want the blank text area to be shown for appearance issues.
Im stacking one item after another using BoxLayout, and it seems to me that for text areas the setMinimum method fails.
If the text area has blank space, then the scrollbar of the ScrollPane won't appear, instead it only appears it there are no blank space left.
Any solution?
JScrollPane materialPane = new FScrollPane();
this.materialPaneView = new TPanel();
this.materialPaneView.setMinimumSize(new Dimension((int)(WIDTH*0.95), (int)(HEIGHT/2)));
this.materialPaneView.setLayout(new BoxLayout(this.materialPaneView, BoxLayout.Y_AXIS));
materialPane.setViewportView(materialPaneView);
materialPane.setMinimumSize(new Dimension((int)(WIDTH*0.95), (int)(HEIGHT/2)));
for(Material mat: this.unit.getMaterial()){
this.addMaterial(mat);
}
centerPanel.add(sectionPane);
centerPanel.add(exercisePane);
centerPanel.add(materialPane);
this.add(upperPanel, BorderLayout.NORTH);
this.add(centerPanel, BorderLayout.CENTER);
public void addMaterial(Material mat){
JTextField matName = new JTextField(30);
JPanel fieldButtonPanel = new TPanel();
fieldButtonPanel.setLayout(new GridLayout(1,2));
JPanel fieldPanel = new TPanel(new FlowLayout(FlowLayout.LEFT));
JPanel deleteMatButtonPanel = new TPanel(new FlowLayout(FlowLayout.RIGHT));
matName.setText(mat.getName());
matName.setMaximumSize(new Dimension(FFont.def.getSize()*20, 30));
fieldPanel.add(matName);
JButton deleteMat = new JButton("Delete Material");
deleteMatButtonPanel.add(deleteMat);
fieldButtonPanel.add(fieldPanel);
fieldButtonPanel.add(deleteMatButtonPanel);
fieldButtonPanel.setAlignmentX(LEFT_ALIGNMENT);
JTextArea matText = new FTextArea(mat.getDesc(), (int)(WIDTH*0.95), (int)(HEIGHT/3.4));
matText.setMinimumSize(new Dimension((int)(WIDTH*0.95), (int)(HEIGHT/3.5)));
/*matText.setMaximumSize(new Dimension((int)(WIDTH*0.95), (int)(HEIGHT/3.4)));*/
matText.setText(mat.getDesc());
matText.setAlignmentX(LEFT_ALIGNMENT);
this.materialPaneView.add(fieldButtonPanel);
this.materialPaneView.add(matText);
matName.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
mat.setName(matName.getText());
}
});
HEIGHT and WIDTH are constants, and TPanel FScrollPane are my predefined transparent panels. The BoxLayout panel is the viewport of a scrollPane, and still, it would resize the text areas.
I am not sure i get what you are asking for so please tell me if i totally missed the point...
As far as i know the Viewport size is controlled by the component inside the JScrollPane and the JScrollPane size wont change no matter what happens to the viewport.
You either want to:
A) Resize the JScrollPane to the same size as it's content.
I would implement listeners to look for the content size change and resize the ScrollPane accordingly but you need to pay attention to resize the whole Hierarchy too.
B) You want to resize the viewport so that it fits in the JScrollPane? Y'know without scrollbars.
I had this problem and fixed it by using a ScrollablePanel component. Check this answer, follow the link to download the .class and use it to use a JPanel that resizes to fit the ScrollPane.
Those arent very detailed answers but i will need more information about what you are trying to do before expanding on it. And your code isnt complete, always share a code that we can CTRL+C/V and readily verify the problem in our end.

Adding a text area to a tabbed pane in java

I have two files that I need to display in a program. I need to use JTabbedPane and each file should be displayed in its own tab. I can make the text appear in the tab, but the scroll bar won't appear, so I can't see all of the information in the file. How do I add the scroll bar to the text area?
I made one method that creates a panel with the text in it (this is for one file). Then, I made another method that has JTabbedPane and I added the panel to a tab.
Panel method:
private void makeTextPanel() throws IOException
{
textPanel = new JPanel();
textArea = new JTextArea();
textArea.setEditable(false);
//width: 770 height: 1000
textAreaDimensions = new Dimension(TEXT_AREA_WIDTH, TEXT_AREA_HEIGHT);
textArea.setPreferredSize(textAreaDimensions);
BufferedReader inputFile = new BufferedReader(new FileReader(FILE_ONE));
String lineOfText = inputFile.readLine();
while(lineOfText != null)
{
textArea.append("\n" + lineOfText);
lineOfText = inputFile.readLine();
}
// Add a scroll bar
scrollPane = new JScrollPane(textArea);
// Add the text area and scroll bar to the panel
textPanel.add(textArea);
textPanel.add(scrollPane);
}
Tabbed pane method:
private void makeTabbedPane() throws IOException
{
frame = new JFrame("Project");
tabbedPane = new JTabbedPane();
frame.add(tabbedPane, BorderLayout.PAGE_START);
// add panel to the tab
makeTextPanel();
tabbedPane.addTab("Tab 1", textPanel);
// dimensions
frameDimensions = new Dimension(FRAME_WIDTH, FRAME_HEIGHT);
frame.setPreferredSize(frameDimensions);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
To reiterate:
How do I make the scroll bar visible?
I set the height of the text area to 1000. Will the scroll bar be able to scroll through everything? If not, how do I set the height of the text area to fit everything in the file?
The component you want scroll bars for should always a child of the JScrollPane. Adding the textArea and then the scrollPane to that tabbedPane probably isn't doing what you think it is. Make sure that textArea is the child of scrollPane, and add just the scrollPane to the tabbedPane, ensuring you've specified a layout that dictates how the scrollPane is to take up the space you want within tabbedPane.
The scrollpane will automatically add scrollbars only when it decides the textArea is bigger than it can render in the space it's been given.
Question 1) The JScrollPane methods setVerticalScrollBarPolicy() and setHorizontalScrollBarPolicy() will allow you to force the scrollbars to be always visible.
Question 2) The "preferred" height of the textArea is what your scrollPane will use to determine the scrollbar behaviour (see this example). It's all taken care of for you. If not, you'd be forced yourself to consider font rendering height, how much text you put in the textArea etc.
Generally speaking, just throwing a JTextArea into a JScrollPane will see the desired behaviour you're seeking without you having to do anything "special" with JTextArea size.

JScrollPane opening to bottom of panel

I have a JScrollPane on a panel (which has many panels inside of it) and when I open the frame with the scroll pane on it, the fame is scrolled to the bottom. Is there anyway I can avoid this?
Here are some facts:
The panel the scroller is on contains multiple panels. Some of these panels have text fields. I have tried to set the carat of the text fields to 0 and this did not work.
I know there should be actual code, but when I tried to make a mock pane (as the one I am using is intertwined with a lot of code) it is not replicating the issue.
The panel that is being scrolled is being generated using a loop that generates a series of questions... so a text box, a said amount of buttons/answers, and a text box and label that shows the amount of points for each question.
The last elements of my panel that is being scrolled are a JTextArea and a JLabel.
Below is the code to declare those.
Is there anyone out there that could at least throw out an idea of what would be making the scroll pane automatically go to the bottom?
Here is the declaration of the pane and the panels inside/outside of it
JPanel newPanel = new JPanel(new BorderLayout(0, 0));
JPanel showPanel = new JPanel();
BoxLayout layout = new BoxLayout(showQuizPanel, BoxLayout.Y_AXIS);
showQuizPanel.setLayout(layout);
buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
buttonPanel.setBackground(Color.white);
newPanel.setBackground(Color.white);
showPanel.setBackground(Color.white);
populateButtonPanel();
populateShowPanel(showPanel, buttonPanel);
populateQuestions(showPanel);
JScrollPane scrollPane = new JScrollPane(showPanel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.getViewport().setBackground(Color.WHITE);
scrollPane.setAlignmentX(JScrollPane.CENTER_ALIGNMENT);
scrollPane.getVerticalScrollBar().setUnitIncrement(16);
scrollPane.getVerticalScrollBar().setValue(0);
scrollPane.getViewport().setViewPosition(new Point(0,0));
newPanel.add(scrollPane, BorderLayout.CENTER);
return newPanel;
code to declare last elements on page
JPanel pPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
pPanel.setBackground(Color.WHITE);
pPanel.add(qValue);
pPanel.add(new JLabel("Points"));
qArea.add(pPanel);
qArea.add(Box.createVerticalStrut(50));
qValue.setCaretPosition(0);
Thanks!
I believe that when you add text to a text area when building the GUI, the scroll pane will scroll to make the text area visible.
So basically you need to reset the scroll pane to the top.
You can use code like the following after adding all components to the scroll pane:
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
scrollPane.getViewport().setViewPosition( new Point(0, 0) );
}
});
The invokeLater() adds the code to the end of the Event Dispatch Thread (EDT) so that it gets executed after the GUI is visible.
Of course this code assumes that you are creating the rest of the GUI properly on the EDT.

Java JPanel & JScrollPane

I working on a application that manage image's filters etc.
I want to have scroll bars when the image is to big to be display.
I put my customize panel that extend JPanel in a JScrollPane and I add it in my JFrame.
My image is displayed but not the whole image and the scroll bars are not there.
How to get the scroll-bars to appear?
Here is my code :
CustomePanel test = new ImagePanel(new File("test.jpg"));
test.setPreferredSize(new Dimension(400, 400));
JScrollPane tmp = new JScrollPane(test);
this.getContentPane().add(tmp);
It is likely that your initial preferred size does not match that of your Image. Rather than using setPreferredSize, override getPreferredSize to reflect the size of the image in ImagePanel:
#Override
public Dimension getPreferredSize() {
return new Dimension(image.getWidth(this), image.getHeight(this));
}
A JLabel would be a better approach here if the panel is not required as a container.
try to set the same preferred size to scroll pane as well and test it

Scrolling issue (Java-JFrame-JScrollBar)

INTRO:
I created a java application using JFrame. I have a JMenuBar at the top and under that I'd like to display rows of text.
PURPOSE:
When I have 50 rows and only 20 are displayable at once, I'd like to be able to scroll down and back up again.
PROBLEM:
Of course, my theory doesn't wanna work as it should. My problem is that I don't know how to add a vertical scroll properly.
QUESTION:
How should I change this code to reach my goal?
public void display(){
Container content = this.window.getContentPane();
content.setLayout(new BorderLayout());
Border border = LineBorder.createGrayLineBorder();
//this is just a sample
for(int i = 0;i<50;i++){
JLabel lab = new JLabel("lonyaladek");
lab.setSize(570, 20);
lab.setBorder(border);
lab.setLocation(10, 20+(i*25));
content.add(lab);
}
//scroll
JScrollBar sb = new JScrollBar(JScrollBar.VERTICAL, 0, 0, 0, 0);
content.add(sb);
}
First you need to start with a layout manager that allows you to add multiple components to the container. Maybe a GridLayout is the best place to start.
Then you add this container to the scrollPane and then you add the scrollpane to the window.
So the basic code would be:
JPanel panel = new JPanel( new GridLayout(0, 1) );
panel.add(...);
panel.add(...);
JScrollPane scrollPane = new JScrollPane( panel );
window.add(scrollPane, BorderLayout.CENTER);
I suggest you read the section from the Swing tutorial on How to Use Scroll Panes for more info.

Categories

Resources