GridLayout, split 2nd row into two equal parts - java

I am using the above layout. All i want is split the second row into two equal and half parts, where I can have a jlabel "enter:" on the left and a jtextfield on the right. How can I accomplish this? I use:
GridLayout gl = new GridLayout(2,1);
setLayout(gl);
JButton jb = new JButton("Click Me!");
jb.setFocusPainted(false);
add(jb);
JLabel jl = new JLabel("Enter:");
JTextField jt = new JTextField();
add(jl);

The simplest solution is to put another JPanel in the bottom row, which is configured to use GridLayout.
Code (tested):
this.setLayout(new GridLayout(2,1));
JButton button = new JButton("Click Me!");
JPanel bottomPanel = new JPanel(new GridLayout(1,2));
JLabel label = new JLabel("Enter:");
JTextField textField = new JTextField();
bottomPanel.add(label);
bottomPanel.add(textField);
add(button);
add(bottomPanel);

Related

How to remove blank space between image and text inside the JPanel?

I'm trying to develop a little application for my Java class. I'm using jsoup to get information from an URL.
I finally got everything, but I don't know how to remove this huge blank between the images and the text. Any advice?
JFrame jf4 = new JFrame("¡¡NEWS WITH PICTURE!!");
JPanel p3 = new JPanel(new BorderLayout());
p3.setBorder(new EmptyBorder(5, 5, 0, 0));
p3.setLayout(new GridLayout(90, 2, 5, 5));
for (Element link: pictures) {
Element picture = link.select("source[media=(max-width: 48em)]").first();
Element text = link.select("img").first();
//System.out.println(picture);
//System.out.println(picture.attr("data-original-set"));
try {
JLabel label3 = new JLabel();
label3.setIcon(new ImageIcon(new ImageIcon(new URL(picture.attr("data-original-set"))).getImage().getScaledInstance(300, 300, Image.SCALE_DEFAULT)));
p3.add(label3);
JLabel label4 = new JLabel(text.attr("alt"));
p3.add(label4);
} catch (Exception exp) {
exp.printStackTrace();
System.out.println(exp);
}
} // IN CASE OF ERROR OF THE URL IT PRINTS java.net.MalformedURLException: no protocol: LINK TRIED
JScrollPane panelPane2 = new JScrollPane(p3);
jf4.getContentPane().add(panelPane2);
jf4.pack();
jf4.setVisible(true);
jf4.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Finally thanks to #prasad_ I get the solution.
I follow his advice. Instead of creating a new JLabel I use the propiertie setText on the same label.
Instead of this:
JLabel label3 = new JLabel();
label3.setIcon(new ImageIcon(new ImageIcon(new URL(picture.attr("data-original-set"))).getImage().getScaledInstance(300, 300, Image.SCALE_DEFAULT)));
p3.add(label3);
JLabel label4 = new JLabel(text.attr("alt"));
p3.add(label4);
I do this:
JLabel label3 = new JLabel();
label3.setIcon(new ImageIcon(new ImageIcon(new URL(picture.attr("data-original-set"))).getImage().getScaledInstance(300, 300, Image.SCALE_DEFAULT)));
label3.setText(text.attr("alt"));
p3.add(label3);
So finally, the blank disappear.

Positioning two panels on JFrame [duplicate]

This question already has an answer here:
JAVA positioning labels on JFRAME
(1 answer)
Closed 5 years ago.
I'm trying to get an output like this (designed with Netbeans designer), where I need to actually design it by code:
Where the layout of the JFrame should be like this:
JFrame frame = new JFrame("Horizontal Histogram");
frame.setVisible(true);
frame.setSize(400, 300);
frame.setResizable(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(5, 1));
panel.setBorder(new EmptyBorder(10, 10, 10, 10));
frame.add(panel);
JPanel panel_2 = new JPanel();
panel_2.setLayout(new GridLayout(5, 1));
panel_2.setBorder(new EmptyBorder(10, 10, 10, 10));
frame.add(panel_2);
JLabel label_1 = new JLabel("0-29");
JLabel label_2 = new JLabel("30-39");
JLabel label_3 = new JLabel("40-69");
JLabel label_4 = new JLabel("70-100");
JLabel stats_1 = new JLabel(); //number of stars
JLabel stats_2 = new JLabel();
JLabel stats_3 = new JLabel();
JLabel stats_4 = new JLabel();
stats_1.setText(stars); //starts is a string like ("***")
stats_2.setText(stars);
stats_3.setText(stars);
stats_4.setText(stars);
panel.add(label_1);
panel.add(label_2);
panel.add(label_3);
panel.add(label_4);
My code below only shows the stars, in one entire column. If I remove the second panel and add the 'stats labels' to the first panel it shows a 2 x 4 grid layout like this:
Any ideas on how to get an output like the first image I've posted?
JFrame uses by default BorderLayout.
This: frame.add(panel); adds panel to BorderLayout.CENTER
This: frame.add(panel_2); adds panel_2 to BorderLayout.CENTER
The problem is that BorderLayout.CENTER can hold one component only.
Use:
frame.add(panel, BorderLayout.WEST); and frame.add(panel_2, BorderLayout.EAST);
To get better insight of layouts read A Visual Guide to Layout Managers.

jTextArea append is not working

I have a scrollpane in a panel and a jtextarea under the scrollpane. jtextarea append is not working
I am using this for logging purpose.
JPanel panel_1 = new JPanel();
tabbedPane.addTab("Logs", null, panel_1, null);
panel_1.setLayout(null);
JScrollPane scrollPane_1 = new JScrollPane();
scrollPane_1.setBounds(0, 0, 672, 303);
panel_1.add(scrollPane_1);
JTextArea jTextArea = new JTextArea(100,200);
jTextArea.setEditable(false);
jTextArea.setVisible(true);
scrollPane_1.add(jTextArea);
jTextArea.append("Hello");
scrollPane_1.add(jTextArea);
Don't add components to a scrollpane. The component needs to be added to the viewport of the scrollpane.
The easiest way to do this is to use:
JTextArea jTextArea = new JTextArea(100,200);
jTextArea.setEditable(false);
jTextArea.setVisible(true);
//scrollPane_1.add(jTextArea);
jTextArea.append("Hello");
JScrollPane scrollPane_1 = new JScrollPane(jTextArea);
scrollPane_1.setBounds(0, 0, 672, 303);
panel_1.add(scrollPane_1);
The other way to do this is to use:
JScrollPane scrollPane_1 = new JScrollPane();
scrollPane_1.setViewportView( jTextArea );
Also you should note when you create a text area the parameters are row/columns, not pixels so your values are too large. I would suggest something like:
//JTextArea jTextArea = new JTextArea(100,200);
JTextArea jTextArea = new JTextArea(30,10);
Finally you should not be setting the bounds of the scrollpane. Swing was designed to be used with layout managers. The layout manager will determine the size of the scrollpane based on the size of the text area:
//scrollPane_1.setBounds(0, 0, 672, 303);

how to create 2 content panels in north widget

can we create 2 content panels in north widget.
BorderLayoutContainer con = new BorderLayoutContainer();
ContentPanel cp = new ContentPanel();
VerticalLayoutContainer logoLayout = new VerticalLayoutContainer();
BorderLayoutData d = new BorderLayoutData(.20);
d.setMargins(new Margins());
Image logo = new Image("/IMAGES/Logo.png");
logoLayout.add(logo);
cp.add(logoLayout);
cp.setHeaderVisible(false);
con.setNorthWidget(cp, d);
please suggest me how to create two content panels.
Basically what I need to do is - please look into the image and let me know what I can do for that
You create your two panels within a single panel, and then assign that single panel to NORTH. Remember that your overall layout can be created from nested layouts.
ContentPanel cp = new ContentPanel();
JPanel panelA = new JPanel();
JPanel panelB = new JPanel();
JPanel panelBig = new JPanel();
panelBig.add(panelA);
panelBig.add(panelB);
cp.add(panelBig, BorderLayout.NORTH);
I think you can probably work out the rest of the details on your own.

Difficulty with JTabbedPane (erroneously compressing content)

I have a GUI built with Java's Swing. I have a JPanel that holds all of my controls:
JPanel rightSidePanel = new JPanel(new GridBagLayout());
rightSidePanel.add(forwardKinematics, new GridBagConstraints());
This works fine. However, I would like to add a tabbed control. Unfortunately, adding the tabs breaks the layout:
JTabbedPane tabs = new JTabbedPane();
tabs.addTab("Forward Kinematics", forwardKinematics);
JPanel rightSidePanel = new JPanel(new GridBagLayout());
rightSidePanel.add(tabs, new GridBagConstraints());
Now, some of the controls in forwardKinematics are horribly crunched up, instead of expanding to their full size. Is there some way that I'm supposed to specify the controls can expand and take up as much space as they want?
I'm using gridbag layouts.
Here is the code that creates the UI controls that get squished (with irrelevant excerpts removed):
panel.add(label, new GridBagConstraints());
GridBagConstraints gbc = new GridBagConstraints();
final DefaultTableModel model = new DefaultTableModel();
model.addColumn("foo");
model.addColumn("bar");
final JTable table = new JTable(model);
JScrollPane scrollPane = new JScrollPane(table);
table.setFillsViewportHeight(true);
gbc = new GridBagConstraints();
gbc.gridy = 2;
gbc.ipady = 10;
gbc.ipadx = 10;
panel.add(scrollPane, gbc);
final JComboBox comboBox = new JComboBox();
gbc = new GridBagConstraints();
gbc.gridy = 1;
panel.add(comboBox, gbc);
JButton makeNewButton = new JButton("Make new from current state");
gbc = new GridBagConstraints();
gbc.gridy = 1;
panel.add(makeNewButton, gbc);
JButton deleteButton = new JButton("Delete Current Keyframe");
gbc = new GridBagConstraints();
gbc.gridy = 2;
panel.add(deleteButton, gbc);
JButton playAll = new JButton("Play All");
gbc = new GridBagConstraints();
gbc.gridy = 2;
panel.add(playAll, gbc);
What am I doing wrong here?
Update: I tried using tabs.setPreferredSize(). That makes the area surrounding the tab bigger (as if I'm adding padding), but the content within them stays squished as ever.
Default constructed GridBagConstraints uses NONE for fill. In your case you probably want to use BOTH instead.
If you want to use GridBagLayout, at least read the basic tutorial.
You seem to be missing a LOT of settings you probably want to set on your gridbag constraints. You're setting everything to be in gridy = 2, which puts them all in the same cell.
The gridbaglayout operates like a table with configurable rows and columns.

Categories