How to set padding at JLabel - java

I want to display a Multiline JLabel into JPanel.
So I have this code but I'm not able to show the multiline JLabel.
public class NotificationFrame extends JFrame{
public NotificationFrame(){
JPanel panelBody = new JPanel();
panelBody.setBackground(Color.white);
GridBagConstraints GBC2 = new GridBagConstraints();
Container CR2 = new Container();
GridBagLayout GBL2 = new GridBagLayout();
CR2.setLayout(GBL2);
panelBody.add(CR2);
GBC2 = new GridBagConstraints();
CR2.add(labelTesto);
GBC2.gridx=0;
GBC2.gridy=0;
GBC2.insets.left = 10;
GBC2.insets.top=0;
GBL2.setConstraints(labelTesto,GBC2);
panelBody.setLayout(new FlowLayout(FlowLayout.CENTER));
add(panelBody,BorderLayout.CENTER);
}
}
If I change the last line of code in
add(labelTest,BorderLayout.CENTER);
I can show that I want. But it is not correct because I want to set a padding on JLabel
EDIT
I have use this code now:
JPanel panelBody = new JPanel();
panelBody.setBackground(Color.white);
SpringLayout layout = new SpringLayout();
panelBody.setLayout(layout);
panelBody.add(labelTesto);
layout.putConstraint(SpringLayout.NORTH, labelTesto, 15, SpringLayout.NORTH, panelBody);
add(panelBody,BorderLayout.CENTER);
This is the layout:
This is the all test that I should see:
"Il 31 Dicembre scadrĂ  l'assistenza, ricorda di rinnovare l'assistenza per ricevere sempre assistenza ed aggiornamenti."

Multiline it's not padding. For achieve multiline text in JLabel or JButton you need to use HTML.
To wrap text (or multiline):
btn.setText("<html><p>Il 31 Dicembre scadrĂ  l'assistenza, ricorda di rinnovare l'assistenza per ricevere sempre assistenza ed aggiornamenti</p></html>");
label.setText("<html>Date:<br></html>"+getDateFromSomeWhere);
To set padding:
label.setBorder(new EmptyBorder(0,10,0,0));//top,left,bottom,right

Related

Combine cells to MigLayout?

How to combine two cells using MigLayout?
I have two cells, as shown in the following code. I want to combine them, what do I have to do?
JPanel jPanelAccOil = new JPanel(new MigLayout("", "0[]0", "0[]0"));
jPanelAccOil.add(new JPanelInAI(null, "",
sistema.getGestorComunicacioOPC().getParametresOPCHyperG().getActuatorOilPressure()));
this.add(jPanelAccOil, "cell 2 3");
JPanel jPanelAccPneumatic = new JPanel(new MigLayout("", "0[]0", "0[]0"));
jPanelAccPneumatic.add(new JPanelInAI(null, "",
sistema.getGestorComunicacioOPC().getParametresOPCHyperG().getAccPneumaticPressure()));
this.add(jPanelAccPneumatic, "cell 3 3, wrap");
You can use to span cells:
span 2
try this:
JPanel jPanelAccPneumatic = new JPanel(new MigLayout("", "0[]0", "0[]0"));
jPanelAccPneumatic.add(new JPanelInAI(null, "",
sistema.getGestorComunicacioOPC().getParametresOPCHyperG().getAccPneumaticPressure()));
this.add(jPanelAccPneumatic, "cell 3 3, span 2, wrap");

JLabel and JButton Center in BoxLayout

I'm newbie in Java programming and can't deal with my Swing problem. I can't get JButtons and JLabels Centred in JPanels(BoxLayout).
Here are some photos :
Code:
PlayerLabel.setHorizontalTextPosition(JLabel.CENTER);
PlayerLabel.setVerticalTextPosition(JLabel.BOTTOM);
PlayerLabel.setText("Player Level: " + CarMain.main[5]);
AccessoriesLVL1Label.setHorizontalTextPosition(JLabel.CENTER);
AccessoriesLVL1Label.setVerticalTextPosition(JLabel.BOTTOM);
AccessoriesLVL1Label.setText("<html>Accessories LVL 1<br>" + "Count: " + Part.parts[1]);
JButton jbtnSellAccessoriesLv1 = new JButton("Sell");
jbtnSellAccessoriesLv1.addActionListener(this);
And this is where i make Jlabel :
//Upgrades Panel
GridLayout UpgradesLayout = new GridLayout(3,3);
JPanel UpgradesPanel = new JPanel();
UpgradesPanel.setLayout(UpgradesLayout);
JPanel UpgradesPanelSub = new JPanel();
UpgradesPanelSub.setLayout(new BoxLayout(UpgradesPanelSub, BoxLayout.PAGE_AXIS));
UpgradesPanelSub.setBorder(BorderFactory.createCompoundBorder(raisedbevel, loweredbevel));
JPanel UpgradesPanelSub2 = new JPanel();
UpgradesPanelSub2.setLayout(new BoxLayout(UpgradesPanelSub2, BoxLayout.PAGE_AXIS));
UpgradesPanelSub2.setBorder(BorderFactory.createCompoundBorder(raisedbevel, loweredbevel));
JPanel UpgradesPanelSub3 = new JPanel();
UpgradesPanelSub3.setLayout(new BoxLayout(UpgradesPanelSub3, BoxLayout.PAGE_AXIS));
UpgradesPanelSub3.setBorder(BorderFactory.createCompoundBorder(raisedbevel, loweredbevel));
UpgradesPanelSub.add(Labels.PlayerLabel);
UpgradesPanelSub.add(jbtnUpgradeLevel);
UpgradesPanelSub2.add(Labels.GarageLabel);
UpgradesPanelSub2.add(jbtnUpgradeGarageLevel);
UpgradesPanelSub3.add(Labels.BoxesLVLLabel);
UpgradesPanelSub3.add(jbtnUpgradeBoxesLevel);
UpgradesPanel.add(new JLabel(""));
UpgradesPanel.add(new JLabel(""));
UpgradesPanel.add(new JLabel(""));
UpgradesPanel.add(UpgradesPanelSub);
UpgradesPanel.add(UpgradesPanelSub2);
UpgradesPanel.add(UpgradesPanelSub3);
UpgradesPanel.add(new JLabel(""));
UpgradesPanel.add(new JLabel(""));
UpgradesPanel.add(new JLabel(""));
As for the layout problem: there's a tutorial on how to use box layout (look for the components' alignment to center them): docs.oracle.com/javase/tutorial/uiswing/layout/box.html.
Alternatively you could use one of the external layout managers like MigLayout which are easier to use IMO.

Java Titled Border

Is there any way to set font of the title of the titled border in java swing?
JPanel panelDOB = new JPanel();
panelDOB.setBorder(new TitledBorder(new LineBorder(new Color(171, 173, 179)), "DATE OF BIRTH", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(109, 109, 109)));
panelDOB.setBackground(Color.WHITE);
panelDOB.setFont(new Font("Tw Cen MT", Font.PLAIN, 12));
sl_panelFac.putConstraint(SpringLayout.NORTH, panelDOB, 6, SpringLayout.SOUTH, txtNationality);
sl_panelFac.putConstraint(SpringLayout.WEST, panelDOB, 10, SpringLayout.WEST, panelFac);
sl_panelFac.putConstraint(SpringLayout.SOUTH, panelDOB, 61, SpringLayout.SOUTH, txtNationality);
sl_panelFac.putConstraint(SpringLayout.EAST, panelDOB, 196, SpringLayout.WEST, panelFac);
panelFac.add(panelDOB);
panelDOB.setLayout(new SpringLayout());
This is answered by simply looking up TitledBorder in the Java API.
There you'll find two constructors (here's one),
public TitledBorder(Border border,
String title,
int titleJustification,
int titlePosition,
Font titleFont)
that take a font as well as a method for setting the font,
public void setTitleFont(Font titleFont)
Alternatively, there are two methods in the BorderFactory that creates a TitledBorder with your selected font, again available through the Java API.
public static TitledBorder createTitledBorder(Border border,
String title,
int titleJustification,
int titlePosition,
Font titleFont)
I'm frankly a little surprised that you didn't look into the API before coming here.

JScrollPane hides the textarea

I can't figure out why this doesn't work... It hides the JTextArea.
JTextArea _area=new JTextArea();
_area.setBounds(10, 10, 465, 250);
add(_area);
JScrollPane scroller=new JScrollPane(_area);
scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
add(scroller);
EDIT:
Shuffle the bound values set the bounds only of ScrollPane and not of TextArea.
JTextArea _area = new JTextArea();
// _area.setBounds(10, 10, 400, 250);
add(_area);
JScrollPane scroller = new JScrollPane(_area);
scroller.setBounds(10, 10, 400, 250);
scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
add(scroller);

I'm having trouble setting up a jpanel and frame

I'm pretty new to swing and am having some troubles.
Heres my code. I'm getting the error
Exception in thread "main" java.lang.IllegalArgumentException: adding container's parent to itself
at java.awt.Container.checkAddToSelf(Container.java:472)
at java.awt.Container.addImpl(Container.java:1083)
at java.awt.Container.add(Container.java:410)
at UMSL.Eval.createUserInterface(Eval.java:126)
at UMSL.Eval.(Eval.java:95)
at UMSL.Eval.main(Eval.java:56)
Java Result: 1
Can anyone explain to me what this error message is trying to tell me and how I can make things work.
private void createUserInterface()
{
JPanel contentPane;
contentPane = new JPanel();
JPanel instructorPanel = new JPanel();
instructorPanel.setBounds(40, 20, 276, 48);
instructorPanel.setBorder (BorderFactory.createEtchedBorder() );
instructorPanel.setLayout( null) ;
instructorPanel = new JPanel();
contentPane.add(instructorPanel);
// set up Instructor Label
JLabel instructorLabel = new JLabel();
instructorLabel.setBounds (25, 15, 100, 20);
instructorLabel.setText("Instructor:");
instructorLabel.add (instructorLabel);
Like #Reimeus said, you're trying to add a label to itself with the line
instructorLabel.add (instructorLabel);
I'm assuming that you're trying to add the label to the instructor panel which would be
instructorPanel.add(instructorLabel);

Categories

Resources