I have the following code:
JList<Song> list = new JList<Song>(this.lib);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setLayoutOrientation(JList.VERTICAL);
list.setVisibleRowCount(3);
list.setSize(1, 1);
JScrollPane listScroller = new JScrollPane();
listScroller.setViewportView(list);
list.setSize(10, 10);
setLayout(new FlowLayout(FlowLayout.LEFT,10,10));
add(list);
I am not sure what is wrong here. When I look at the JFrame, I see the list in a single box with all 9 items that I have in my list. I tried messing with the size to see if I could get that to work but it isn't. The size doesn't seem to change no matter what I set it to.
My goal is to have a vertical and horizontal scroll-bar when necessary and a JList that is of some fixed size (fixed compared to the frame size would be best if possible).
You're adding the list to the container, not the listScroller.
JScrollPane listScroller = new JScrollPane();
listScroller.setViewportView(list);
list.setSize(10, 10);
setLayout(new FlowLayout(FlowLayout.LEFT,10,10));
add(list);
You should try using something like...
JScrollPane listScroller = new JScrollPane();
listScroller.setViewportView(list);
setLayout(new FlowLayout(FlowLayout.LEFT,10,10));
//add(list);
add(listScroller);
I think you will also find list.setSize(10, 10); is pointless, as the JScrollPane (actually the viewport, but lets keep it simple) has it's own layout manager.
You should take a look at Jlist#setVisibleRowCount if you want to affect the number of visible rows a JList will show before it needs to be scrolled.
The actual width and height of each row is determined by the ListCellRenderer, but you can use JList#setPrototypeCellValue to affect how these might be calculated.
Related
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.
I used windowbuilder to create a GUI that should be a contact list. There are two lists: one of contacts, the other one of numbers. when you select a contact his number will appear.
But, when I add a lot of contacts, the scroll bar does not automatically appear, as it does to my friend, so I tried adding a JScrollPane. Still does not work.
Here is what it was:
DefaultListModel contact = new DefaultListModel();
JList contacts = new JList(contact);
contacts.setBounds(22, 64, 186, 135);
contentPane.add(contacts);
here is what I tried:
DefaultListModel contact = new DefaultListModel();
JList contacts = new JList(contact);
contacts.setBounds(22, 64, 186, 135);
JScrollPane scrollPane1 = new JScrollPane(contacts);
contentPane.add(contacts);
contentPane.add(scrollPane1, BorderLayout.WEST);
then I tried addind Bounds to the scrollPanel as well:
DefaultListModel contact = new DefaultListModel();
JList contacts = new JList(contact);
contacts.setBounds(22, 64, 186, 135);
JScrollPane scrollPane1 = new JScrollPane(contacts);
scrollPane1.setBounds(22, 64, 186, 135);
contentPane.add(contacts);
contentPane.add(scrollPane1, BorderLayout.WEST);
when I erased 'contacts.setBounds(22, 64, 186, 135);', the list stopped working.
I also tried not having the last line, I tried only having the last line and not having the second last line, I tried adding "scrollPane1.setViewportView(contacts);", I tried taking away the "BorderLayout.WEST" argument for all the other tries, so I basically tried every combination of "solutions" I could find on the internet
I have no idea what is going on and I have no idea how to fix it. All I want is to be able to see all my contacts by scrolling.
EDIT, SOLVED:
The problem was my panel was declared as contentPane.setLayout(null); therefore "BorderLayout.WEST" was unnecessary. In addition, the bounds should be applied to the scrollpane, not the list. Last, I should not have used add(contacts) to the panel. the solution was:
DefaultListModel contact = new DefaultListModel();
JList contacts = new JList(contact);
JScrollPane scrollPaneContact = new JScrollPane(contacts);
scrollPaneContact.setBounds(22, 64, 186, 135);
contentPane.add(scrollPaneContact);
I'm updating in case anyone have a similar problem.
You need to set the policies for JScrollPane using proper constructor.
JScrollPane(Component view, int vsbPolicy, int hsbPolicy)
Creates a JScrollPane that displays the view component in a viewport whose view position can be controlled with a pair of scrollbars.
For instance:
new JScrollPane(COMPONENT, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
EDIT:
DefaultListModel model = new DefaultListModel();
JList contactsList = new JList(model);
JScrollPane scrollPane1 = new JScrollPane(contactsList, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
JScrollPane scrollPane1 = new JScrollPane(contacts);
//contentPane.add(contacts); // get rid of this
contentPane.add(scrollPane1, BorderLayout.WEST);
First you add the contacts JList to the viewport of the scroll pane which is correct.
But then you add the contacts to the frame directly, which is wrong. A component can only have a single parent. By adding the contact JList to the frame you remove it from the scroll pane.
Also, don't use a null layout and don't use setBounds(...). Swing was designed to be used with Layout Managers.
If you need more help then post a proper SSCCE that demonstrates the problem. A SSCCE should be included with every question.
I'm trying to create a scroll bar for my text area. However, the scroll bar isn't appearing. Can anyone give me any tips. This is the from the method which creates the panel where the scroll bar will be.
displayCD = new JPanel();
displayCD.setSize(new Dimension(500, 500));
jta = new JTextArea();
jta.setMaximumSize(new Dimension(500, 500));
scrollPane = new JScrollPane();
scrollPane.getViewport().add(jta);
displayCD.add(scrollPane);
see this example. JScrollPane.VERTICAL_SCROLLBAR_ALWAYS property.
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Main {
public static void main(String[] argv) throws Exception {
JTextArea textArea = new JTextArea();
JScrollPane pane = new JScrollPane(textArea);
pane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
}
}
Reference: http://www.java2s.com/Code/JavaAPI/javax.swing/JScrollPaneVERTICALSCROLLBARALWAYS.htm
You need to provide the textArea to the constructor of the scrollpane. Please check this link, for more information: http://docs.oracle.com/javase/tutorial/uiswing/components/scrollpane.html.
JScrollPane scrollPane = new JScrollPane(textArea);
Check out How to Use Scroll Panes. Here follows their example:
//In a container that uses a BorderLayout:
textArea = new JTextArea(5, 30);
...
JScrollPane scrollPane = new JScrollPane(textArea);
...
setPreferredSize(new Dimension(450, 110));
...
add(scrollPane, BorderLayout.CENTER);
Try to pass the JTextArea in the JScrollPane's constructor, and, most of all, try to give it meaningful size hints (rows and columns).
Applying this to your specific case:
jta = new JTextArea(5, 30);
scrollPane = new JScrollPane(jta);
displayCD = new JPanel();
displayCD.add(scrollPane);
The scroll bar should appear, if your text contents exceed the default dimensions. To always show the bars, try the setHorizontalScrollBarPolicy and setVerticalScrollBarPolicy methods, or other JScrollPane constructors.
Regarding the size of the text area:
Unless you explicitly set a scroll pane's preferred size, the scroll pane computes it based on the preferred size of its nine components (the viewport, and, if present, the two scroll bars, the row and column headers, and the four corners). The largest factor, and the one most programmers care about, is the size of the viewport used to display the client.
I have a BoxLayout (in a panel of a BorderLayout) in which I've put a JTable and a button vertically. I would like the table to have a fixed width and that the height should resize itself up to filling the panel. At the moment, I'm fine with the displayed width but not that it's filling up the whole panel height. For example, if there are zero data in the table, I would like only the column names to be shown. I've tried things like setViewportView() and setPreferredSize(), but can't really make it work. Any suggestions? Is it in the layout or scrollpane?
String[] columnNames = { "Date", "Type"
};
Object[][] data = {
};
JTable myTable = new JTable(data, columnNames);
JButton okButton = new JButton("OK");
JPanel panWest = new JPanel();
panWest.setLayout(new BoxLayout(panWest, BoxLayout.PAGE_AXIS));
JScrollPane scrollPane = new JScrollPane(myTable);
panWest.add(scrollPane);
panWest.add(okButton);
EDIT
This is what ended up working:
Dimension dimension = new Dimension();
dimension.height = (myTable.getRowCount()*myTable.getRowHeight())+
myTable.getTableHeader().getPreferredSize().height;
dimension.width = myTable.getColumnModel().getTotalColumnWidth();
scrollPane.setMaximumSize(dimension);
I would like the table to have a fixed width and that the height
should resize itself up to filling the panel.
and
I have a BoxLayout (in a panel of a BorderLayout) in which I've put a
JTable and a button vertically.
there are three (mentioned only simple) ways
use built_in LayoutManager for JPanel - FlowLayout (FLowLayout.CENTER),
override FLowLayout.LEFT or RIGHT in the case that you want to aling,
JComponent laid by FlowLayout never will be resizable without programatically to change XxxSize and notified by revalidate & repaint,
set proper size for JScrollPane by override JTable.setPreferredScrollableViewportSize(new Dimension(int, int));,
JScrollPane accepts this Dimension as initial size, required is usage of JFrame.pack(), before JFrame.setVisible(true)
BoxLayout accepts min, max and preferred size, override all these three sizes for JScrollPane
(half solution, but most comfortable) change LayoutManager for JPanel to BorderLayout,
put JScrollPane with JTable to BorderLayout.EAST/WEST area,
then override JTable.setPreferredScrollableViewportSize(new Dimension(int, int));,
JScrollPane will occupy whole area for BorderLayout.EAST/WEST and will be resizeable only on heights coordinates
use MigLayout, here are a few posts about MigLayout and JScrollPane (with JTextArea, JTable)
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.