JTable and JButton inside JScrollPane - java

I have a JPanel with many components on it. He is a part of the main frame. I used GridBagLayout on him for placing components and it all works great.
In one part of that JPanel I placed JScrillingPane with another JPanel (p2). I need to place JTable (extended one by my own) and JButton on p2 one beyond second one. I used several Layouts on p2 but my extended JTable ALWAYS gets too much wide. It must not pass JScrollPane (or p2 border, whatever). When I put only JButton on p2, all works fine, but with table, all goes really bad.
So, the order of components is: JPanel with GridBagLayout, JScrollPane, JPanel with any layout, JButton and JTable at last level.
What I need to look for or whatever? I didnt post the code because it is very long and will take too much space on this page.
P.S. I know there are similar questions, but I really didnt find anything helpfull for me.I searched for hours already.

Related

My Jpanels keep sinking when I go to them

In my program I go from the log in to the main menu,to different areas of the site.
jPanel2.removeAll();
Items panel = new Items();
jPanel2.add(panel);
jPanel2.repaint();
jPanel2.revalidate();
I thought that I found a way to change jpanels so that I can go from one jpanel to another which in the same screen.
The problem with the above code is that each time I change jpanels, the old jpanel isn't gotten rid of, it is staying there, meaning whenever I change jpanels the jpanel slowly gets lower on the page due to it being put underneath the other jpanels that were created before it.
Is there a way to get rid of the jpanels so that when I go to a new jpanel it doesn't slowly lower down the page?
I think you should use CardLayout to achieve this requirement.
Check this below link.
https://docs.oracle.com/javase/tutorial/uiswing/layout/card.html
or this SO
Changing Panels using the Card layout

Setting boundaries for JPanel

I am new on using Swings
my requirement is to align components within the jpanel(panel2)
I have taken 2 JPanels (panel1, panel2) and added to the jframe
panel1.add(panel2);
panel2.setLayout(new flowLayout());
panel2.setBounds(80,120,100,100);
getContentPane() .add(Panel1);
and I have created a "Create" button that will generate text area dynamically in panel2
now my problem is if the created textarea is reaching out of panel2 it has to show an error
message "You reached the boundaries of the jpanel so the textarea cant be created "
Thanks in advance
Set the layout before you add any elements to the panel. No not call setBounds as with layout manager present it likely has no effect at all.
I cannot explain how to layout your elements as from your question seems not possible to figure out that do you want to do. Best, post the drawing with elements as they should look like. GridLayout maybe would be good if you want to align multiple elements as in the table.

java JLayeredPane how to add JPanel in front of another panel generated by Window builder

I am writing in java some kind of application and i have to place something in front of another objects( in Z order)
I know that that i should use JLayeredPane but in fact i am not very familiar with it.
My idea is to make two JPanel's with different Z-order factors while inserting them to JLayeredPane.
i pasted my code http://www.wklejto.pl/130038
i would be very grateful if you tell me what is wrong because i am doing it for a long of time with no effect.
I don't see anything wrong with this code. Maybe you're trying to paint a transparent (not opaque) JPanel (e.g. with message) on top of the underlying base JPanel.
In that case you should invoke setOpaque(false) on your front JPanel.
JPanel second = new JPanel();
second.setOpaque(false);
second.add(new JLabel("message"));
jlp.add(second, new Integer(300));
JPanels are opaque by default - on the other hand JLabels aren't.
And take a look into tutorial.

How can I position a JButton under a JTable?

How can I position a JButton under a JTable? What kind of layouts? How? I have a JTable table what is scrollable, and the table is in a frame.
There are many layouts to fulfill this need.
The simplest is using BorderLayout:
JFrame frame = new JFrame();
Container contentPane = frame.getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(new JScrollPane(jtable), BorderLayout.CENTER);
contentPane.add(button, BorderLayout.SOUTH);
If you want this button to be not resized just add JPanel to contentPane. This JPanel should contain your button centered using almost any layout.
If you would like to use more sophisticated layout - the best in my opinion is MigLayout
To correct LoveToCode's well-meaning but misleading advice, you would never want to give a JTable itself a layout. The solution to your problem is not to set the JTable's layout but to set the layout for the JPanel that holds both the JTable's JScrollPane and the JButton. Likely a BorderLayout would work best with the JScrollPane being placed BorderLayout.CENTER and the JPanel that holds the JButton BorderLayout.SOUTH.
Note, if you're adding these components to the JFrame, then know that its contentPane already uses BorderLayout (the tutorials will tell you this -- please read them). So just add these guys to the contentPane as described above.
If hard coding of swing and awt is not mandatory, try using the WindowsBuilder Pro, a Free tool now from google, install its plugin in to eclipse, then Use
GroupLayout - Introduced by NetBeans team in 2005 integrated in WindowsBuilder Pro, is one of the most convenient way create a good gui in less time in Java.
It seems that you can set the layout of a jtable the same way in which you can set the layout of a jframe or a jpane:
table.setLayout(new grideLayout(4, 3)
would give it a grid layout with 4 rows and 3 columns.
the scrollable feature allows you to 'scroll' through your table with the moving bar. It may be a default feature, or it may be that you must use it, try seeing what methods your table gives you.
Lastly, your table should be in a frame, so that you can view it on your window, make you class extend JFrame and it will automatically be a frame upon which you can simply add a table!

JLabel problem with BoxLayout in Java

I have a panel with a BoxLayout declared as follows:
venueInfoPanel.setLayout(new BoxLayout(venueInfoPanel, BoxLayout.Y_AXIS));
When I add two JTextArea to this panel, they all align to the left, which is what I want. However, when I add a JLabel, it aligns itself to the center, instead of to the left. Why is this? How can I make it so it aligns with all the other JTextArea? I read the document here and found out that I use Component.LEFT_ALIGNMENT and I did that by doing
label.setAlignmentX(label.LEFT_ALIGNMENT);
where label is the JLabel I wanted to add to the JPanel
All components need the alignmentX set to left. The tutorial you referenced has plenty of working examples. You should be able to figure it out on your own. If you still have a problem post your SSCCE demonstrating the problem.

Categories

Resources