Insert a JPanel with null layout into a JScroll - java

I have a JPanel (myPanel) with a lot of button inside (this panel is contained in another panel that contain other components). I would insert this JPanel (myPanel) into a scroll to control better the button.
This is a part of my code:
JPanel firstPanel = new JPanel(null);
......
......
JPanel myPanel = new JPanel(null);
myPanel.setBounds(0, position+22, 400, 500);
for (int i=0; i<size; i++) {
JButton button = new JButton(myList.get(i));
if (counter%4 == 0) {
button.setBounds(270, 0+(4*i), 90, 18);
} else if (counter%3 == 0) {
button.setBounds(180, 4+(4*i), 90, 18);
} else if (counter%2 == 0) {
button.setBounds(90, 8+(4*i), 90, 18);
} else {
button.setBounds(0, 12+(4*i), 90, 18);
}
myPanel.add(bottone);
}
......
......
firstPanel.add(myPanel);
So, how can i do to insert it into a scroll?

Your question appears to ask about how to add a JPanel to a JScrollPane when the JPanel uses null layout, and the answer is easy:
First and foremost DON'T use a null layout. Use of null layouts almost guarantees that the component held by the JScrollPane won't scroll appropriately since the JScrollPane mechanics require the use of this.
Either this or you will be required to create a class that extends JPanel and implements the Scrollable interface, and this will require far more work, and completely unnecessary work.
Instead you really are forced to learn how to use and then use an appropriate mix of layout managers to have your JPanels hold and display their components. Note that you can nest JPanels, each using its own layout, thereby easily creating complex but easy to maintain GUI's. Please check the layout manager tutorial for more on this.
Adding a JPanel to a JScrollPane is easy. Either pass the JPanel into the JScrollPane's constructor: JScrollPane scrollpane = new JScrollPane(myPanel); or else pass the JPanel into the JScrollPane's viewport view via setViewportView(myPanel). Here's the JScrollPane tutorial for more on this, and the general Swing tutorials.

Related

Second JPanel and JTexField look too cramped

I have an application where I use two JPanels. One of them is a PaintPanel. The second panel,the jtextfield and the jtextarea work fine but they look too cramped against the right side. I tried changing the sizes with setSize() but it didn't work.
The code for the paintpanel
public void center() {
jpCenter = new PaintPanel();
jpCenter.addMouseListener(this);
jpCenter.setSize(100, 100);
jpCenter.setBackground(Color.white);
add(jpCenter, BorderLayout.CENTER);
}
The code for the panel of the chatbox
public void east() {
// CREATE EAST Panel
gl = new GridLayout(4, 1);
jpEast = new JPanel();
jpEast.setSize(200, 200);
jpEast.setLayout(gl);
jpEast.setBackground(Color.white);
label = new JLabel("Number of shapes: ");
jpEast.add(label);
// ADD TEXT FIELD
jtf = new JTextField();
jtf.setText("");
jtf.setSize(200, 200);
jpEast.add(jtf);
// ADD BUTTON
jbSend = new JButton("Send");
jbSend.setEnabled(false);
jbSend.setSize(20, 60);
jpEast.add(jbSend);
jbSend.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
send(jtf.getText());
jtf.setText("");
}
});
// ADD TEXT AREA
jta = new JTextArea("");
jta.setSize(100, 100);
jpEast.add(jta);
// ADD EAST panel
add(jpEast, BorderLayout.EAST);
}
Avoid setting the size of components since it can make them not work well on all platforms, and with JTextArea in particular, it will not allow it to expand correctly if held within a JScrollPane (which is where a JTextArea belongs). Note also that most layout managers don't even respect a component's size but rather its preferred size.
Instead, set the row and column properties of your JTextAreas (done most easily via the JTextArea(int row, int column) constructor), the column property of your JTextField, the font sizes of other components (if need be). Then allow your container (JPanel) layout managers and component's own preferred sizes size all appropriately when you call pack() on your top-level window (often a JFrame), after adding all components but before setting it visible.
For more specific help, consider posting an image of the GUI you're getting vs. the one you're trying to achieve.

Best practice to resize JScrollPane

