Netbean java gui - java

I am new to JFrame and I am having problems with the layout in NetBean, how to solve this layout problem? I tried to resize, but the position of the elements will become mess. I am trying to display each detail to the specified text areas .
Am i using the wrong layout or my coding are having problems? help...><
This is my output now
And this is the output i want
These are my codings :
public class PokemonJournal extends JFrame
implements ActionListener {
JTextArea FirstName = new JTextArea(1, 10);
JTextArea LastName = new JTextArea(1, 10);
JTextArea Level = new JTextArea(1, 10);
JTextArea Gender = new JTextArea(1, 10);
JButton showPokemon = new JButton("Show");
JButton showRaid = new JButton("Show");
JButton showPokestop = new JButton("Show");
DBHandler db = new DBHandler();
ImageIcon image = new ImageIcon("p1.jpg");
JLabel imageLabel = new JLabel(image);
public static void main(String[] args) {
PokemonJournal jf = new PokemonJournal();
}
public PokemonJournal() {
setLayout(new FlowLayout());
setSize(300, 600);
setTitle(" Pokemon Journal ");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setLocationRelativeTo(null);
setResizable(false);
JPanel top = new JPanel();
add("north", top);
top.add(new Label("Pokemon Journal"));
JPanel side = new JPanel();
add("West", side);
side.setLayout(new GridLayout());
side.add(imageLabel);
JPanel line1 = new JPanel();
add("center", line1);
line1.setLayout(new GridLayout());
line1.add(new Label("First Name :"));
line1.add(FirstName);
JPanel line2 = new JPanel();
add("Center", line2);
FirstName.setEditable(false);
line2.setLayout(new GridLayout());
line2.add(new Label("Last Name :"));
line2.add(LastName);
LastName.setEditable(false);
JPanel line3 = new JPanel();
add("Center", line3);
line3.setLayout(new GridLayout());
line3.add(new Label("Level :"));
line3.add(Level);
Level.setEditable(false);
JPanel line4 = new JPanel();
add("Center", line4);
line4.setLayout(new GridLayout());
line4.add(new Label("Gender : "));
line4.add(Gender);
Gender.setEditable(false);
JPanel line5 = new JPanel();
add("Center", line5);
line5.setLayout(new GridLayout());
line5.add(new Label("Pokemon Caught:"));
line5.add(showPokemon);
JPanel line6 = new JPanel();
add("Center", line6);
line6.setLayout(new GridLayout());
line6.add(new Label("Raid Won:"));
line6.add(showRaid);
JPanel line7 = new JPanel();
add("Center", line7);
line7.setLayout(new GridLayout());
line7.add(new Label("Pokestop visited:"));
line7.add(showPokestop);
}

Am i using the wrong layout or my coding are having problems?
Well, yes, a little, but it has more to do with the size of your JFrame, never call setSize() on them, instead call pack(). However if you change it for pack(), the default orientation of the FlowLayout is FlowLayout.HORIZONTAL and thus all your components will go to the right of each other.
Then, you have 2 options:
Keep the JFrame's layout as FlowLayout but make two JPanels, one for the image and one for the data, the image might have the default FlowLayout as well but the other one might have GridLayout.
Use GridBagLayout for all your components, letting the image JLabel to have a height of 7 cells and all the others a height of 1.
For the following example I will be using the GridBagLayout option, and I recommend you to try to make the multipanel option (option 1).
However, before starting I must mention your other problems in your code:
You're not following the Java naming conventions: firstWordLowerCaseVariable, firstWordLowerCaseMethods(), FirstWordUpperCaseClasses, ALL_WORDS_UPPER_CASE_CONSTANTS, this will make your code easier to read and understand for you and for us.
You're not changing the behavior of the JFrame so, there's no need to inherit from it: See Extends JFrame vs. creating it inside the program for more information.
Related to the above point, it's wise to build your GUI towards JPanels instead of JFrames, see: The Use of Multiple JFrames: Good or Bad Practice? as well, as I think you might end up in similar problems in the future.
You're not placing your program on the Event Dispatch Thread (EDT), see this related answer to see how to solve this problem.
You're calling setSize(...) method, and as I said before, it's always better to call pack() method instead, leaving the calculations for the GUI size to the layout manager.
You're calling setVisible before adding all your components to the GUI, this could lead you to blank or black screens, it should be the last line in your program.
Related to the above point, I always like to start doing the GUI from inside out and adding the items that way, instead of outside in.
Why are you using JTextAreas instead of JTextFields for the user information?
Now, the code that follows the above recommendations using the 2nd option I gave you is this:
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class PokemonJournal {
private JFrame frame;
private JPanel pane;
private GridBagConstraints gbc;
private JLabel imageLabel;
private JLabel firstName;
private JLabel lastName;
private JLabel level;
private JLabel gender;
private JLabel pokemonCaught;
private JLabel raidWon;
private JLabel pokestopVisited;
private JTextField nameField;
private JTextField lastNameField;
private JTextField levelField;
private JTextField genderField;
private JButton pokemonCaughtButton;
private JButton raidWonButton;
private JButton pokestopVisitedButton;
public static void main(String[] args) {
SwingUtilities.invokeLater(new PokemonJournal()::createAndShowGui);
}
private void createAndShowGui() {
frame = new JFrame(getClass().getSimpleName());
pane = new JPanel();
pane.setLayout(new GridBagLayout());
gbc = new GridBagConstraints();
imageLabel = new JLabel();
try {
imageLabel.setIcon(new ImageIcon(
new URL("http://pm1.narvii.com/6535/1b362404d09091a335ce53fa68506827418890a3_128.jpg")));
} catch (MalformedURLException e) {
e.printStackTrace();
}
firstName = new JLabel("First Name: ");
lastName = new JLabel("Last Name: ");
level = new JLabel("Level: ");
gender = new JLabel("Gender: ");
pokemonCaught = new JLabel("Pokemon Caught: ");
raidWon = new JLabel("Raid Won: ");
pokestopVisited = new JLabel("Pokestop Visited: ");
nameField = new JTextField(10);
lastNameField = new JTextField(10);
levelField = new JTextField(10);
genderField = new JTextField(10);
pokemonCaughtButton = new JButton("Show");
raidWonButton = new JButton("Show");
pokestopVisitedButton = new JButton("Show");
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridheight = 7;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(5, 5, 5, 5);
pane.add(imageLabel, gbc);
gbc.gridx = 1;
gbc.gridheight = 1;
pane.add(firstName, gbc);
gbc.gridx = 2;
pane.add(nameField, gbc);
gbc.gridx = 1;
gbc.gridheight = 1;
gbc.gridy++;
pane.add(lastName, gbc);
gbc.gridx = 2;
pane.add(lastNameField, gbc);
gbc.gridx = 1;
gbc.gridheight = 1;
gbc.gridy++;
pane.add(level, gbc);
gbc.gridx = 2;
pane.add(levelField, gbc);
gbc.gridx = 1;
gbc.gridheight = 1;
gbc.gridy++;
pane.add(gender, gbc);
gbc.gridx = 2;
pane.add(genderField, gbc);
gbc.gridx = 1;
gbc.gridheight = 1;
gbc.gridy++;
pane.add(pokemonCaught, gbc);
gbc.gridx = 2;
pane.add(pokemonCaughtButton, gbc);
gbc.gridx = 1;
gbc.gridheight = 1;
gbc.gridy++;
pane.add(raidWon, gbc);
gbc.gridx = 2;
pane.add(raidWonButton, gbc);
gbc.gridx = 1;
gbc.gridheight = 1;
gbc.gridy++;
pane.add(pokestopVisited, gbc);
gbc.gridx = 2;
pane.add(pokestopVisitedButton, gbc);
frame.add(pane);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
And it produces a GUI similar to this one:

Related

How to improve the margin and padding of JPanel?

When I initially run my Java Swing application, the padding and margining of the JLabel and JTextField components in a JPanel look good. But whenever I resize the application, the padding and margining for the labels and text fields increases so much that it does not look as good.
I understand a GridLayout has a set amount of vertical and horizontal spacing, but what can I do to make the spacing look better as the size of the application increases? I would also like the text in the text fields to resize as the text field increases but have been unable to figure it out.
I have been thinking to use a GridBagLayout but have not had much luck with that either. There is just way too much spacing as the size increases, which does not look good, and I have been unable to find or learn a good solution.
I would appreciate any advice.
Code:
package TestPackage;
import java.awt.GridLayout;
import javax.swing.*;
public class TestGridLayout {
public static void main(String[] args) {
JPanel testPanel = new JPanel();
GridLayout layoutManager = new GridLayout(5,2);
testPanel.setLayout(layoutManager);
JLabel platformEnumerationNameLabel = new JLabel();
JLabel logicalEnumerationNameFieldLabel = new JLabel();
JLabel valueTypeUnitNameFieldLabel = new JLabel();
JLabel measurementNameFieldLabel = new JLabel();
JLabel measurementAxisNameFieldLabel = new JLabel();
JTextField platformEnumerationNameField = new JTextField();
JTextField logicalEnumerationNameField = new JTextField();
JTextField valueTypeUnitNameField = new JTextField();
JTextField measurementNameField = new JTextField();
JTextField measurementAxisNameField = new JTextField();
platformEnumerationNameLabel.setText("Label 1");
testPanel.add(platformEnumerationNameLabel);
platformEnumerationNameField.setText("input some data");
testPanel.add(platformEnumerationNameField);
logicalEnumerationNameFieldLabel.setText("Label 2");
testPanel.add(logicalEnumerationNameFieldLabel);
logicalEnumerationNameField.setText("input some data");
testPanel.add(logicalEnumerationNameField);
valueTypeUnitNameFieldLabel.setText("Label 3");
testPanel.add(valueTypeUnitNameFieldLabel);
valueTypeUnitNameField.setText("input some data");
testPanel.add(valueTypeUnitNameField);
measurementNameFieldLabel.setText("Label 4");
testPanel.add(measurementNameFieldLabel);
measurementNameField.setText("input some data");
testPanel.add(measurementNameField);
measurementAxisNameFieldLabel.setText("Label 5");
testPanel.add(measurementAxisNameFieldLabel);
measurementAxisNameField.setText("input some data");
testPanel.add(measurementAxisNameField);
JDialog dialog = new JDialog();
dialog.add(testPanel);
dialog.setSize(700,200);
dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
}
}
Use a different layout manager that doesn't resize all the components (unless you want them to)
See How to Use GridBagLayout for more details
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(8, 8, 8, 8);
gbc.gridx = 0;
gbc.gridy = 0;
JLabel platformEnumerationNameLabel = new JLabel();
JLabel logicalEnumerationNameFieldLabel = new JLabel();
JLabel valueTypeUnitNameFieldLabel = new JLabel();
JLabel measurementNameFieldLabel = new JLabel();
JLabel measurementAxisNameFieldLabel = new JLabel();
JTextField platformEnumerationNameField = new JTextField(20);
JTextField logicalEnumerationNameField = new JTextField(20);
JTextField valueTypeUnitNameField = new JTextField(20);
JTextField measurementNameField = new JTextField(20);
JTextField measurementAxisNameField = new JTextField(20);
platformEnumerationNameLabel.setText("Label 1");
add(platformEnumerationNameLabel, gbc);
gbc.gridx++;
platformEnumerationNameField.setText("input some data");
add(platformEnumerationNameField, gbc);
gbc.gridy++;
gbc.gridx = 0;
logicalEnumerationNameFieldLabel.setText("Label 2");
add(logicalEnumerationNameFieldLabel, gbc);
gbc.gridx++;
logicalEnumerationNameField.setText("input some data");
add(logicalEnumerationNameField, gbc);
gbc.gridy++;
gbc.gridx = 0;
valueTypeUnitNameFieldLabel.setText("Label 3");
add(valueTypeUnitNameFieldLabel, gbc);
gbc.gridx++;
valueTypeUnitNameField.setText("input some data");
add(valueTypeUnitNameField, gbc);
gbc.gridy++;
gbc.gridx = 0;
measurementNameFieldLabel.setText("Label 4");
add(measurementNameFieldLabel, gbc);
gbc.gridx++;
measurementNameField.setText("input some data");
add(measurementNameField, gbc);
gbc.gridy++;
gbc.gridx = 0;
measurementAxisNameFieldLabel.setText("Label 5");
add(measurementAxisNameFieldLabel, gbc);
gbc.gridx++;
measurementAxisNameField.setText("input some data");
add(measurementAxisNameField, gbc);
}
}
}

Component not appearing when adding to panel

So I'm trying to create a series of radio buttons and check boxes that are displayed as follows:
Radio Button
Check Box
Radio Button
Check Box
Radio Button
However, I'm still in the learning process for java and I was wondering if anyone could solve this problem. At the moment the buttons and boxes are being displayed in the correct location, however the first radio button ("Courier") is not being displayed for some reason. If you could perhaps describe the reason and a possible solution that'd be great.
Thanks
Updated Code:
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
public class Question2 {
public static void main(String[] args) {
MyFrame f = new MyFrame("Font Chooser");
f.init();
}
}
class MyFrame extends JFrame {
MyFrame(String title) {
super(title);
}
private JPanel mainPanel;
private GridBagConstraints gbc = new GridBagConstraints();
private GridBagLayout gbLayout = new GridBagLayout();
void init() {
mainPanel = new JPanel();
mainPanel.setLayout(gbLayout);
mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 20, 10, 20));
this.setContentPane(mainPanel);
gbc.gridx = 0;
gbc.gridy = 1;
JCheckBox cb = new JCheckBox("Bold");
gbLayout.setConstraints(cb, gbc);
mainPanel.add(cb);
gbc.gridy = 3;
gbLayout.setConstraints(cb, gbc);
cb = new JCheckBox("Italic");
mainPanel.add(cb);
gbc.gridx = 1;
gbc.gridy = 0;
JRadioButton rb = new JRadioButton("Times");
gbLayout.setConstraints(rb, gbc);
mainPanel.add(rb, gbc);
gbc.gridy = 2;
rb = new JRadioButton("Helvatica");
mainPanel.add(rb, gbc);
rb = new JRadioButton("Courier");
gbc.gridy = 4;
mainPanel.add(rb, gbc);
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
3 issues
Y Coordinate not re-assigned to different value causing last 2 radio buttons to exist at same location
GridBagConstraints not being used for left-hand side components
setConstraints erroneously being used to set constraints
Resultant code:
public class GoodGridBagApp {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame("Font Chooser");
frame.add(getMainPanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
private JPanel getMainPanel() {
JPanel mainPanel = new JPanel();
GridBagConstraints gbc = new GridBagConstraints();
mainPanel.setLayout(new GridBagLayout());
mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 20, 10, 20));
gbc.gridx = 1;
gbc.gridy = 2;
JCheckBox cb = new JCheckBox("Bold");
mainPanel.add(cb, gbc);
gbc.gridy = 4;
cb = new JCheckBox("Italic");
mainPanel.add(cb, gbc);
gbc.gridx = 2;
gbc.gridy = 1;
JRadioButton rb = new JRadioButton("Times");
mainPanel.add(rb, gbc);
gbc.gridy = 3;
rb = new JRadioButton("Helvatica");
mainPanel.add(rb, gbc);
rb = new JRadioButton("Courier");
gbc.gridy = 5;
mainPanel.add(rb, gbc);
return mainPanel;
}
});
}
}
Read: How to Use GridBagLayout

