Results 1 to 1 of 1
- 02-19-2012, 06:53 PM #1
Problem with MigLayout (Added Components don't appear on screen)
I am trying to write an IRC client for fun and practice. One thing I'm struggling with is adding lines received from the server to my display. I basically have a class called chatline which has a timestamp, a prefix and a message, all represented by a JLabel. When such a line is added to the display, I just add each of these JLabels to the parent JPanel and wrap after the last one. But, for some reason, no line is to be seen.
The "Screen" class, which contains the ScrollPane and JPanel
and the ChatLine class:Java Code:package de.boreeas.aurora.gui; import java.util.ArrayList; import javax.swing.JPanel; import javax.swing.JScrollPane; import net.miginfocom.swing.MigLayout; /** * * @author malte */ public class Screen { public final String name; public final JScrollPane scrollPane; public final JPanel panel; private final MigLayout layout; private final ArrayList<ChatLine> savedLines = new ArrayList<ChatLine>(100); public Screen(final String name) { this.name = name; scrollPane = new JScrollPane(); layout = new MigLayout(); panel = new JPanel(layout); scrollPane.add(panel); panel.setLayout(layout); } public void addLine(ChatLine line) { if (savedLines.size() == 100) { savedLines.remove(0); } savedLines.add(line); rebuild(); } public void addLine(String hexcolor, String prefix, String message) { addLine(new ChatLine(prefix, message).withAllColors(hexcolor)); } public void addError(String message) { addLine(ChatLine.createErrorMessage(message)); } private void rebuild() { for (ChatLine line: savedLines) { line.append(panel); } } }
A SSCCE can be found here:Java Code:package de.boreeas.aurora.gui; import java.awt.Color; import java.text.SimpleDateFormat; import java.util.Date; import javax.swing.JLabel; import javax.swing.JPanel; /** * * @author malte */ public class ChatLine { private final JLabel timeStampLabel; private final JLabel prefixLabel; private final JLabel messageLabel; public ChatLine(String name, String prefix) { this.timeStampLabel = new JLabel(new SimpleDateFormat("hh:mm").format(new Date())); this.prefixLabel = new JLabel(name); this.messageLabel = new JLabel(prefix); } public ChatLine withTimeStampColor(String hexcolor) { timeStampLabel.setBackground(Color.decode(hexcolor)); return this; } public ChatLine withPrefixColor(String hexcolor) { prefixLabel.setBackground(Color.decode(hexcolor)); return this; } public ChatLine withMessageColor(String hexcolor) { messageLabel.setBackground(Color.decode(hexcolor)); return this; } public ChatLine withAllColors(String hexcolor) { timeStampLabel.setBackground(Color.decode(hexcolor)); prefixLabel.setBackground(Color.decode(hexcolor)); messageLabel.setBackground(Color.decode(hexcolor)); return this; } public void append(JPanel panel) { panel.add(timeStampLabel); panel.add(prefixLabel); panel.add(messageLabel, "wrap"); } /** * Creates a ChatLine with prefix [ERROR] and color #990000 * @param message The error message * @return A new ChatLine object with prefix [ERROR] and color #990000 */ public static ChatLine createErrorMessage(String message) { // TODO remove debug output System.out.println("Creating error message chat line: [ERROR] " + message); return new ChatLine("[ERROR]", message).withAllColors("#990000"); } }
http://dl.dropbox.com/u/31575034/AuroraSSCCE.zip
Similar Threads
-
Problem with splash screen
By frintocf in forum AWT / SwingReplies: 1Last Post: 09-06-2011, 11:42 PM -
Problem with aligning components
By kedecr in forum New To JavaReplies: 2Last Post: 03-09-2011, 01:23 PM -
screen resolution problem
By prabhug1987 in forum Advanced JavaReplies: 1Last Post: 10-15-2010, 07:01 PM -
Weird problem with doubles not being added to sum
By kiregad in forum New To JavaReplies: 3Last Post: 03-24-2010, 09:08 PM -
Problem In Swing Components
By SANDY_INDIA in forum AWT / SwingReplies: 1Last Post: 07-19-2008, 10:23 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks