Panels and text don't show up when running the program [closed] - java

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 days ago.
Improve this question
I've been working on an application that allows you to add recipes to a recipe book. I designed it with JFrame on NetBeans:
The design
How it should work:
The input in the JTextArea of instructions should be added to the preview of the instruction list below by clicking the + button next to it.
I want max. 4 steps to be displayed on the preview and then add pagination.
I created a JPanel with a Gridlayout of 2 rows and 2 columns.
Here is the code for it:
private void stepsPlusButtActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//Get the instruction text from instructions input text area
String steps = stepsTextArea.getText();
// Check if no input has been made
if (steps.isEmpty()) {
// Don't create step if there is no input
return;
}
// Create a new JLabel with step number and instruction
JLabel stepLabel = new javax.swing.JLabel();
stepLabel.setFont(new java.awt.Font("Segoe UI", 1, 12));
stepLabel.setForeground(new java.awt.Color(102, 102, 255));
stepLabel.setText("Step" + stepNumber + ":" + steps);
// Create a new JTextArea within a JScrollPane; this is to display the input
JTextArea stepTextArea = new javax.swing.JTextArea();
stepTextArea.setFont(new java.awt.Font("Segoe UI", 0, 12)); // NOI18N
stepTextArea.setForeground(new java.awt.Color(51, 51, 51));
JScrollPane stepScrollPane = new javax.swing.JScrollPane(stepTextArea);
stepScrollPane.setBackground(new java.awt.Color(204, 204, 255));
stepScrollPane.setBorder(null);
stepTextArea.setEditable(false);
// make the text area read-only
// Add the step label and text area to the panel
JPanel stepPanel = new javax.swing.JPanel();
stepPanel.setBackground(new java.awt.Color(204, 204, 255));
stepPanel.setLayout(new java.awt.BorderLayout());
stepPanel.add(stepLabel, BorderLayout.WEST);
stepPanel.add(stepScrollPane, BorderLayout.CENTER);
// Add the step panel to the grid layout
int row = (stepNumber - 1) / 2;
int col = (stepNumber - 1) % 2;
stepsPanel.add(stepPanel, new GridBagConstraints(col, row, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
stepsPanel.setComponentZOrder(stepPanel, 0);
// Increment the step number
stepNumber++;
// Clear the instruction text area
stepsTextArea.setText("");
}
Whenever I run the file, and add the input by clicking the plus button, nothing shows up on the instruction list preview. I also tried printing out the input and step number to the console and it seems to be working. How can I fix this?
It should look like this

Related

JLabel multiline with HTML

I have a JLabel that I would like to contain a text that can go over multiple lines, and resizes if the window changes shape.
I've looked this up and most people seem to recommend wrapping the label text in HTML. This however does not make new lines for me.
My label is located in a JPanel and I suspect that the problem may be that my panel has misconfigured its border, and so the label text just continues beyond the panel border.
here is how the label looks inside the status panel
Here are the settings of the panel:
private final JPanel statusPanel = new JPanel(new FlowLayout());
statusPanel.setBackground(Color.white);
statusPanel.add(latestOrdreLabel);
this.add(statusPanel, new GridBagConstraints(0, 0, 6, 1, 1.0, 1.0
, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(2, 2, 2, 2), 0, 0));
Then the label is set up like this:
private final JLabel latestOrdreLabelResult = new JLabel();
String latestOrdreStatus = getBean().getLatestOrdreStatus(etelOrderInterface.getOrderId());
latestOrdreLabelResult.setText("<html>"+latestOrdreStatus+"</html>");
statusPanel.add(latestOrdreLabelResult);
In HTML, to down line your'e need to use <br/> instead of \n.
You can to use in replace String method, for example:
latestOrdreStatus.replace("\n", "<br/>");
You just need to resize the label.

Java - Distance between JLabel and vertical box

I wrote this code for realize a box into I put four JRadioButtons
JRadioButton beginner = new JRadioButton("Beginner"); beginner.setSelected(true);
JRadioButton intermedie = new JRadioButton("Intermedie");
JRadioButton expert = new JRadioButton("Expert");
JRadioButton custom = new JRadioButton("Custom");
Box boxDifficulty = Box.createVerticalBox();
boxDifficulty.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 50));
boxDifficulty.add(beginner);
boxDifficulty.add(intermedie);
boxDifficulty.add(expert);
boxDifficulty.add(custom);
Furthermore I set the border of the box using the setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 50)) method.
Also, I created a JLabel for indicate the title of the box:
JLabel difficulty = new JLabel("Choose the difficulty:");
This is the portion complete of the code:
Box boxDifficulty = Box.createVerticalBox();
boxDifficulty.add(difficulty);
boxDifficulty.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 50));
boxDifficulty.add(beginner);
boxDifficulty.add(intermedie);
boxDifficulty.add(expert);
boxDifficulty.add(custom);
How can I increase the space between JLabel and JRadioButton?
How can I increase the space between JLabel and JRadioButton?
You can add a spacing component to the panel after you add the label:
boxDifficulty.add(difficulty);
boxDifficulty.add( Box.createVerticalStrut(...) );
A strut is a fixed height(width) invisible component.

