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.
Related
I have a program which i need to get input from the user through a Jframe window.
Its like a while loop, where i invoke the Jframe window and get input from the user. But the JFrame window doesnt wait for input and run the loop until the last time it should run and only there waits for input.
I would like to make the Jframe window wait for input at each iteration of the while loop. Is there any way to do this ?
...
/**
* Create the frame.
*/
public Janela3(KieSession kSession) {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 462, 447);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
kSession.setGlobal("J3", this);
contentPane.setLayout(null);
JPanel panelMain = new JPanel();
panelMain.setBackground(new Color(248, 148, 6));
panelMain.setBounds(0, 0, 448, 44);
contentPane.add(panelMain);
tituloJanela = new JLabel();
tituloJanela.setFont(new Font("Tahoma", Font.BOLD, 24));
tituloJanela.setForeground(new Color(255, 255, 255));
panelMain.add(tituloJanela);
kSession.setGlobal("TJ3", tituloJanela);
JPanel childPugh = new JPanel();
childPugh.setBounds(0, 44, 448, 364);
...
}
If I'm correct, you have a while loop in which you get user input but the loop may not continue if there is no input yet? Currently the information you provide is too limited to give a concrete answer but I'll give it a shot.
You could solve this by using another while loop that waits for input. Check 'the pseudo-semi-real code' below.
input = getInput();
while (input == null)
{
input = getInput();
}
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.
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);
I get a compiler error while trying to access addMenu.setLayout().
Syntax error on token "setLayout", = expected after this token
Here is my code:
JPanel pnl1 = new JPanel(new GridLayout(8,0));
JPanel homeMenu = new JPanel(new GridLayout(3,0));
JPanel addMenu = new JPanel();
addMenu.setLayout(new BoxLayout(addMenu, BoxLayout.Y_AXIS)); // Here is the red underline
Are you using Eclipse? Sometimes it is buggy.
Just do File >> Save All.
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);