Last active
August 29, 2015 14:24
-
-
Save laszlokorte/2df0eaf4350e0e06d7d9 to your computer and use it in GitHub Desktop.
JScrollpane and Textarea
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.BorderLayout; | |
import java.awt.Color; | |
import java.awt.Component; | |
import java.awt.Dimension; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import java.awt.event.MouseEvent; | |
import java.awt.event.MouseMotionAdapter; | |
import javax.swing.Box; | |
import javax.swing.BoxLayout; | |
import javax.swing.JButton; | |
import javax.swing.JFrame; | |
import javax.swing.JLabel; | |
import javax.swing.JPanel; | |
import javax.swing.JScrollPane; | |
import javax.swing.JSplitPane; | |
import javax.swing.JTextArea; | |
import javax.swing.ScrollPaneConstants; | |
import javax.swing.SwingUtilities; | |
public class Main { | |
static final Box inner = Box.createVerticalBox(); | |
// if the jtextarea should contain text | |
private static boolean textEnabled = true; | |
public static void main(final String[] args) { | |
final JFrame frame = new JFrame(); | |
final JPanel insideScroll = insideScroll(); | |
final JScrollPane scrollpane = new JScrollPane(insideScroll); | |
scrollpane.setAutoscrolls(false); | |
scrollpane | |
.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); | |
final JPanel rightPanel = new JPanel(); | |
rightPanel.setPreferredSize(new Dimension(300, 300)); | |
rightPanel.addMouseMotionListener(new MouseMotionAdapter() { | |
@Override | |
public void mouseDragged(final MouseEvent e) { | |
super.mouseDragged(e); | |
final int pos = scrollpane.getVerticalScrollBar().getValue(); | |
update(); | |
SwingUtilities.invokeLater(new Runnable() { | |
@Override | |
public void run() { | |
System.out.println(String.format("[Out] %s", pos)); | |
} | |
}); | |
} | |
}); | |
// Add labels describing the problem | |
rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.PAGE_AXIS)); | |
final JButton toggleButton = new JButton("Toggle text"); | |
rightPanel.add(toggleButton); | |
rightPanel | |
.add(new JLabel( | |
"<html>If the text is disabled, you can press/drag<br> your mouse on on right side of the<br> window and the scrollbar will<br> stay in its position.")); | |
rightPanel | |
.add(new JLabel( | |
"<html>If the text is enabled, the scrollbar<br> will jump around when dragging<br> on the right side.")); | |
// enable/disable the text when the button is clicked. | |
toggleButton.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(final ActionEvent e) { | |
textEnabled = !textEnabled; | |
update(); | |
} | |
}); | |
final JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, | |
scrollpane, rightPanel); | |
frame.add(split); | |
// initialize the scrollpane content | |
update(); | |
frame.pack(); | |
frame.setVisible(true); | |
frame.setLocationRelativeTo(null); | |
} | |
// initializes the components inside the scrollpane | |
private static JPanel insideScroll() { | |
final JPanel panel = new JPanel(); | |
panel.setLayout(new BorderLayout()); | |
panel.add(inner, BorderLayout.NORTH); | |
return panel; | |
} | |
// replaces all components inside the scrollpane | |
private static void update() { | |
inner.removeAll(); | |
for (int i = 0; i < 30; i++) { | |
inner.add(buildRow(i)); | |
} | |
inner.revalidate(); | |
} | |
// build a single component to be inserted into the scrollpane | |
private static JPanel buildRow(final int i) { | |
final JPanel row = new JPanel(); | |
final Color bg = i % 2 == 0 ? Color.DARK_GRAY : Color.LIGHT_GRAY; | |
row.setBackground(bg); | |
row.setPreferredSize(new Dimension(300, 80)); | |
row.setLayout(new BorderLayout()); | |
row.add(textarea(bg), BorderLayout.CENTER); | |
return row; | |
} | |
// build the textarea to be inserted into the cells in the scroll pane | |
private static Component textarea(final Color bg) { | |
final JTextArea textarea = new JTextArea(); | |
textarea.setEnabled(false); | |
textarea.setLineWrap(true); | |
textarea.setBackground(bg); | |
textarea.setDisabledTextColor(Color.black); | |
textarea.setEditable(false); | |
if (textEnabled) { | |
textarea.setText("Lorem ipsum dolor si amet. Lorem ipsum dolor si amet. Lorem ipsum dolor si amet"); | |
} | |
return textarea; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment