Soel
06-05-2009, 19:24
Salve a tutti.
Ho un JPanel contenente una JLabel, e vorrei fare in modo che il testo vada automaticamente a capo una volta raggiunto il limite orizzontale della JLabel. Con i tag <html> riesco ad ottenere questo comportamento, tuttavia non riesco ad impostare l'altezza della JLabel in modo che si adatti a quella del testo, ritorni a capo compresi. Per quanto lungo sia il testo, mi viene sempre e solo visualizzata un'unica riga. Sapreste aiutarmi?
Alternativamente ho pensato di utilizzare una JTextArea al posto della JLabel, tuttavia vorrei che il colore di sfondo della JTextArea fosse lo stesso della JLabel, invece del bianco. Esiste un comando per impostarlo in questo modo?
Grazie anticipatamente per l'aiuto.
Se servisse, il codice del JPanel è questo (la JLabel su cui abilitare il wrapping è trigger):
public class Trigger extends JPanel implements ActionListener {
private JFrame main;
private int ID;
private JLabel status;
public Trigger(String triggerText, int triggerID, JFrame owner) {
super();
this.main = owner;
this.ID = triggerID;
triggerText = triggerText.replace("<", "<");
triggerText = "<html>"+triggerText+"</html>";
JLabel trigger = new JLabel(triggerText.replace("\n", "<br>"), JLabel.LEFT);
trigger.setPreferredSize(new Dimension(400, trigger.getPreferredSize().height));
status = new JLabel("Stato: Attivo", JLabel.LEFT);
SpringLayout templayout = new SpringLayout();
setLayout(templayout);
Spring hlabel = Spring.constant(5);
Spring hbutton = Spring.constant(-8);
Spring hstatus = Spring.constant(5);
Spring vlabel = Spring.constant(owner.VERTICAL_SPRING);
Spring vbutton = Spring.constant(4);
Spring vstatus = Spring.constant(18);
JButton deleteButton = new JButton("Delete");
deleteButton.addActionListener(this);
add(trigger);
add(status);
add(deleteButton);
templayout.putConstraint(SpringLayout.WEST, trigger, hlabel, SpringLayout.WEST, this);
templayout.putConstraint(SpringLayout.NORTH, trigger, vlabel, SpringLayout.NORTH, this);
templayout.putConstraint(SpringLayout.WEST, status, hstatus, SpringLayout.WEST, this);
templayout.putConstraint(SpringLayout.NORTH, status, vstatus, SpringLayout.SOUTH, trigger);
templayout.putConstraint(SpringLayout.EAST, deleteButton, hbutton, SpringLayout.EAST, this);
templayout.putConstraint(SpringLayout.NORTH, deleteButton, vbutton, SpringLayout.SOUTH, status);
setBorder(new TitledBorder("Trigger "+triggerID));
setPreferredSize(new Dimension(owner.panelWidth, trigger.getPreferredSize().height + status.getPreferredSize().height + 2*owner.VERTICAL_SPRING + vstatus.getValue() + 60));
}
[...]
public void actionPerformed(ActionEvent e) {
[...]
}
}
Ho un JPanel contenente una JLabel, e vorrei fare in modo che il testo vada automaticamente a capo una volta raggiunto il limite orizzontale della JLabel. Con i tag <html> riesco ad ottenere questo comportamento, tuttavia non riesco ad impostare l'altezza della JLabel in modo che si adatti a quella del testo, ritorni a capo compresi. Per quanto lungo sia il testo, mi viene sempre e solo visualizzata un'unica riga. Sapreste aiutarmi?
Alternativamente ho pensato di utilizzare una JTextArea al posto della JLabel, tuttavia vorrei che il colore di sfondo della JTextArea fosse lo stesso della JLabel, invece del bianco. Esiste un comando per impostarlo in questo modo?
Grazie anticipatamente per l'aiuto.
Se servisse, il codice del JPanel è questo (la JLabel su cui abilitare il wrapping è trigger):
public class Trigger extends JPanel implements ActionListener {
private JFrame main;
private int ID;
private JLabel status;
public Trigger(String triggerText, int triggerID, JFrame owner) {
super();
this.main = owner;
this.ID = triggerID;
triggerText = triggerText.replace("<", "<");
triggerText = "<html>"+triggerText+"</html>";
JLabel trigger = new JLabel(triggerText.replace("\n", "<br>"), JLabel.LEFT);
trigger.setPreferredSize(new Dimension(400, trigger.getPreferredSize().height));
status = new JLabel("Stato: Attivo", JLabel.LEFT);
SpringLayout templayout = new SpringLayout();
setLayout(templayout);
Spring hlabel = Spring.constant(5);
Spring hbutton = Spring.constant(-8);
Spring hstatus = Spring.constant(5);
Spring vlabel = Spring.constant(owner.VERTICAL_SPRING);
Spring vbutton = Spring.constant(4);
Spring vstatus = Spring.constant(18);
JButton deleteButton = new JButton("Delete");
deleteButton.addActionListener(this);
add(trigger);
add(status);
add(deleteButton);
templayout.putConstraint(SpringLayout.WEST, trigger, hlabel, SpringLayout.WEST, this);
templayout.putConstraint(SpringLayout.NORTH, trigger, vlabel, SpringLayout.NORTH, this);
templayout.putConstraint(SpringLayout.WEST, status, hstatus, SpringLayout.WEST, this);
templayout.putConstraint(SpringLayout.NORTH, status, vstatus, SpringLayout.SOUTH, trigger);
templayout.putConstraint(SpringLayout.EAST, deleteButton, hbutton, SpringLayout.EAST, this);
templayout.putConstraint(SpringLayout.NORTH, deleteButton, vbutton, SpringLayout.SOUTH, status);
setBorder(new TitledBorder("Trigger "+triggerID));
setPreferredSize(new Dimension(owner.panelWidth, trigger.getPreferredSize().height + status.getPreferredSize().height + 2*owner.VERTICAL_SPRING + vstatus.getValue() + 60));
}
[...]
public void actionPerformed(ActionEvent e) {
[...]
}
}