Reputation: 2923
I have a fairly simple view class that I am in the process of building. I test as I go, so the code is far from complete. However, when I test, everything looks fine until I scroll. If I scroll a page at a time by clicking the open area on the scrollbar, everything is fine. If I use the scroll arrows, drag the scroll bar, or use the mouse wheel, the newly revealed content is completely mangled. This occurs both with 1.6.35 and 1.7.09. I also notice mangling when I click and drag on the "log line", which is a JTextField. Please tell me I'm doing something wrong here. The code should run as is.
package com.mycompany.utility.logs;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.EmptyBorder;
/**
* This class implements the log viewer view.
*/
public class LogViewer extends JFrame
{
private static final long serialVersionUID = 1L;
private static final Color TRANSPARENT = new Color(255, 255, 255, 0);
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable() {
@Override
public void run()
{
try
{
LogViewer frame = new LogViewer();
frame.setVisible(true);
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
/**
* Create the view.
*/
public LogViewer()
{
GridBagConstraints gridBagConstraints = null;
int row = 0;
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 713, 684);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel topPanel = new JPanel();
contentPane.add(topPanel, BorderLayout.NORTH);
GridBagLayout gbl_topPanel = new GridBagLayout();
gbl_topPanel.columnWidths = new int[] { 0 };
gbl_topPanel.rowHeights = new int[] { 0 };
gbl_topPanel.columnWeights = new double[] { Double.MIN_VALUE };
gbl_topPanel.rowWeights = new double[] { Double.MIN_VALUE };
topPanel.setLayout(gbl_topPanel);
JLabel titleLabel = new JLabel("Tattle Tail Log Viewer");
titleLabel.setFont(new Font("Lucida Sans", Font.BOLD, 12));
gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridy = 0;
gridBagConstraints.gridx = 0;
topPanel.add(titleLabel, gridBagConstraints);
JPanel bottomPanel = new JPanel();
contentPane.add(bottomPanel, BorderLayout.SOUTH);
JSplitPane splitPane = new JSplitPane();
splitPane.setResizeWeight(0.75);
splitPane.setOneTouchExpandable(true);
splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
contentPane.add(splitPane, BorderLayout.CENTER);
JPanel scrollPanel = new JPanel();
scrollPanel.setBackground(Color.WHITE);
scrollPanel.setLayout(new GridBagLayout());
JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportView(scrollPanel);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
splitPane.setLeftComponent(scrollPane);
final JTextPane textPane = new JTextPane();
splitPane.setRightComponent(textPane);
textPane.setFont(new Font("Courier New", Font.PLAIN, 11));
for (int i = 0; i < 25; i++)
{
addLogEntry(scrollPanel, textPane, row,
"2013-03-11 15:40:19,123 INFO com.mycompany.business.logic.ImportantProcess",
"Something of which you need to be aware happened " + i + ".");
row++;
}
JPanel fillPanel = new JPanel();
fillPanel.setBackground(Color.LIGHT_GRAY);
gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = row;
gridBagConstraints.gridwidth = 2;
gridBagConstraints.weighty = 1D;
gridBagConstraints.fill = GridBagConstraints.BOTH;
scrollPanel.add(fillPanel, gridBagConstraints);
}
private void addLogEntry(final JPanel scrollPanel, final JTextPane textPane, final int row, final String logText,
final String messageText)
{
GridBagConstraints gridBagConstraints = null;
JPanel entryPanel = new JPanel();
entryPanel.setBackground(TRANSPARENT);
entryPanel.setLayout(new GridBagLayout());
gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = row;
gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
gridBagConstraints.weightx = 1D;
gridBagConstraints.weighty = 0D;
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
scrollPanel.add(entryPanel, gridBagConstraints);
JPanel logLinePanel = new JPanel();
logLinePanel.setBackground(TRANSPARENT);
logLinePanel.setFocusable(true);
logLinePanel.setLayout(new GridBagLayout());
gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.weightx = 1D;
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
entryPanel.add(logLinePanel, gridBagConstraints);
JLabel logLineLevelLabel = new JLabel(" ");
logLineLevelLabel.setOpaque(true);
logLineLevelLabel.setBackground(new Color(0, 128, 0));
logLineLevelLabel.setFont(new Font("Courier New", Font.BOLD, 11));
gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
logLinePanel.add(logLineLevelLabel, gridBagConstraints);
JTextField logLineText = new JTextField(logText);
logLineText.setEditable(false);
logLineText.setBackground(TRANSPARENT);
logLineText.setBorder(null);
logLineText.setFont(new Font("Courier New", Font.PLAIN, 11));
gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 0;
gridBagConstraints.weightx = 1D;
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
logLinePanel.add(logLineText, gridBagConstraints);
JPanel messageLinePanel = new JPanel();
messageLinePanel.setBackground(TRANSPARENT);
messageLinePanel.setLayout(new GridBagLayout());
gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
gridBagConstraints.weightx = 1D;
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
entryPanel.add(messageLinePanel, gridBagConstraints);
JLabel hasMoreMessageLineLabel = new JLabel("+ ");
hasMoreMessageLineLabel.setFont(new Font("Courier New", Font.BOLD, 11));
gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
messageLinePanel.add(hasMoreMessageLineLabel, gridBagConstraints);
JLabel messageLineLabel = new JLabel(messageText);
messageLineLabel.setBackground(TRANSPARENT);
messageLineLabel.setFocusable(true);
messageLineLabel.setFont(new Font("Courier New", Font.PLAIN, 11));
gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 0;
gridBagConstraints.weightx = 1D;
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
messageLinePanel.add(messageLineLabel, gridBagConstraints);
entryPanel.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e)
{
final JPanel containerPanel = (JPanel) e.getComponent();
final JPanel messagePanel = (JPanel) containerPanel.getComponent(1);
final JLabel messageLabel = (JLabel) messagePanel.getComponent(1);
String text = messageLabel.getText();
textPane.setText(text);
}
@Override
public void mouseExited(MouseEvent e)
{
textPane.setText("");
}
});
}
}
Upvotes: 0
Views: 192
Reputation: 324098
private static final Color TRANSPARENT = new Color(255, 255, 255, 0);
I would guess that is your problem. Be careful when setting backgrounds with a transparent color. See Backgrounds With Transparency to understand why this is a problem.
In your case (because you are using full transparency) you can just use:
setOpaque( false );
Upvotes: 3
Reputation: 13103
Man, that's weird looking.
For anyone looking for a little more information: when scrolling the text panel, the contents of the panels within the scroll panel don't maintain their integrity; parts of lines of letters stay behind while their originals scroll down, making it look a bit "melted". In other words, 'mangling' doesn't mean mangling the text, it is the pixels making up the image that are mangled.
I would try making panels within the scroll panel without GridBag. I'm not that familiar with GridBag, I find it unpleasant and have managed so far to mostly do without it. But the structure of your inner panel looks simple enough that you don't need it either: it appears you have an image upperleft, then a text line, then maybe another image, then another line of text. Make two JPanels, one for each line, you can use flowlayout and horizontal orientation and just stick the image and text in there, then horizontal layout and stick the two JPanels into a panel, and now you have a panel you can just add to the scrolling panel.
I don't know if that will solve the problem, but the GridBag is the only thing from your code that looks that different from things I've done before that didn't have this behavior. And I'm just not facile enough with GridBag to figure out quickly what might be wrong with it.
Upvotes: 0