Having trouble changing a variable with ActionListener

I'm working on a GUI and I have everything displaying correctly but I'm having trouble changing the value of a variable when I press a button. The variable is called passover and it is a private int that is initialized to the value of 1997 in the 0 argument constructor and I have 3 buttons that need to change the value of passover when pressed.
Code for passover:
panel.setBorder(
BorderFactory.createEmptyBorder(0, 0, 0, 0));
panel.setLayout(new BorderLayout(0, 0));
JPanel topPanel = getPanel();
topPanel.setLayout(new GridBagLayout());
topPanel.setBackground(new Color(173, 216, 230));
JLabel moveLabel = getLabel("Move Data");
moveLabel.setFont(new Font("Serif", Font.PLAIN, 20));
addComp(topPanel, moveLabel, 0, 0, 1, 1, 0.5, 0.2,
GridBagConstraints.HORIZONTAL, GridBagConstraints.NORTHEAST);
JLabel passoverLabel = getLabel(" Passover : " + passover);
passoverLabel.setFont(new Font("Serif", Font.PLAIN, 20));
addComp(topPanel, passoverLabel, 1, 0, 1, 1, 0.5, 0.2,
GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER);
Code for the buttons:
JPanel bottomRightPanel = getPanel();
bottomRightPanel.setBorder(
BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
bottomRightPanel.setLayout(new GridBagLayout());
bottomRightPanel.setBackground(new Color(255, 105, 180));
JPanel passoverButtonPanel = getPanel();
passoverButtonPanel.setBackground(new Color(255, 105, 180));
passoverButtonPanel.setLayout(new BoxLayout(passoverButtonPanel, BoxLayout.Y_AXIS));
nextPassoverButton = new JButton("Next Passover");
goToPassoverButton = new JButton("Go To Passover: ");
previousPassoverButton = new JButton("Previous Passover");
JLabel filler = getLabel(" ");
goToField = new JTextField(6);
goToField.setPreferredSize(new Dimension(5, 30));
theHandler handler = new theHandler();
nextPassoverButton.addActionListener(handler);
goToPassoverButton.addActionListener(handler);
previousPassoverButton.addActionListener(handler);
goToField.addActionListener(handler);
I want the value of passover to be raised one when nextPassoverButton is pressed, lowered one when previousPassoverButton is pressed, and to be changed to the value the user inputs into the goToField when goToPassoverButton is pressed.
My ActionListener class looks like:
public class theHandler extends MyDataPalV2 implements ActionListener{
public void actionPerformed(ActionEvent event)
{
if (event.getSource() == goToField)
{
this.passover = Integer.parseInt(String.format("%s", event.getActionCommand()));
}
if (event.getSource() == nextPassoverButton)
{
this.passover++;
}
if (event.getSource() == previousPassoverButton)
{
this.passover--;
}
}
}
The error is a private access error so I think I would need to use a getter and setter but I have no idea how I would do that. Maybe there is a complete different way to do this?
This is my first time posting here so sorry if I made any mistakes in my post.
You cannot compare two objects via '=='. Use .equals(Object) instead.
if(event.getSource().equals(goToField))
A getter and setter would be a public method in the same class as your private int passover. The get method would just be a single line mehtod that returns the value of your private int. Like
public static int getPassover(){
return passover;
}
It works because it is in the same class as passover and thus has access to it.
The setter just straight up sets the value:
public static void setPassover(int num){
passover = num;
}
Be careful with this if you are using threads.
For your method, if you want to do it with get set methods, you could increment it like
setPassover(getPassover() + 1);
If passover is not a class variable, you will take the 'static's out of the method heading. Does this make sense?
you must set the new passover value to the passoverLabel in handler code after passover value change. otherwhise you cannot see the new value on the label.
passoverLabel.setText(Integer.toString(passover))

JLabels in a JScrollPane

I have a scrollpane in which i want to add multiple jlabel.
This is the code..
JPanel panelEast = new JPanel();
panelEast.setBorder(new TitledBorder(null, "Notifiche", TitledBorder.LEADING, TitledBorder.TOP, null, null));
panelEast.setPreferredSize(new Dimension(250,120));
panelEast.setLayout(null);
JLabel lblNewLabel_3 = new JLabel("label1");
lblNewLabel_3.setIcon(new ImageIcon(Home.class.getResource("/it/polimi/icon/contact.png")));
lblNewLabel_3.setBounds(10, 81, 240, 52);
panelEast.add(lblNewLabel_3);
JLabel label_3 = new JLabel("label2");
label_3.setIcon(new ImageIcon(Home.class.getResource("/it/polimi/icon/verified.png")));
label_3.setBounds(new Rectangle(4, 0, 0, 0));
label_3.setAlignmentY(Component.TOP_ALIGNMENT);
label_3.setBounds(10, 30, 240, 52);
panelEast.add(label_3);
JLabel label_4 = new JLabel("label3");
label_4.setIcon(new ImageIcon(Home.class.getResource("/it/polimi/icon/verified.png")));
label_4.setBounds(new Rectangle(4, 0, 0, 0));
label_4.setAlignmentY(Component.TOP_ALIGNMENT);
label_4.setBounds(10, 131, 240, 52);
panelEast.add(label_4);
JScrollPane scrollPane = new JScrollPane(panelEast,JScrollPane.VERTICAL_SCROLLBAR_NEVER,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
pan2.add(scrollPane,BorderLayout.CENTER);
And does not work, any suggestion?
Avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify
JScrollPane relies on the layout manager API in order to calculate the preferred size of the container it is showing and determine when that view is larger than itself and it should show the scroll bars

Organize dynamically sized panels in a table format (Java SWING) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I've been trying to use a GridBagLayout to organize a bunch of panels (containing a bunch of labels which alter the size of the panel, mostly vertically). The major reason why i chose this is because the panel sizes can vary quite different, and I would like the panels to scale when the frame size is changed.
I keep coming across problems ranging from different sized panels (checking via borders on panels), weird sizing issues, cant get the panels to stick to each other vertically (massive space inbetween panels).
Gridbag seemed like the best option but it seems like its becoming a bigger pain than i imagined. What other options provide proper scaling when altering the frame size?
I have become a fan of what #SplungeBob showed me a long time ago. The code snippet below gives you an idea of what it takes to accomplish the GUI shown below it.
frame.add(menu, new GBConstraints( 0, 0).spanX(32).fill(HORIZONTAL));
frame.add(new JLabel("Available letters"),
new GBConstraints( 0, 1).insets( 2, 2, 0, 2));
frame.add(new JLabel("Play by play..."),
new GBConstraints( 2, 1).insets( 0, 5, 0, 0));
frame.add(pnlPlays, new GBConstraints( 2, 1).insets(10, 0, 0, 0).spanY(14).anchor(WEST).fill(BOTH));//.ipad(0,0));
frame.add(pnlAvail, new GBConstraints( 0, 2).insets(0, 5, 5, 5));
frame.add(pnlLegend, new GBConstraints( 1, 2) .spanY(14) .fill(BOTH));
frame.add(pnlBonuses, new GBConstraints( 0, 3).insets(0, 100, 0, 0));
frame.add(pnlSBBonuses, new GBConstraints( 0, 3).insets(0, 125, 0, 0));
frame.add(gamePanel, new GBConstraints( 0, 5).insets(0, 50, 0, 0));
frame.add(pnlTheScore, new GBConstraints( 0, 5) .anchor(EAST));
btnDebug.setPreferredSize(new Dimension(1, 1));
frame.add(btnDebug, new GBConstraints( 0, 0) .anchor(WEST));
frame.add(btnNextReplayStep,new GBConstraints( 0, 6).insets(0,310, 0, 0) .anchor(CENTER));
btnNextReplayStep.setVisible(false);
frame.add(new JLabel("User letters"),
new GBConstraints( 0, 6).insets(0,240, 30, 0));
frame.add(btnCopyUser, new GBConstraints( 0, 6).insets(0,310,0,0) .anchor(CENTER));
frame.add(btnCopyAllLetters,new GBConstraints( 0, 6) .anchor(EAST));
frame.add(pnlNotes, new GBConstraints( 0, 6).insets(25,10,10, 10).spanY( 2).anchor(WEST));
frame.add(btnClearMnemHi, new GBConstraints( 0, 7).insets(0,430, 0, 0));
frame.add(btnClearBorders, new GBConstraints( 0, 7).insets(0,530, 0, 0));
I won't lie; it can still be a major task, but with careful planning with an actual grid marked off in vertical and horizontal lines to show, e.g., how many columns a field must span, it's worth a try. And chances are excellent that what #Hovercraft suggests is even better.
(I haven't mastered it, it seems as the weird insets values seem to indicate. But it's a nice-looking reliable GUI.)
EDIT
Here's code for how two of the JPanel objects were created prior to executing the code above. One also uses GridBagLayout; other doesn't. Just whatever works easiest and best.
private void makePlays(){
pnlPlays = new JPanel(new GridBagLayout());
pnlPlays.setSize(500,300);
pnlPlays.add(scrPlays, new GBConstraints(0,1).span(40, 62)
.fill(BOTH).ipad(100, 50));
scrPlays.setEnabled(false);
}
private void makeScoremasterBonuses(){
BonusBackgroundColors c;
BonusBackgroundColors all [] = BonusBackgroundColors.values();
String labels[] = new String[all.length];
String abbrs [] = new String[all.length];
JButton but;
pnlBonuses = new JPanel();
pnlBonuses.add(new JLabel("Legend:"));
for (int i = 0; i < all.length; i++) {
labels[i] = all[i].name().replace("_", " ").toLowerCase();
abbrs [i] = Utilities.abbreviate(all[i].name());
c = BonusBackgroundColors.values()[i];
but = new JButton(abbrs[i]);
but.setToolTipText(labels[i]);
but.setBackground(c.background(c));
but.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
but.setActionCommand("" + i);
but.addActionListener(this);
pnlBonuses.add(but);
}
}

Categories