two synchronized scrollBars don't work - java

I need to do two element with one horizontal scroll bar. I did:
JTable myTable = new JTable(10, 5);
JTable myTable2 = new JTable(1, 5);
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
JScrollPane SP1 = new JScrollPane(myTable, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
SP1.setColumnHeaderView(myTable.getTableHeader());
JScrollPane SP2 = new JScrollPane(myTable2, ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
SP2.getHorizontalScrollBar().setModel(SP1.getHorizontalScrollBar().getModel());
panel.add(SP1, BorderLayout.CENTER);
panel.add(SP2, BorderLayout.SOUTH);
getContentPane.add(panel);
I need to scroll my SP2 together with the SP1.
But They don't. I can't see horizontal scrollBar after SP2 panel.
And no one element hasn't horizontal scrolling.
Please, help me to find what I missed.
I looked the example in that post How to scroll two or more JTables with a single Scrollbar?

Related

Java, GirdLayout, ScrollPane not working

I have Two JTables, every JTables is added to a Container with a Borderlayout (the TableHeader to BoderLayout.PAGE_START and the JTable to the Center) these Container are added to a JScrollPane.
The Two Container are added to a Container with a Gridlayout, which is added to the Center of the JPanel.
The Problem is, that the scrollBar is not showing or if a force the scrollbar to Display all the time, it is not working. The JTable has over 100 entries but only the first ~30 entries are shown.
I already tried searching and found already some possible fixes, but they didn't work. I have tried it with a prefered Size, added a Layout to the ScrollPane and some other possibilties, but nothing worked.
Some code of the Jtable:
this.locations = new JTable(data, columnNames);
this.locations.getTableHeader().setReorderingAllowed(false);
this.locations.setDefaultEditor(Object.class, null);
// this.locations.setPreferredSize(new Dimension(100, 100));
Container table = new Container();
table.setLayout(new BorderLayout());
table.add(this.locations.getTableHeader(), BorderLayout.PAGE_START);
table.add(this.locations, BorderLayout.CENTER);
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
this.content.add(scrollPane);
Locations is the JTable, Content is the Container with the Gridlayout, which is then added to the JPanel with the BorderLayout.
Container table = new Container();
table.setLayout(new BorderLayout());
table.add(this.locations.getTableHeader(), BorderLayout.PAGE_START);
table.add(this.locations, BorderLayout.CENTER);
There is no need for the above code.
All you need is:
this.locations = new JTable(data, columnNames);
JScrollPane scrollPane = new JScrollPane(table);
this.content.add(scrollPane);

I want my JTable at the centre

I want my JTable at the centre of my JFrame and for this purpose i have used setbounds method but it's not doing anything and i don't want to use layout managers.I just want to know that why it's not doing anything?
Here is my code:
tabel=new JTable(data,columnNames);
tabel.setLayout(null);
tabel.setPreferredScrollableViewportSize(new Dimension(500,50));
tabel.setFillsViewportHeight(true);
JScrollPane pane=new JScrollPane(tabel);
pane.setBounds(100,700,200,200);
add(pane);
It's better to use layout managers but if you insist,that you want to move JTable in JFrame by setting layout to null,then you should try the following option:
(1)Make a JPanel in JFrame and add that table to JPanel in this way
JPanel panel=new JPanel();
panel.setBounds(20,300,700,300);
add(panel);
tabel=new JTable(data,columnNames);
tabel.setBounds(100,20,700,400);
tabel.setPreferredScrollableViewportSize(new Dimension(500,50));
tabel.setFillsViewportHeight(true);
JScrollPane pane=new JScrollPane(tabel);
panel.add(pane);
You should add everything to a JPanel, and then set the layout of the JPanel to a border layout. Add the table to the JPanel and then position it at the center. This is the easiest way to do it. Layouts may seem complicated and inconvenient at first but once you learn them, you quickly realise they are a million times easier that setting bounds and null layouts. Here is a link to learn about border layout: https://docs.oracle.com/javase/tutorial/uiswing/layout/border.html
You are adding JTable to a JFrame(Top level component).This may get you going
JPanel tablePanel = new JPanel(new GridBagLayout());
GridBagConstraints layout= new GridBagConstraints();
JTable jtable = new JTable();
jtable.setPreferredScrollableViewportSize(new Dimension(500,50));
jtable.setFillsViewportHeight(true);
layout.fill = GridBagConstraints.BOTH;
layout.weightx = 1;
layout.weighty = 1;
layout.gridx = 0;
layout.insets = new Insets(10,10,10,10);
tablePanel.add(new JScrollPane(jtable), layout)
You may want to set layout dimensions and insets according to your jtable.

how to add scrollbar to textarea in java

the jscrollpane that I am adding doesnt appearin my textarea
textArea = new JTextArea();
scroll = new JScrollPane(textArea);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
this.add(textArea);
this.add(scroll);
this.setSize(1000, 600);
this.setLayout(new BorderLayout());
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
textArea = new JTextArea();
scroll = new JScrollPane(textArea);
//this.add(textArea); // get rid of this
this.add(scroll);
You create the scrollpane with the text area, but then the next statement removes the text area from the scrollpane because a component can only have a single parent.
Get rid of that statement and just add the scrollpane to the frame.
Then scrollbars will appear automatically as you add data to the text area.
Also you should create the text area using something like:
textArea = new JTextArea(5, 20);
to give a suggestion on how big to make the text area.
I did what you said but still nothing happens
Another problem is that you need to set the layout manager BEFORE you start adding components to the frame (or panel).
Remove this.add(textArea); and add scroll.setSize( 100, 100 ); will also work for you.

Scroll bars for JTextArea

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.

Add JScrollpane to JPanel containing multiple JPanels

I have one JFrame (black) that contains one main JPanel (grey), this JPanel contains three other JPanels : North and South (red) and the Center one (blue). The blue JPanel in the center will have lots of JPanels (green) added dynamically to it during the course of the program. When the Center JPanel is full I would like a JScrollbar to appear automatically to scroll down the Center Panel and see all the child (green) panels it contains. Can somebody help me? The problem is that the scrollbar isn't appearing at all, if i add 15 green JPanels to my blue JPanel container, i only see 10 and i can't scroll down.
This is the type of code i have tried so far...
JPanel panelNorth = new JPanel(new GridBagLayout());
panelNorth.setPreferredSize(new Dimension(1000,100);
//add some labels and buttons
JPanel panelCenter = new JPanel();
panelCenter.setLayout(new BoxLayout(panelCenter, BoxLayout.Y_AXIS));
panelCenter.setPreferredSize(new Dimension(1000,500)
JPanel panel1 = new JPanel(new GridLayout(1, 10));
panel1.setPreferredSize(new Dimension(1000,50));
panelCenter.add(panel1);
//...etc dynamically
JPanel panelSouth = new JPanel(new GridBagLayout());
panelSouth.setPreferredSize(new Dimension(1000,100);
//add some labels and buttons
JScrollPane scrollPaneCenter = new JScrollPane(panelCenter,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
//panelCenter must be scrollable when too many panels are added to panelCenter
//add everything to the main panel container
JPanel panelContainer = new JPanel(new BorderLayout(3,3));
panelContainer.setBorder(new EmptyBorder(5,5,5,5));
panelContainer.add(panelNorth,BorderLayout.NORTH);
panelContainer.add(scrollPaneCenter,BorderLayout.CENTER);
panelContainer.add(panelSouth,BorderLayout.SOUTH);
//add everything to frame
JFrame frame = new JFrame();
frame.add(panelContainer);
Thank you.
EDIT:
I changed
JScrollPane scrollPaneCenter = new JScrollPane(panelCenter,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
to this :
JScrollPane scrollPane = new JScrollPane(panelCentre,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
And this is what i get:
I can see it now, it's in the right place, i just can't scroll down to see my other components.
Fixed it by switching
panelCenter.setPreferredSize(new Dimension(1000,500);
with
scrollPane.setPreferredSize(new Dimension(1000,500));

Categories

Resources