This is the screenshot.
This is my code to build and set the header.
private void buildHeader() {
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
panel.add(Box.createHorizontalStrut(10));
panel.add(new JLabel("Documents"));
panel.add(Box.createHorizontalGlue());
panel.add(new JButton("View"));
panel.add(new JButton("Print"));
panel.setBorder(BorderFactory.createLineBorder(new Color(210, 210, 210)));
panel.setBackground(new Color(245, 245, 245));
panel.setPreferredSize(new Dimension(panel.getPreferredSize().width, 51));
setColumnHeaderView(panel);
}
But, I am not able to make the header to take its whole space. It is leaving the space above vertical scroll bar.
So, How can I make this column header to take up its whole width.
But, I am not able to make the header to take its whole space. It is
leaving the space above vertical scroll bar.
by placing the JComponent to the Corner (there yould be an issue with Borders)
or by moving JPanel contians JButtons out of the JScrollPane
This is how headers work, they are designed to provide header information to the viewable area, so they have to be constrained to the width of the viewable area.
Instead of setting the column header view, try adding the panel to the NORTH position of the parent container and the scroll pane to the CENTER position.
Related
I'm currently learning more about Java. I'm working on creating a GUI which is able to 'translate' amino-acid characters into their 3-letter codes.
I've got everything working as intended, but I'm still struggling to understand how I can resize the text inside my JScrollpane to not exceed the width. (Example in picture)
Do I just need to change some settings or maybe add '\n's to fit the JTextArea? Here's the code:
Thanks in advance!
private void createGUI() {
Container window = this.getContentPane();
window.setLayout(new FlowLayout());
panel = new JPanel();
inputField = new JTextField();
startButton = new JButton("Convert to 3-letter code");
display = new JTextPane();
scroll = new JScrollPane(display);
//CUSTOMIZE GUI OBJECTS
inputField.setPreferredSize(new Dimension(200, 20));
display.setPreferredSize(new Dimension(400, 300));
startButton.addActionListener(this);
//SETTING UP TEXTAREA
display.setEditable(false);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
panel.setLayout(new BorderLayout());
panel.add(scroll, BorderLayout.CENTER);
//
window.add(inputField);
window.add(startButton);
window.add(panel);
}
Better use a JTextArea instead of a JScrollPane since the best that the JScrollPane can do is to dynamically resize (Dynamically Resize a JScrollPane?)
I changed JTextPane display to a JTextArea object and changed 'display.setLineWrap(true);'
This fixed the issue I was having with JTextPane.
To answer the question in the title: How do I resize the text inside my JScrollpane
Inside your scrollpane you have some JComponent. Either that JComponent is fully visible since it is smaller or equal to the JScrollpane's viewport. Or it is bigger, in which case the JScrollpane will start displaying scrollbars and the relevant part.
To resize the text you will just have to tell the JComponent inside the JScrollpane to display the text differently. Depending on the JComponent you use this method may vary. Here some examples:
In a JLabel and most other components, increase the font size (How to change the size of the font of a JLabel to take the maximum size)
In a JLabel, switch to a multiline label (Multiline text in JLabel)
In a JTextArea, turn on word wrapping and line wrapping
In a JEditorPane you can even use markup inside the document to use different font sizes at the same time
For my Java program I am actually using the simple library TableLayout as layout for my main JPanel body so that I can add any widget just by specifying its row and column index, for example"
body.add(new JLabel(
"Search by date"),
"1,8");
Now I would need to add two JScrollPane (one horizontal and one vertical) but they should include all the body and not just a single cell of the layout. Shall I add another JPanel? How can I do it?
Now I would need to add two JScrollPane (one horizontal and one
vertical) but they should include all the body and not just a single
cell of the layout. Shall I add another JPanel?
IMO, yes you should. Nesting Layouts is a common approach that could be applied in this way:
Create a new JScrollPane and set your panel as its viewport view.
Give the scroll pane a reasonable preferred size to enable the scroll bars if your panel's size exceeds this preferred size.
Have a wrapper panel with BorderLayout and add the scroll pane to its CENTER location.
In a nutshell:
JScrollPane scrollPane = new JScrollPane(yourPanel);
scrollPane.setPreferredSize(new Dimension(400, 300));
JPanel wrapperPanel = new JPanel(new BorderLayout());
wrapperPanel.add(scrollPane);
See also:
How to Use Scroll Panes
I have a problem with scroling JPanel,
I have a lot of labels and fields which are generated dynamicly, but my frame can't show it all.
My code:
JPanel showPanel = new JPanel();
JScrollPane scrollPane = new JScrollPane(showPanel);
add(scrollPane);
scrollPane.setBounds(0, 0, 400, 400);
scrollPane.setVisible(true);
scrollPane.add(Jbuttons);
And Im adding a lot of these buttons but my scrollPane won't show it.
I don't have any scrollBar, with text area I didn't have any problems.
Do you have any idea?
scrollPane.setBounds(0, 0, 400, 400); looks like you're using a null layout, don't this
scrollPane.add(Jbuttons); isn't how you should be adding content to the scroll pane, instead, add it to the showPanel which is already inside the JScrollPane. JScrollPane contains a single component, the JViewport, you can not "add" components to the JScrollPane, you must set the JViewports view to what you want shown and the manipulate this view
Take a look at How to Use Scroll Panes for more details
I'm developing a game called GalaxyWar, and I am trying to make a map selection menu. I found a problem that when I am using a BoxLayout with BoxLayout.Y_AXIS on a JPanel with setAlignmentX(CENTER_ALIGNMENT), the subcomponents (JPanel's) with assigned size, take up the entire height of the panel (all together), instead of the assigned height!
Here is my code:
scrollPane = new JScrollPane();
scrollPane.setBounds(160, 11, 452, 307);
add(scrollPane);
mapContainer = new JPanel();
mapContainer.setAlignmentX(CENTER_ALIGNMENT);
mapContainer.setAlignmentY(JPanel.TOP_ALIGNMENT);
mapContainer.setLayout(new BoxLayout(mapContainer, BoxLayout.Y_AXIS));
scrollPane.setViewportView(mapContainer);
JPanel demoPanel = new JPanel();
demoPanel.setLayout(null);
demoPanel.setBackground(Color.YELLOW);
demoPanel.setSize(50, 100);
mapContainer.add(demoPanel);
I've researched on this for long, but couldn't find any solutions so far.
try to check out
setPreferredSize()
setMaximumSize()
setMinimumSize()
set all 3 to the same value.
If it still doesn't work, you can try to put the panel, of which you are trying to set the size to fixed, inside another panel.
I want to set the size of the JTextPane according to the size of the panel so that when i add other panels, it changes accordingly. But it just gives a small text pane in the center and when i add some text, it's size changes accordingly.
JPanel panel = new JPanel();
JTextPane txt = new JTextPane();
JScrollPane pane = new JScrollPane();
pane.add(txt);
panel.add(pane,BorderLayout.CENTER);
add(pane);
now the jtextpane just appears at the center of the screen like a small box. I want it to appear according to the size of the panel
JPanel uses FlowLayout by default which sizes components according to their preferred sizes. You can use BorderLayout which will use the maximum area possible.
Also using constraints such as BorderLayout.CENTER has no effect unless the container is actually using BorderLayout. Dont add components to the JScrollPane. This will replaces all components within the view of the component. Instead set the JTextPane as the ViewPortView, for example
JPanel panel = new JPanel(new BorderLayout());
JTextPane txt = new JTextPane();
JScrollPane pane = new JScrollPane(txt);
// pane.add(txt); remove
panel.add(pane, BorderLayout.CENTER);
Read:
How to Use BorderLayout
How to Use Scroll Panes
You added pane twice. Add panel to your base (a JFrame?) instead and remember to actually set your JPanel to use BorderLayout.