I read some answered questions in this forum (this one for example) where it is strictly recommended to avoid the use of setXXXSize() methods to resize components in swing applications.
So, coming to my problem, i would like to know how to best resize a JScrollPane in order to avoid its parent panel to increase its size without any control.
Before writing some code, i want to describe the real situation, since i will post a "toy example".
In my JFrame i'm currently using a border layout for my content pane. At BorderLayout.CENTER there is a JPanel where i do some custom painting.
At BorderLayout.EAST there is a JPanel (say eastPanel) containing some components inside another panel (this panel will be added to eastPanel at BorderLayout.NORTH), and a JScrollPane which contains a JTable (added to eastPanel at BorderLayout.CENTER). This table will have a lot of rows.
Since i want eastPanel's height to be the same as centerPanel's height, i need some way to avoid the JScrollPane to increase its size in order to try to display as much rows as possible.
For now i wasn't be able to find another solution apart from calling setPreferredSize on the eastPanel containing the scrollpane, but i have to admit that i hate this kind of solution.
Sample Code
In this code sample i added some random labels at the north of eastPanel and inside the JScrollPane, since my purpose was to post a short sample of code.
However, the situation is very similar to the one i have described above.
I wasn't be able to solve my problem without using this "terrible" line of code :
eastPanel.setPreferredSize(new Dimension(eastPanel.getPreferredSize().width, centerPanel.getPreferredSize().height));
I would like to avoid a more complex layout for a simple situation like this. Am i missing something ? Also, is setting that empty border an acceptable way to set the size of the panel where i will do some custom painting?
Code :
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class Test
{
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
new TestFrame().setVisible(true);
}
catch(Exception exception) {
JOptionPane.showMessageDialog(null, "Fatal error while initialiing application", "Error", JOptionPane.ERROR_MESSAGE);
}
}
});
}
}
class TestFrame extends JFrame
{
public TestFrame() {
super("Test");
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel pane = new JPanel(new BorderLayout(20, 0));
pane.setBorder(new EmptyBorder(20, 20, 20, 20));
JPanel centerPanel = new JPanel();
centerPanel.setBackground(Color.WHITE);
centerPanel.setBorder(new EmptyBorder(400, 400, 0, 0));
// centerPanel.setPreferredSize(new Dimension(400, 400));
JPanel eastPanel = new JPanel(new BorderLayout(0, 20));
JPanel labelsContainer = new JPanel(new GridLayout(0, 1));
for(int i=0;i<7;i++) labelsContainer.add(new JLabel(String.valueOf(i)));
eastPanel.add(labelsContainer, BorderLayout.NORTH);
JPanel moreLabelsContainer = new JPanel(new GridLayout(0, 1));
for(int i=7;i<70;i++) moreLabelsContainer.add(new JLabel(String.valueOf(i)));
JScrollPane scroll = new JScrollPane(moreLabelsContainer, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
eastPanel.add(scroll, BorderLayout.CENTER);
eastPanel.setPreferredSize(new Dimension(eastPanel.getPreferredSize().width, centerPanel.getPreferredSize().height));
pane.add(centerPanel, BorderLayout.CENTER);
pane.add(eastPanel, BorderLayout.EAST);
setContentPane(pane);
pack();
setLocationRelativeTo(null);
}
}
Thanks for your help !
I am not aware of a layout manager that restricts the height of the panel based on the height of a specific component in the panel.
One way is to customize the behaviour of the parent panel that contains the two child components.
The code might be something like:
#Override
public Dimension getPreferredSize()
{
Dimension d = super.getPreferredSize();
BorderLayout layout = (BorderLayout)getLayout();
Component center = layout.getLayoutComponent(BorderLayout.CENTER);
int centerHeight = center.getPreferreidSize().height;
if (d.height > centerHeight)
d.height = centerHeight;
return d;
}
This approach will allow for dynamic calculation of the height based on the component in the center.
Another option is to write you own layout manager. Then you can control this type of logic from within the layout manager.
Also, is setting that empty border an acceptable way to set the size of the panel where i will do some custom painting?
I override the getPreferredSize() to return the appropriate dimension.
By using the EmptyBorder you lose the ability to add a true Border to the panel, so I wouldn't recommend it.

JLayeredPane display problems

I am working on a little card game and I have been having some trouble: when I try to add dynamic components to my JLayeredPane it does not display them.
I have a custom component that represents a card and I want to display 2 of them in a layered fashion. For that I have the following class:
public class PairView extends JPanel {
private JLayeredPane layeredPane;
private CardView attackCard;
private CardView defenceCard;
private static Point origin = new Point(0, 0);
private static int offset = 10;
public PairView() {
}
public PairView(Card attackCard) {
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
layeredPane = new JLayeredPane();
layeredPane.setPreferredSize(new Dimension(120, 170));
this.defenceCard = null;
this.attackCard = new CardView(attackCard);
this.attackCard.setOpaque(true);
this.attackCard.setForeground(Color.black);
this.attackCard.setBorder(BorderFactory.createLineBorder(Color.black));
this.attackCard.setBounds(origin.x, origin.y, 100, 150);
layeredPane.add(this.attackCard, 0);
this.origin.x += offset;
this.origin.y += offset;
}
public void addDefenceCard(Card defenceCard) throws DurakException {
if (this.defenceCard == null) {
this.defenceCard = new CardView(defenceCard);
this.defenceCard.setOpaque(true);
this.defenceCard.setForeground(Color.black);
this.defenceCard.setBorder(BorderFactory.createLineBorder(Color.black));
this.defenceCard.setBounds(origin.x, origin.y, 100, 150);
layeredPane.add(this.defenceCard, 1);
} else {
throw new DurakException("A defence Card is already present");
}
}
I tested this via the drag and drop interface in NetBeans and I have the following problem:
From what I understand, the default constructor is always called, so when I create 2 random CardView components and add them to the layered pane in the default constructor the parent JFrame display them just fine.
If I use the overwritten constructor or try to add another component (like the addDefenceCard method) it does not display the added component.
calling revalidate() or repaint() isn't doing anything.
How to get the components to show?
Your class extend JPanel, but you never add any components to the panel so there is nothing to display.
You need to add the JLayeredPane to the panel:
layeredPane = new JLayeredPane();
this.add( layeredPane );
I don't know if you need the set the layout to a BoxLayout. The default FlowLayout of the panel will respect the preferredSize of any component added to it.
this.attackCard.setOpaque(true);
this.attackCard.setForeground(Color.black);
this.attackCard.setBorder(BorderFactory.createLineBorder(Color.black));
You may want to consider setting these properties in the constructor of the CardView class. That way the default properties are set in one place and can easily be changed.
Instead of using a layered pane you may want to consider using the Overlap Layout. It was designed to support the requirement of overlapping components.

JTable Not Appearing

Whenever I set the panel's Layout to FlowLayout, the JTable appears, however my imageBackground and buttons are misplaced. And when I set the layout to null, the the table doesn't appear, but the buttons and imageBackground are where I wanted them to be. What am I'm going to do with this?
public class AssetPanel extends JPanel implements ActionListener{
private ArrayList<AssetDetails> assetList;
private Frame frame;
private Database db;
private JTable assetTable;
private JScrollPane scrollPane;
private JButton btnBack;
private JButton btnView;
public AssetPanel (Frame frame){
super();
this.frame = frame;
initialize();
}
public void initialize(){
setName("Assets");
setSize(700, 475);
setLayout(null);
setVisible(true);
db = new Database();
btnView = new JButton("View");
btnView.addActionListener(this);
btnView.setBounds(450, 400, 90, 20);
add(btnView);
btnBack = new JButton("Back");
btnBack.setFont(new Font("Tahoma", Font.BOLD, 12));
btnBack.setBounds(550, 400, 90, 20);
btnBack.addActionListener(this);
add(btnBack);
ImageIcon imageBackground = new ImageIcon(AssetPanel.class.getResource("/resources/assets.png"));
JLabel jlBackground = new JLabel(imageBackground);
jlBackground.setBounds(0,0, 700, 475);
add(jlBackground);
initializeTable();
}
#Override
public void actionPerformed(ActionEvent ae) {
if(ae.getSource() == btnBack){
frame.changePanel("Main Menu");
}
}
public void initializeTable(){
Object[][] assetData;
assetList = new ArrayList<>();
String[] columnNames = {"Asset Name", "Date Acquired", "Type", "Classification"};
assetList = db.getAssetTable();
assetData = new Object[assetList.size()][columnNames.length];
for(int i = 0; i < assetList.size(); i++){
assetData[i][0] = assetList.get(i).getAssetName();
assetData[i][1] = assetList.get(i).getDateAcquired();
assetData[i][2] = assetList.get(i).getType();
assetData[i][3] = assetList.get(i).getClassification();
}
assetTable = new JTable(assetData, columnNames);
assetTable.setPreferredScrollableViewportSize(new Dimension(400, 100));
assetTable.setLocation(150, 100);
assetTable.setFillsViewportHeight(true);
scrollPane = new JScrollPane(assetTable);
add(scrollPane);
}
}
Don't use a null layout or use the setBounds() method to position and size components.
however my imageBackground and buttons are misplaced
A background is a Container component. That is you create it as a component and paint an image as the background. Then you add other components to the background component. Now the image will appear in the background and the other components appear on top of it.
See the Background Panel to give an example of creating a background component.
On possible solution: I recommend switching to Mig Layout as a solution to all java layout problems. I now use it for the layout of every single container component in my apps. If you switch you'll probably be glad you did (will never again have problems like that listed in this question).
http://www.miglayout.com/
MigLayout may be included in the JDK in a future version of java.
null layouts mean you have to explicitly place all the components.
I recommend BoxLayout. it's really simple, and you can put in spacers to create space between objects, and glue to fill in all remaining space.
you can also nest the boxes, as well.
if you look at java sample code (and at the source for things);
they nest a lot of JPanels to get the complicated layouts.
Try adding this before trying the steps below if it does not work:
// Set your flow layout thus:
setLayout(new FlowLayout(FlowLayout.LEFT,5,5));
// Set your table Auto Resize Mode to OFF
assetTable.setAutoResizeMode(assetTable.AUTO_RESIZE_OFF);
EXTRA: Try if above tips does not help
Technically, your class should extend a JFrame.
Add a root layout to the class(i.e. the JFrame):
setLayout(new FlowLayout(FlowLayout.LEFT,5,5));
Create two panels; one should contain your label and buttons components.
The other should contain the JScrollPane that contains your table.
Both panels can have their Layout which determines how the components will be laid out.
You can use FlowLayout.
Then you can add both panels to the mother layout (JFrame).

My JPanels doesn't show as i want them

Im trying to make the look of my application to look something like this:
But when i try to make this i get something like this:
Here is the code i use to create my two JPanels and how i add the buttons and soo on..
//This is the panel that shows the image
appletRunningPanel = new ImagePanel();
appletRunningPanel.setSize(600, 300);
appletRunningPanel.validate();
//This is the panels that shows the 3 buttons
appletRunningPanel2 = new Panel(new GridLayout(1, 3));
appletRunningPanel2.setSize(600, 300);
appletRunningPanel2.add(test1);
appletRunningPanel2.add(test2);
appletRunningPanel2.add(test3);
appletRunningPanel2.validate();
//Then i add them to the applet with this:
add(appletRunningPanel);
add(appletRunningPanel2);
Here is the code for ImagePanel
public class ImagePanel extends JPanel{
private BufferedImage image;
public ImagePanel() {
setSize(600, 300);
try {
image = ImageIO.read(getClass().getResourceAsStream("/res/TCHLogo.png"));
} catch (IOException ex) {
// handle exception...
System.out.println(ex);
}
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, null);
}
}
GridLayout will stretch the component in each cell to fit the size of the cell, if you want to avoid this stretching then add the Buttons to another Panel and add that panel to the GridLayout.
Laying out a user interface in Java, in my experience, is all about mixing and matching Layout managers to achieve your overall goal. There are some simplified managers out there like Mig Layout
For you example I'd do something like this:
+----------------------------------------+
| panel1 |
|+--------------------------------------+|
|| panel2 ||
|+--------------------------------------+|
+----------------------------------------+
+----------------------------------------+
| panel3 |
|+-----------++-----------++------------+|
|| panel4 || panel5 || panel6 ||
|+-----------++-----------++------------+|
+----------------------------------------+
panel1 has a BorderLayout
panel2 is your ImagePanel and added to panel1 via panel1.add(panel2, BorderLayout.CENTER);
panel3 is your GridLayout.
panel4, panel5, and panel6 are all default (FlowLayout) and each of these JPanels will contain one of your three buttons.
You can then add this to the Content Pane by setting it's Layout to BorderLayout and adding panel1 via getContentPane().add(panel1, BorderLayout.NORTH); and panel3 via getContentPane().add(panel3, BorderLayout.SOUTH);
It's not perfect, but it'll achieve a cleaner look for you. There is a lot more that you can add to make things appear nicer. One of my favorite layout managers is BoxLayout.
You need to better understand Layout Managers. Have a look through this handy guide and pick a layout manager that suits your need.
On the bottom panel, GridLayout doesn't respect the preferred size of the components (JButtons), it uses all the space available in the grid section.
You may need to use different layout managers for each panel, and another one again for your applet frame.
Try to add an intermediate JPanel
i.e
JPanel appletRunningPanel2Wrapper = new JPanel();
appletRunningPanel2Wrapper.add(appletRunningPanel2);
add(appletRunningPanel2Wrapper);

Categories