Adding Jtable to Tab in java - java

I am developing an java gui. It has TabedPane. I have added panel to tab. How to add jtable to panel which is in tab2;

To get the compoenent at a particular index use getComponentAt (as suggested by Jonas Eicher in the comment.)
JPanel tabPanel = (JPanel) tabbedPane.getComponentAt(index);
Add your table to the panel like the following.
tabPanel.add(new JScrollPane(table));

Just create fields or local variables for your components, so you can reference them, i.e.:
JPanel panel = new JPanel();
JTable table = new JTable();
tabbedPane.insertTab("title", null, panel, null, 0);
panel.add(table);

Related

Add JButton to JScrollPame

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.

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);

Fix the length of java GUI Table

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

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.

Adding components via code

I'm trying to have painted into a JPanel (which is inside a ScrollPane), a bunch of labels and RadioButtons, dynamically. I receive an ArrayList with "Advice" objects, and I want to iterate over them to represent them in a way I have a label that describes them, and then, two radio buttons (to choose "Yes" or "No").
But at the moment, with this code at the JFrame's constructor, it's not properly working:
// My constructor
public CoachingFrame(AdvicesManager am) {
initComponents();
this.am = am;
// I set the layout for the inner panel (since ScrollPane doesn't allow BoxLayout)
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
// Iterate over the arraylist
for(int i=0;i<am.advices.size();i++){
//Add elements to the panel
panel.add(new JLabel( am.advices.get(i).getQuestion()));
ButtonGroup group = new ButtonGroup();
// Group the RadioButtons inside another panel, so I can use FlowLayout
JPanel buttonsPanel = new JPanel();
buttonsPanel.setLayout(new FlowLayout());
JRadioButton rad1 = new JRadioButton();
JRadioButton rad2 = new JRadioButton();
group.add(rad1);
group.add(rad2);
buttonsPanel.add(rad1);
buttonsPanel.add(rad2);
// Add the radiobuttons' panel to the main one, and revalidate
panel.add(buttonsPanel);
panel.revalidate();
}
// Finally, add the panel to the ScrollPane.
questions.add(panel);
}
I receive the arraylist correctly; I already checked that. The problem seems to be when painting the components.
Since I always use the NetBeans GUI creator, I'm not very used to add components via code. Can someone help me? I guess I'm missing something here.
edit: Note that "questions" is the ScrollPane object!
edit 2: This "questions" panel should have all those components painted: http://i.imgur.com/tXxROfn.png
As Kiheru said, ScrollPane doesn't allow views (like my JPanel) to be added with .add(), instead, I had to use .setViewportView(Component). Now it's working perfectly, thank you!

Categories

Resources