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);
Related
I have the following code taken from GeeksforGeeks that displays the contents of a 2-d array in JTable using JScrollPane:
public class JTableExamples {
// frame
JFrame f;
// Table
JTable j;
// Constructor
JTableExamples()
{
// Frame initiallization
f = new JFrame();
// Frame Title
f.setTitle("JTable Example");
// Data to be displayed in the JTable
String[][] data = {
{ "Kundan Kumar Jha", "4031", "CSE" },
{ "Anand Jha", "6014", "IT" }
};
// Column Names
String[] columnNames = { "Name", "Roll Number", "Department" };
// Initializing the JTable
j = new JTable(data, columnNames);
j.setBounds(30, 40, 200, 300);
// adding it to JScrollPane
JScrollPane sp = new JScrollPane(j);
f.add(sp);
// Frame Size
f.setSize(500, 200);
// Frame Visible = true
f.setVisible(true);
}
What I am trying to do is add a simple Component (like JButton) underneath the table but it does not seem to work. I tried modifying the code by adding the JButton to JPanel and adding JPanel to the frame:
JButton button = new JButton("Back");
JPanel panel = new JPanel();
panel.add(button);
f.add(sp);
f.add(panel);
But this simply deletes the entire table and replaces it with a single button. I also tried adding the button to JPanel and adding that JPanel to JScrollPane:
JButton button = new JButton("Back");
JPanel panel = new JPanel();
panel.add(button);
sp.add(panel);
f.add(sp);
But this did not seem to change anything. I also tried to tinker with preferred and maximum size of JScrollPanel to no avail - it always occupies the entire screen and prevents JButton from appearing on the screen.
Not shooting for design here, just functionality: have a JButton appear underneath my JTable. Any suggestions will be greatly appreciated. Thank you in advance!
The default layout manager of a JFrame is the BorderLayout.
f.add(sp);
f.add(panel);
When you don't specify a constraint for the BorderLayout the CENTER is assumed. You can only have a single component added to the CENTER.
Instead your code should be:
f.add(sp, BorderLayout.CENTER);
f.add(panel, BorderLayout.PAGE_END);
Note the default layout manager for a JPanel is the FlowLayout. So the button will be horizontally centered in the panel.
Also, instead of using a JPanel, try adding the button directly to the PAGE_END of the frame to see the difference.
Read the section from the Swing tutorial on Using Layout Manager for more information and examples for using each of the different layout managers to understand the differences of the above suggestions.
Edit:
Is there a way to decrease the height of the table
If you know you have a small table then you can use:
table.setPreferredScrollableViewportSize(table.getPreferredSize());
This will make the scroll pane the size of the table.
Then you use:
//f.setSize(500, 200);
f.pack();
Now all components will be displayed at their preferred size.
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?
I want to increase the width that the table is covering On Jpanel.
JFrame jf = new JFrame();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setTitle("Person Table");
jf.setSize(1100, 700);
JPanel jp1 = new JPanel();jp1.setBackground(Color.green);
JPanel jp2 = new JPanel(); jp2.setBackground(Color.red);
jp1.setSize(1100, 400);
jp2.setSize(980, 200);
JTable jt = new JTable(data,columnNames); jt.setSize(900, 350);
jt.setBackground(Color.ORANGE);
// Add table to JScrollpane
JScrollPane sp = new JScrollPane(jt); sp.setSize(1000, 380);
sp.setBackground(Color.CYAN);
jp1.add(sp);
jf.add(jp1);
jf.add(jp2);
jf.setVisible(true);
This is the output
I noticed that IF I don't use Scroll pan the Column name disappears and size increases..
But I also want the column name to appear..
Keep the scroll pane, and replace jp1 with it,
AKA Change:
jf.add(jp1);
to
jf.add(jsp);
This will make the table take up the whole green area*. If this isn't what you want, use nested layouts.
*Depending on the LayoutManager. In this case, FlowLayout is used which does not resize the component. Were you using a GridLayout or BorderLayout, the entire green area would be filled.
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.
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.