JPanel does not appear in window unless I resize the window

I am new to GUI in java, and have been using this video to learn. When I run the program, the window is blank until I resize it.
public class GUIProgram extends JFrame
{
int screenWidth = 1000; //screenSize.width;
int screenHeight = 800; //screenSize.height;
public GUIProgram()
{
super("DATABASE");
setSize(screenWidth, screenHeight);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel p = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel(new GridBagLayout());
JButton b = new JButton("Button 1");
JButton c = new JButton("Button 2");
p.add(b);
p.add(c);
JCheckBox cb = new JCheckBox("Do you LOVE bacon?");
JCheckBox cb2 = new JCheckBox("Do you LOVE cheese?");
p2.add(cb);
p2.add(cb2);
JLabel label = new JLabel("This is a label");
JTextArea tb = new JTextArea("this is a test area");
JTextField textField = new JTextField("text field");
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(15,15,15,15);
gbc.gridx = 0;
gbc.gridy = 0;
p3.add(label, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
p3.add(tb, gbc);
gbc.gridx = 1;
gbc.gridy = 2;
p3.add(textField, gbc);
add(p, BorderLayout.SOUTH);
add(p3, BorderLayout.CENTER);
add(p2, BorderLayout.NORTH);
}
}
Thanks in advance to anyone who can lend me some advice!
Please let me know if there is some ambiguity in what I'm asking or how I explained myself.
Call setVisible() last, immediately after pack().
Tip:
setSize(screenWidth, screenHeight); Don't set the size of top level containers. Instead layout the content & call pack().

Is it possible to make the elements inside a JPanel with GridBagLayout to start from the top left?

I have a Swing Form that contains a JScrollPane(activityScrollPane) for a JPanel(activityPanel). The panel contains a JTextField and a JButton (that is used to add more fields to the Panel). Now the problem is that the elements start from the center of the panel as in the image below (with the borders marking the activityScrollPane boundary)
Following is the code I am currently using to make the scroll pane and associated components.
//part of the code for creating the ScrollPane
final JPanel activityPanel = new JPanel(new GridBagLayout());
gbc.gridx=0;
gbc.gridy=0;
JScrollPane activityScrollPane = new JScrollPane(activityPanel);
//adding activity fields
activityFields = new ArrayList<JTextField>();
fieldIndex = 0;
activityFields.add(new JTextField(30));
final GridBagConstraints activityGBC = new GridBagConstraints();
activityGBC.gridx=0;
activityGBC.gridy=0;
activityGBC.anchor = GridBagConstraints.FIRST_LINE_START;
activityPanel.add(activityFields.get(fieldIndex),activityGBC);
fieldIndex++;
JButton btn_more = (new JButton("more"));
activityGBC.gridx=1;
activityPanel.add(btn_more,activityGBC);
How can I make the JTextField and the JButton or for that matter any component to appear on the top left corner of the JScrollPane. I have already tried using
activityConstraints.anchor = GridBagConstraints.NORTHWEST;
as pointed in the SO post, but it does not at all seem to work.
You simply forgot to provide any weightx/weighty values, atleast one having a non-zero value will do. have a look at this code example :
import java.awt.*;
import javax.swing.*;
public class GridBagLayoutDemo
{
private JTextField tfield1;
private JButton button1;
private void displayGUI()
{
JFrame frame = new JFrame("GridBagLayout Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel contentPane = new JPanel();
contentPane.setLayout(new GridBagLayout());
tfield1 = new JTextField(10);
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
contentPane.add(tfield1, gbc);
button1 = new JButton("More");
gbc.gridx = 1;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.NONE;
contentPane.add(button1, gbc);
frame.setContentPane(contentPane);
frame.pack();
frame.setVisible(true);
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new GridBagLayoutDemo().displayGUI();
}
});
}
}
Latest EDIT : No spacing along Y-Axis
import java.awt.*;
import javax.swing.*;
public class GridBagLayoutDemo
{
private JTextField tfield1;
private JButton button1;
private JTextField tfield2;
private JButton button2;
private void displayGUI()
{
JFrame frame = new JFrame("GridBagLayout Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel contentPane = new JPanel();
contentPane.setLayout(new GridBagLayout());
tfield1 = new JTextField(10);
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.weightx = 1.0;
//gbc.weighty = 0.2;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
contentPane.add(tfield1, gbc);
button1 = new JButton("More");
gbc.gridx = 1;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.NONE;
contentPane.add(button1, gbc);
tfield2 = new JTextField(10);
gbc.weightx = 1.0;
gbc.weighty = 0.2;
gbc.gridx = 0;
gbc.gridy = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
contentPane.add(tfield2, gbc);
button2 = new JButton("More");
gbc.gridx = 1;
gbc.gridy = 1;
gbc.fill = GridBagConstraints.NONE;
contentPane.add(button2, gbc);
JScrollPane scroller = new JScrollPane(contentPane);
frame.add(scroller);
frame.pack();
frame.setVisible(true);
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new GridBagLayoutDemo().displayGUI();
}
});
}
}
Sorry as my answer me be on the off-side of what you have asked, but why dont you use GroupLayout instead of GridBag Layout, thats much more easier to handle
try it with BorderLayout: controls.setLayout(new BorderLayout()); and then apply it for your JPanel controls.add(yourPanel, BorderLayout.PAGE_START);
I also have problems with GridBagLayout so i solved it with BorderLayout and it works so fine.
So i wrote for your little example:
private void initComponents() {
controls = new Container();
controls = getContentPane();
controls.setLayout(new BorderLayout());
panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
field = new JTextField(20);
c.gridx = 0;
c.gridy = 0;
panel.add(field, c);
one = new JButton("Go!");
c.gridx = 1;
c.gridy = 0;
panel.add(one, c);
controls.add(panel, BorderLayout.PAGE_START);
}
Hope it helps!
I think this could be simple and possible, you can
put Nested JPanel to the JScrollPane
to this JPanel
put JPanels contains JComponent to the GridLayout (notice about scrolling, you have to change scrolling increment)
or use most complex JComponents as
put JPanels contains JComponent as Item to the JList
put JPanels contains JComponent as row to the JTable (with only one Column, with or without TableHeader)
Add one panel at the right and one at the bottom.
Right Panel:
Set Weight X to 1.0.
Set Fill to horizontal.
Bottom Panel:
Set Weight Y to 1.0.
Set Fill to vertical
There may be better ways to that, but this one worked for me.

Java Swing - JLabel width changed when icon or text added?

Same Question, different context
It seems I was too hasty in my accepting before, since the problem is still there. The problem? JLabel takes the liberty of expanding its parent panel when content is added to it.
It's time for reproducing it per "Hovercraft full of eels"-ses suggestion, and here it is:
import java.awt.*;
import javax.swing.*;
public class TestLabel {
public static void main(String[] args) {
// Var inits
JFrame frame;
JPanel panel;
JLabel label;
Container pane;
GridBagConstraints gbc = new GridBagConstraints();
// Frame, content pane, layout inits
frame = new JFrame("Label Tester");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pane = frame.getContentPane();
pane.setLayout(new GridBagLayout());
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
// Add panels (note gbc weighty and fill carries over all instances)
gbc.weightx = 0.3;
gbc.gridx = 0;
gbc.gridy = 0;
panel = new JPanel();
panel.setBackground(Color.GREEN);
frame.add(panel,gbc);
label = new JLabel("THE PANEL IS NOW DISTORTED TO FIT THIS LABEL WHY IS THIS HAPPENING");
//label = new JLabel("");
label.setOpaque(true);
label.setBackground(Color.WHITE);
panel.add(label);
gbc.weightx = 0.7;
gbc.gridx = 1;
gbc.gridy = 0;
panel = new JPanel();
panel.setBackground(Color.RED);
frame.add(panel,gbc);
gbc.weightx = 0.3;
gbc.gridx = 0;
gbc.gridy = 1;
panel = new JPanel();
panel.setBackground(Color.BLUE);
frame.add(panel,gbc);
gbc.weightx = 0.7;
gbc.gridx = 1;
gbc.gridy = 1;
panel = new JPanel();
panel.setBackground(Color.YELLOW);
frame.add(panel,gbc);
frame.pack();
frame.setSize(800,600);
frame.setVisible(true);
}
}
Results:
As you can see, the green panel is forced wider and throws off my whole layout when text (or, in the original question, and icon) is added to it. I want my layout to remain the same weights, regardless of the content. The reason this came up is because I'm trying to add a scaled image as an icon to the label, as seen in the original question.
Incidentally, setPreferredSize() doesn't seem to work.
Is there a way to fix this?
Original Question
My JLabel element expands dramatically when I add an Icon to it. Why is this happening? Here's the applicable portion of the code:
// Show label and BG color
redLabel.setBackground(Color.RED);
redLabel.setOpaque(true);
// Grab stretched image (already loaded elsewhere in the code) and turn to icon
Img = Img.getScaledInstance(redLabel.getWidth(),12,Image.SCALE_REPLICATE);
ImageIcon icon = new ImageIcon(Img);
// This line throws everything off!
//It's commented out in the first pic, and included in the second.
redLabel.setIcon(icon);
As you can see from the first pic, I've got a label (in red) of width W. What I'm trying to do is stretch my icon to width W and put it in the label.
When I do this, the label expands (by exactly 50 pixels, I think) and also squeezes over the left edge (green). Does anyone have any idea why this is happening?
I've tried several things that are too verbose to explain but can't find the problem :-/
Your component expands because it allocates the necessary space for its Icon.
public class JLabelDemo {
private static BufferedImage bi;
public static void main(String[] args) throws IOException{
loadImage();
SwingUtilities.invokeLater(new Runnable(){
#Override
public void run() {
createAndShowGUI();
}
});
}
private static void loadImage() throws IOException{
bi = ImageIO.read(JLabelDemo.class.getResource("../resource/forever-alone.jpg"));
}
private static void createAndShowGUI(){
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel panel = new JPanel();
panel.setBackground(Color.YELLOW);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
final JLabel emptyLabel = new JLabel();
final JLabel textLabel = new JLabel("This label has text only");
final JLabel textAndImageLabel = new JLabel("This label has text and image");
textAndImageLabel.setIcon(new ImageIcon(bi));
panel.add(emptyLabel);
panel.add(textLabel);
panel.add(textAndImageLabel);
frame.add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
System.out.println("Empty label dimensions - " + emptyLabel.getSize());
System.out.println("Text only label dimensions - " + textLabel.getSize());
System.out.println("Image width: " + bi.getWidth() + ", Image height: " + bi.getHeight());
System.out.println("Text and image label dimensions - " +textAndImageLabel.getSize());
}
}
The following is outputted to console:
Empty label dimensions - java.awt.Dimension[width=0,height=0]
Text only label dimensions - java.awt.Dimension[width=129,height=16]
Image width: 194, Image height: 180
Text and image label dimensions - java.awt.Dimension[width=363,height=180]
Consider using a JLayeredPane to add components in layers. There are trips and traps though when doing this in matters of opacity, size and position of components added.
For example,
import java.awt.*;
import javax.swing.*;
public class TestLabel {
private static final Dimension SIZE = new Dimension(800, 600);
public static void main(String[] args) {
GridBagConstraints gbc = new GridBagConstraints();
JPanel defaultPane = new JPanel();
defaultPane.setLayout(new GridBagLayout());
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
// Add panels (note gbc weighty and fill carries over all instances)
gbc.weightx = 0.3;
gbc.gridx = 0;
gbc.gridy = 0;
JPanel panel = new JPanel();
panel.setBackground(Color.GREEN);
defaultPane.add(panel, gbc);
gbc.weightx = 0.7;
gbc.gridx = 1;
gbc.gridy = 0;
panel = new JPanel();
panel.setBackground(Color.RED);
defaultPane.add(panel, gbc);
gbc.weightx = 0.3;
gbc.gridx = 0;
gbc.gridy = 1;
panel = new JPanel();
panel.setBackground(Color.BLUE);
defaultPane.add(panel, gbc);
gbc.weightx = 0.7;
gbc.gridx = 1;
gbc.gridy = 1;
panel = new JPanel();
panel.setBackground(Color.YELLOW);
defaultPane.add(panel, gbc);
defaultPane.setSize(SIZE);
JLabel label = new JLabel("THE PANEL IS NOW DISTORTED TO FIT THIS LABEL WHY IS THIS HAPPENING");
label.setOpaque(true);
label.setBackground(Color.WHITE);
JPanel northPalettePanel = new JPanel();
northPalettePanel.setOpaque(false);
northPalettePanel.add(label);
JPanel palettePanel = new JPanel(new BorderLayout());
palettePanel.setOpaque(false);
palettePanel.setSize(SIZE);
palettePanel.setLocation(0, 0);
palettePanel.add(northPalettePanel, BorderLayout.NORTH);
JLayeredPane layeredPane = new JLayeredPane();
layeredPane.setPreferredSize(SIZE);
layeredPane.add(defaultPane, JLayeredPane.DEFAULT_LAYER);
layeredPane.add(palettePanel, JLayeredPane.PALETTE_LAYER);
JFrame frame = new JFrame("Label Tester");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(layeredPane, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
}
Java swing is pretty old for me but if I remember well, setting a preferred size (setPreferredSize()) sometime solve these kind of problem ... Also try top lay with setMaximumSize and setMinimumSize.
You can maybe find more information in java documentation:
http://download.oracle.com/javase/tutorial/uiswing/layout/using.html#sizealignment
Regards!

Categories