JPanel auto resize - java

Reading through SO questions. Sadly no solution worked for me.
My problem is, I have a JPanel:
contentPane = new JPanel();
contentPane.setName("contentPane");
contentPane.setLayout(new BorderLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100,305, 403);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
This one has fixed elements. When I enter another project from the "projekt" combobox I might have the chance (dependent on the project) that I get added elements. Input fields or comboboxes. These elements are in a different JPanel which lies ontop of the other JPanel.
this.zusatzPanel = new JPanel();
zusatzPanel.setName("zusatzPanel");
this.zusatzPanel.setLayout(null);
this.zusatzPanel.setBounds(11, 301, 270, 713);
When this is the case I need more space (in this case I have enough space but there could be more elements added), but I found no clue how to make the JPanel resize. I believe that I need to make both resizeable.
with too many added elements:
I also tried with contentPane.setLayout(new BorderLayout()); but that did not do anything.
EDIT: tried jframe.pack(); changed the Jpanel to make is smaller to test it out. But did not work. Or did i do something wrong?
this.pack();
contentPane = new JPanel();
contentPane.setName("contentPane");
contentPane.setLayout(new CardLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100,305, 203);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);

Related

How to have gap between the frame and a component?

I'm trying practice my GUI and I am having troubles putting gap between component and the frame.
The picture above is what I have so far. But I really want to put a gap between the left side of the frame and "label1".
private void initialize() {
frame = new JFrame();
frame.setTitle("WINDOW");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 300);
panel = new JPanel();
panel.setLayout(new BorderLayout());
bottomPanel = new JPanel();
bottomPanel.setLayout(new GridLayout(1, 5));
l1 = new JLabel("Label1");
l2 = new JLabel("Label2");
l3 = new JLabel("Label3");
l4 = new JLabel("Label4");
l5 = new JLabel("Label5");
bottomPanel.add(l1);
bottomPanel.add(l2);
bottomPanel.add(l3);
bottomPanel.add(l4);
bottomPanel.add(l5);
panel.add(bottomPanel, BorderLayout.SOUTH);
frame.add(panel);
}
Above is part my code. I tried doing:
bottomPanel.setLayout(new GridLayout(1, 5, -20, 0));
to put some horizontal gap but that only added gap between the components. That didn't move "label1" away from the frame at all. Is there any other way of doing this? I am very new to Java so I don't really know much of the other tricks. I would appreciate any help! Thank you!
The other answers are fudges that won't achieve the desired effect when the GUI is resized. Instead use:
JLabel.setHorizontalAlignment(SwingConstants.CENTER);
By centering the text within the JLabel, combined with GridLayout stretching the components to the full width of the cell, each label will have as much space either side as the GUI can allow. E.G. here is the effect when the GUI is at minimum size.
And when stretched wider:
(The red border is added to show the bounds of each label.)
Add a Border to the panel:
bottomPanel = new JPanel();
bottomPanel.setBorder( new EmptyBorder(0, 10, 0, 0) );
Read the section from the Swing tutorial on How to Use Borders for more information about the different borders you can create.
Try the following:
bottomPanel.add(javax.swing.Box.createHorizontalStrut(10));

How to change the height of a JComboBox?

I have a class extending JFrame and I want to add a JComboBox on a panel. When I did this, the JComboBox size is very large. I want to change its size, but don't know how to do this. .setSize() and .setPreferredSize() are not working. The layout of the JFrame must be GridLayout.
public CountryController() {
setLayout(new GridLayout(1, 3));
panel1.setPreferredSize(new Dimension(180, 600));
//panel1.setLayout(new FlowLayout(FlowLayout.LEFT));
panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));
panel1.setBorder(BorderFactory.createMatteBorder(0, 0, 0, 1, Color.BLACK));
panel1.add(new JLabel("Some text:"));
//list countries
JScrollPane scrollPane = new JScrollPane(jDropDown);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
jDropDown.setOpaque(true);
jDropDown.setBorder(BorderFactory.createMatteBorder(0, 1, 0, 1, Color.BLACK));
jDropDown.setBackground(Color.LIGHT_GRAY);
panel1.add(scrollPane);
getContentPane().add(panel1);
}
What I have:
What I want:
My solution is the method setMaximumSize(new Dimension(150, 25)). In BoxLayout components are added with max size and this method helps me to change the max size.

Image does not appear

Im trying to use JLabel to insert a image into my GUI. However it does not appear
Here is a partial of my code
public FirstAid() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 700, 507);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0, 0));
tabbedPane = new JTabbedPane(JTabbedPane.TOP);
contentPane.add(tabbedPane, BorderLayout.CENTER);
panel = new JPanel();
ImageIcon imageIcon = new ImageIcon("src/method_1.png");
JLabel label = new JLabel(imageIcon);
panel.add(label);
tabbedPane.addTab("name", null, panel, null);
You create a JPanel called panel, add a JLabel to it, but do nothing with the panel variable after this. You must add it to your GUI for the JLabel and the image it might hold to be seen. I'm guessing that you want to add panel to your JTabbedPane, but without more information, I can only guess.
Edit: your edited question now shows that you're adding the JPanel to the JTabbedPane. If you're still not seeing the image, then the problem is likely in your reading in of the image. Myself, I use ImageIO.read(...) and try to read the image in as an InputStream or as a URL. Key to all of these methods is to make sure that you have the image path correct, and don't make any assumptions about how you might think that it is correct. The only way to know for sure is to test it.

java method call on multiple objects

I installed a plugin for eclipse that helps creating a JFrame in a UI.
The code generated form the plugin has some strange syntax.
I never ever saw something like this in java:
private JPanel b_,cb_,pb_,l_,tf_,ta_contentPane;
public Mainf() {
b_,cb_,pb_,l_,tf_,ta_contentPane = new JPanel();
b_,cb_,pb_,l_,tf_,ta_contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
b_,cb_,pb_,l_,tf_,ta_contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(b_,cb_,pb_,l_,tf_,ta_contentPane);
}
how is that even possible? ^^
The standard java compiler sees that as a syntax error.
Is there a option to compile this right?
Edit:
I found it again (^^). All of these tools use this syntax.
URL for Eclipse: Help->Install new Software...
WindowBuilder Pro Eclipse Update Site - http://download.eclipse.org/windowbuilder/WB/integration/4.3/
and the Website:
http://www.eclipse.org/windowbuilder/
No, there is no option to compile that right.
private JPanel b_,cb_,pb_,l_,tf_,ta_contentPane;
Is a correct sentence.
b_,cb_,pb_,l_,tf_,ta_contentPane = new JPanel();
b_,cb_,pb_,l_,tf_,ta_contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
b_,cb_,pb_,l_,tf_,ta_contentPane.setLayout(new BorderLayout(0, 0));
Are incorrect sencences.
The code is correct but the variable name generated is a not a valid identifier. The solution is to rename the variable and the code will compile fine.
private JPanel contentPane;
public Mainf() {
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
}

Vertical alignment of components of jpanels using GridLayout

I am making KenKen as my term project using java swing library. For alignment I have used gridbag and gridlayout, But now i want to add one more component of JPanel to the UI. These screenshots will make the problem more clear:
Now I select the grid cell to which i want to add respective candidates of in the left most panel.
It disturbs the adjacent alignments of the grid and panels.
Here are the panels with their respective layouts:
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(1, 4, 5, 5));
buttonPanel.setPreferredSize(new Dimension(20,40));
buttonPanel.add(undoButton);
buttonPanel.add(redoButton);
buttonPanel.add(eraseButton);
buttonPanel.add(hintButton);
JPanel cellPanel = new JPanel();
cellPanel.setName("cellPanel");
cellPanel.setLayout(new GridLayout(pSize, pSize, 0, 0));
JPanel numPanel = new JPanel();
numPanel.setName("numPanel");
numPanel.setLayout(new GridLayout(1,1,5,5));
numPanel.setPreferredSize((new Dimension(50,60)));
JPanel candPanel = new JPanel();
candPanel.setName("candidatesPanel");
JLabel candidates = new JLabel("Candidates");
candidates.setFont(new Font("Courier New", Font.ITALIC, 14));
candidates.setForeground(Color.GRAY);
candPanel.setLayout(new GridLayout(0,1));
candPanel.add(candidates);
Then it all goes into the content panel:
content.add(buttonPanel, pos.nextCol().expandW());
content.add(candPanel, pos.nextRow());
content.add(new Gap(GAP) , pos.nextRow()); // Add a gap below
content.add(cellPanel, pos.nextCol());
content.add(numPanel,pos.nextCol().expandW());
The buttons are all generated on runtime, and they are added to the candPanel in an action listener.
You appear to be using a GridBagConstraints subclass of which I am unaware (variable pos), though I can guess its function from context.
Assuming your problem is that you want the candidates panel to be to the left of the cellPanel, and not above it, you need to swap the lines which add the candPanel and the new Gap(GAP) as follows:
content.add(buttonPanel, pos.nextCol().expandW());
content.add(new Gap(GAP), pos.nextRow()); // These two lines
content.add(candPanel, pos.nextRow()); // swapped over
content.add(cellPanel, pos.nextCol());
content.add(numPanel,pos.nextCol().expandW());

Categories