JScrollPane with FlowLayout [duplicate] - java

I'm trying to show multiple smaller JPanel on a JScrollPane.
To achieve this I currently add them to another JPanel and set this panel as the ViewportView of the scrollPane.
Is there a way to add the panels directly to the scrollpane?
What didn't work is this:
JScrollPane scrollPane = new JScrollPane();
scrollPane.setPreferredSize(new Dimension(480, 480));
scrollPane.setSize(new Dimension(480, 480));
scrollPane.setMinimumSize(new Dimension(480, 40));
scrollPane.setViewportBorder(null);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
System.out.println("start");
for (int i=0; i<5;i++)
{
SingleClientPanel x = new SingleClientPanel();
x.setLocation(0, 45 *i);
scrollPane.getViewport().add(x);
}

To achieve this I currently add them to another JPanel and set this panel as the viewport of the scrollPane.
Not quite. You would not make the container JPanel the viewport but rather the viewport's view. The viewport itself is a very specialized container with its own layout manager, and this would be messed up if you simply replaced it with a JPanel.
i.e.,
JViewport viewport = myScrollPane.getViewport();
viewport.setView(myContainerJPanel);
or more concisely
myScrollPane.setViewportView(myContainerJPanel);
Note that this worries me: x.setLocation(0, 45 *i); and suggests use of null layouts somewhere. Whatever you do, don't do this, don't use null layouts, especially within JScrollPanes as it will muck it all up.
For more detailed help, consider creating and posting an sscce or a minimal example program/mcve where you condense your code into the smallest bit that still compiles and runs, has no outside dependencies (such as need to link to a database or images), has no extra code that's not relevant to your problem, but still demonstrates your problem. Also consider posting an image of your desired output.
For example:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.*;
public class ScrollPaneEg extends JPanel {
private static final int PREF_W = 480;
private static final int PREF_H = PREF_W;
public ScrollPaneEg() {
JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportBorder(null);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
JPanel container = new JPanel(new GridLayout(0, 1)); // 1 column variable
// number of rows
for (int i = 0; i < 15; i++) {
SingleClientPanel x = new SingleClientPanel(String.valueOf(i + 1));
// x.setLocation(0, 45 *i);
container.add(x);
}
scrollPane.setViewportView(container);
setLayout(new BorderLayout());
add(scrollPane, BorderLayout.CENTER);
}
#Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
private static void createAndShowGui() {
ScrollPaneEg mainPanel = new ScrollPaneEg();
JFrame frame = new JFrame("ScrollPaneEg");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
class SingleClientPanel extends JPanel {
private static final int PREF_H = 60;
public SingleClientPanel(String text) {
setBorder(BorderFactory.createTitledBorder("Single Client"));
setLayout(new GridBagLayout());
add(new JLabel("Panel: " + text, SwingConstants.CENTER));
}
#Override
public Dimension getPreferredSize() {
Dimension superSz = super.getPreferredSize();
if (isPreferredSizeSet()) {
return superSz;
}
int prefH = Math.max(superSz.height, PREF_H);
return new Dimension(superSz.width, prefH);
}
}
Also, consider using a JTable to display your tabular data. For instance,...
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Window;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.table.AbstractTableModel;
public class ClientOverviewTest {
private static void createAndShowGui() {
ClientOverviewPanel2 mainPanel = new ClientOverviewPanel2();
JFrame frame = new JFrame("ClientOverviewPanel");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
class ClientOverviewPanel2 extends JPanel {
private static final int CLIENTS = 5;
private static final int PREF_W = 600;
private static final int PREF_H = 200;
private ClientTableModel model = new ClientTableModel();
private JTable table = new JTable(model);
public ClientOverviewPanel2() {
for (int i = 0; i < CLIENTS; i++) {
String ip = "127.000.000.001";
UUID uuid = UUID.randomUUID();
boolean isLocal = true;
SingleClient client = new SingleClient(ip, uuid, isLocal);
model.addRow(client);
}
table.getColumnModel().getColumn(1).setPreferredWidth(150); //!!
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
setLayout(new BorderLayout());
add(scrollPane, BorderLayout.CENTER);
add(new JButton(new OkAction("OK")), BorderLayout.PAGE_END);
}
#Override
public Dimension getPreferredSize() {
Dimension superSz = super.getPreferredSize();
if (isPreferredSizeSet()) {
return superSz;
}
int prefW = PREF_W;
int prefH = Math.min(superSz.height, PREF_H);
return new Dimension(prefW, prefH);
}
private class OkAction extends AbstractAction {
public OkAction(String name) {
super(name);
int mnemonic = (int) name.charAt(0);
putValue(MNEMONIC_KEY, mnemonic);
}
#Override
public void actionPerformed(ActionEvent e) {
Component source = (Component) e.getSource();
Window window = SwingUtilities.getWindowAncestor(source);
if (window != null) {
window.dispose();
}
}
}
}
class ClientTableModel extends AbstractTableModel {
public final static String[] COLUMNS = { "IP", "UUID", "Local" };
private List<SingleClient> clientList = new ArrayList<>();
#Override
public int getColumnCount() {
return COLUMNS.length;
}
#Override
public int getRowCount() {
return clientList.size();
}
#Override
public String getColumnName(int column) {
return COLUMNS[column];
}
public void addRow(SingleClient client) {
clientList.add(client);
int index = clientList.size() - 1;
fireTableRowsInserted(index, index);
}
#Override
public Object getValueAt(int rowIndex, int columnIndex) {
if (rowIndex >= getRowCount() || rowIndex < 0) {
String text = "for rowIndex: " + rowIndex;
throw new IllegalArgumentException(text);
}
if (columnIndex < 0 || columnIndex >= COLUMNS.length) {
String text = "for columnIndex: " + columnIndex;
throw new IllegalArgumentException(text);
}
SingleClient client = clientList.get(rowIndex);
switch (columnIndex) {
case 0:
return client.getIp();
case 1:
return client.getUuid();
case 2:
return client.isLocal();
}
return null;
}
#Override
public Class<?> getColumnClass(int columnIndex) {
if (columnIndex < 0 || columnIndex >= COLUMNS.length) {
String text = "for columnIndex: " + columnIndex;
throw new IllegalArgumentException(text);
}
switch (columnIndex) {
case 0:
return String.class;
case 1:
return UUID.class;
case 2:
return Boolean.class;
}
// default value
return super.getColumnClass(columnIndex);
}
#Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return columnIndex == 2;
}
#Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
SingleClient client = clientList.get(rowIndex);
switch (columnIndex) {
case 0:
break;
case 1:
break;
case 2:
boolean isLocal = (boolean) aValue;
client.setLocal(isLocal);
default:
break;
}
}
}
class SingleClient {
private String ip;
private UUID uuid;
private boolean isLocal;
public SingleClient(String ip, UUID uuid2, boolean isLocal) {
this.ip = ip;
this.uuid = uuid2;
this.isLocal = isLocal;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public UUID getUuid() {
return uuid;
}
public void setUuid(UUID uuid) {
this.uuid = uuid;
}
public boolean isLocal() {
return isLocal;
}
public void setLocal(boolean isLocal) {
this.isLocal = isLocal;
}
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((ip == null) ? 0 : ip.hashCode());
result = prime * result + ((uuid == null) ? 0 : uuid.hashCode());
return result;
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
SingleClient other = (SingleClient) obj;
if (ip == null) {
if (other.ip != null)
return false;
} else if (!ip.equals(other.ip))
return false;
if (uuid == null) {
if (other.uuid != null)
return false;
} else if (!uuid.equals(other.uuid))
return false;
return true;
}
}

Thanks to the help I was able to get it working. I'll just add my solution including changes for reference and further comments:
public class SingleClientPanel extends JPanel
{
private JTextField ipTextfield;
private JTextField uuidTextField;
public SingleClientPanel()
{
this("127.000.000.001",UUID.randomUUID().toString(),true);
}
public SingleClientPanel(String ip, String uuid,boolean isLocal)
{
/*
removed:
setSize(new Dimension(440, 35));
setPreferredSize(new Dimension(440, 35));
setMaximumSize(new Dimension(440, 35));
setMinimumSize(new Dimension(440, 35));*/
setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
setLayout(new BoxLayout(this, BoxLayout.X_AXIS)); // added this
ipTextfield = new JTextField();
ipTextfield.setHorizontalAlignment(SwingConstants.CENTER);
ipTextfield.setAlignmentX(Component.LEFT_ALIGNMENT);
ipTextfield.setFocusable(false);
ipTextfield.setEditable(false);
add(ipTextfield);
ipTextfield.setColumns(15);
ipTextfield.setText(ip);
uuidTextField = new JTextField();
uuidTextField.setHorizontalAlignment(SwingConstants.CENTER);
uuidTextField.setEditable(false);
uuidTextField.setFocusable(false);
add(uuidTextField);
uuidTextField.setColumns(30);
uuidTextField.setText(uuid);
JButton button = new JButton(">");
button.setEnabled(!isLocal);
add(button);
this.revalidate();
}
}
public class ClientOverviewPanel extends JPanel
{
public ClientOverviewPanel()
{
setLayout(new BorderLayout(0, 0));
JButton btnOk = new JButton("Ok");
btnOk.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
Window x = SwingUtilities.getWindowAncestor(ClientOverviewPanel.this);
if(x != null) x.dispose();
}
});
add(btnOk, BorderLayout.SOUTH);
JScrollPane scrollPane = new JScrollPane();
/*
removed:
scrollPane.setPreferredSize(new Dimension(480, 480));
scrollPane.setSize(new Dimension(480, 480));
scrollPane.setMinimumSize(new Dimension(480, 40));*/
scrollPane.setViewportBorder(null);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
Box box = new Box(BoxLayout.PAGE_AXIS); //added
for (int i=0; i<5;i++)
{
SingleClientPanel cpan = new SingleClientPanel();
//cpan.setLocation(0, 45 *i); removed
box.add(cpan); //changed
}
scrollPane.setViewportView(box); //changed
add(scrollPane, BorderLayout.CENTER);
this.revalidate();
}
}

Related

MouseClick Event - Multiple Selection

Currently in this project, I'm able to select the specific grid and change it to a blue background with the SHIFT key being hold.
Right now, I would like to select multiple grids and assign new background color all at once.
How can I do so?
Source code:
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import javax.swing.border.LineBorder;
//based on #trashgod code from http://stackoverflow.com/a/7706684/714968
public class FocusingPanel extends JFrame {
private static final long serialVersionUID = 1L;
private int elements = 10;
private List<GridPanel> list = new ArrayList<GridPanel>();
private final JFrame mainFrame = new JFrame();
private final JPanel fatherPanel = new JPanel();
public FocusingPanel() {
fatherPanel.setLayout(new GridLayout(elements, elements));
for (int i = 0; i < elements * elements; i++) {
int row = i / elements;
int col = i % elements;
GridPanel gb = new GridPanel(row, col);
list.add(gb);
fatherPanel.add(gb);
}
mainFrame.setLayout(new BorderLayout(5, 5));
mainFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
mainFrame.add(fatherPanel, BorderLayout.CENTER);
mainFrame.pack();
mainFrame.setVisible(true);
}
private GridPanel getGridPanel(int r, int c) {
int index = r * elements + c;
return list.get(index);
}
private class GridPanel extends JPanel {
private int row;
private int col;
#Override
public Dimension getPreferredSize() {
return new Dimension(20, 20);
}
public GridPanel(int row, int col) {
this.row = row;
this.col = col;
this.setBackground(Color.red);
this.setBorder(new LineBorder(Color.black,1));
this.addMouseListener(new MouseListener() {
//*#Override
public void mouseEntered(MouseEvent e) {
}
#Override
public void mousePressed(MouseEvent e) {
if(e.isShiftDown()){
setBackground(Color.blue);
}
}
#Override
public void mouseReleased(MouseEvent e) {
//setBackground(Color.green);
}
#Override
public void mouseClicked(MouseEvent e) {
}
#Override
public void mouseExited(MouseEvent e) {
//setBackground(Color.red);
}
});
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
FocusingPanel focusingPanel = new FocusingPanel();
}
});
}
}

How can I add to or change a label on a JMenuItem?

I need to add a label on the right hand side of a JMenuItem, like the labels shown below:
I have an application that uses Ctrl++ and Ctrl+- to zoom into an image. However, the + key by default (without Shift) is the = key. When I try adding accelerators for these menu items, the Ctrl+- shortcut label displays as "Ctrl+Minus" (I would prefer "Ctrl -") and the Ctrl++ shortcut label displays as "Ctrl+Equals" (even worse - in the interest of user experience, I would prefer "Ctrl +"):
menuBar_view_zoomIn.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_EQUALS, ActionEvent.CTRL_MASK));
I want Ctrl++ to display as "Ctrl +" and Ctrl+- to display as "Ctrl -". How can this be done?
not an answer, you need to search for
paint() to heavy, paintComponent() for lightweight JPopup, JMenu (for custom painting can be switch to isHeavyWeight...)
overlay JLabel into container (few question about JTable by #Guillaume Polet and #Robin)
create own JMenu/JPopup (see my comment to your question)
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.plaf.basic.BasicArrowButton;
public class ComboBoxMenuExample extends JFrame {
public ComboBoxMenuExample() {
super("ComboBoxMenu Example");
String[] itemStr = {"name", "Red", "Blue", "number", "255,0,0", "0,0,255",
/*separator*/ "system", "control", "controlHighlight", "controlShadow", "text"};
JMenuItem[] menuItems = new JMenuItem[7];
menuItems[0] = new JMenuItem(itemStr[1]);
menuItems[1] = new JMenuItem(itemStr[2]);
menuItems[2] = new JMenuItem(itemStr[4]);
menuItems[3] = new JMenuItem(itemStr[5]);
menuItems[4] = new JMenuItem(itemStr[8]);
menuItems[5] = new JMenuItem(itemStr[9]);
menuItems[6] = new JMenuItem(itemStr[10]);
JMenu[] menus = new JMenu[4];
menus[0] = new JMenu(itemStr[0]);
menus[1] = new JMenu(itemStr[3]);
menus[2] = new JMenu(itemStr[6]);
menus[3] = new JMenu(itemStr[7]);
menus[0].add(menuItems[0]);
menus[0].add(menuItems[1]);
menus[1].add(menuItems[2]);
menus[1].add(menuItems[3]);
menus[3].add(menuItems[4]);
menus[3].add(menuItems[5]);
menus[2].add(menus[3]);
menus[2].add(menuItems[6]);
JMenu menu = ComboMenuBar.createMenu(menuItems[0].getText());
menu.add(menus[0]);
menu.add(menus[1]);
menu.addSeparator();
menu.add(menus[2]);
ComboMenuBar comboMenu = new ComboMenuBar(menu);
JComboBox combo = new JComboBox();
combo.addItem(itemStr[1]);
combo.addItem(itemStr[2]);
combo.addItem(itemStr[4]);
combo.addItem(itemStr[5]);
combo.addItem(itemStr[8]);
combo.addItem(itemStr[9]);
combo.addItem(itemStr[10]);
getContentPane().setLayout(new FlowLayout());
getContentPane().add(new ComboPanel("Fake ComboBox", comboMenu));
getContentPane().add(new ComboPanel("ComboBox", combo));
}
class ComboPanel extends JPanel {
ComboPanel(String title, JComponent c) {
setLayout(new FlowLayout());
setBorder(new TitledBorder(title));
add(c);
}
}
public static void main(String args[]) {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception evt) {
}
ComboBoxMenuExample frame = new ComboBoxMenuExample();
frame.addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setSize(370, 100);
frame.setVisible(true);
}
}
class ComboMenuBar extends JMenuBar {
JMenu menu;
Dimension preferredSize;
public ComboMenuBar(JMenu menu) {
this.menu = menu;
Color color = UIManager.getColor("Menu.selectionBackground");
UIManager.put("Menu.selectionBackground", UIManager.getColor("Menu.background"));
UIManager.put("Menu.selectionBackground", color);
menu.updateUI();
MenuItemListener listener = new MenuItemListener();
setListener(menu, listener);
add(menu);
}
class MenuItemListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
JMenuItem item = (JMenuItem) e.getSource();
menu.setText(item.getText());
menu.requestFocus();
}
}
private void setListener(JMenuItem item, ActionListener listener) {
if (item instanceof JMenu) {
JMenu menu1 = (JMenu) item;
int n = menu1.getItemCount();
for (int i = 0; i < n; i++) {
setListener(menu1.getItem(i), listener);
}
} else if (item != null) { // null means separator
item.addActionListener(listener);
}
}
public String getSelectedItem() {
return menu.getText();
}
#Override
public void setPreferredSize(Dimension size) {
preferredSize = size;
}
#Override
public Dimension getPreferredSize() {
if (preferredSize == null) {
Dimension sd = super.getPreferredSize();
Dimension menuD = getItemSize(menu);
Insets margin = menu.getMargin();
Dimension retD = new Dimension(menuD.width, margin.top
+ margin.bottom + menuD.height);
menu.setPreferredSize(retD);
preferredSize = retD;
}
return preferredSize;
}
private Dimension getItemSize(JMenu menu) {
Dimension d = new Dimension(0, 0);
int n = menu.getItemCount();
for (int i = 0; i < n; i++) {
Dimension itemD;
JMenuItem item = menu.getItem(i);
if (item instanceof JMenu) {
itemD = getItemSize((JMenu) item);
} else if (item != null) {
itemD = item.getPreferredSize();
} else {
itemD = new Dimension(0, 0); // separator
}
d.width = Math.max(d.width, itemD.width);
d.height = Math.max(d.height, itemD.height);
}
return d;
}
public static class ComboMenu extends JMenu {
ArrowIcon iconRenderer;
public ComboMenu(String label) {
super(label);
iconRenderer = new ArrowIcon(SwingConstants.SOUTH, true);
setBorder(new EtchedBorder());
setIcon(new BlankIcon(null, 11));
setHorizontalTextPosition(JButton.LEFT);
setFocusPainted(true);
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Dimension d = this.getPreferredSize();
int x = Math.max(0, d.width - iconRenderer.getIconWidth() - 3);
int y = Math.max(0,
(d.height - iconRenderer.getIconHeight()) / 2 - 2);
iconRenderer.paintIcon(this, g, x, y);
}
}
public static JMenu createMenu(String label) {
return new ComboMenu(label);
}
}
class ArrowIcon implements Icon, SwingConstants {
private static final int DEFAULT_SIZE = 11;
//private static final int DEFAULT_SIZE = 5;
private int size;
private int iconSize;
private int direction;
private boolean isEnabled;
private BasicArrowButton iconRenderer;
public ArrowIcon(int direction, boolean isPressedView) {
this(DEFAULT_SIZE, direction, isPressedView);
}
public ArrowIcon(int iconSize, int direction, boolean isEnabled) {
this.size = iconSize / 2;
this.iconSize = iconSize;
this.direction = direction;
this.isEnabled = isEnabled;
iconRenderer = new BasicArrowButton(direction);
}
#Override
public void paintIcon(Component c, Graphics g, int x, int y) {
iconRenderer.paintTriangle(g, x, y, size, direction, isEnabled);
}
#Override
public int getIconWidth() {
//int retCode;
switch (direction) {
case NORTH:
case SOUTH:
return iconSize;
case EAST:
case WEST:
return size;
}
return iconSize;
}
#Override
public int getIconHeight() {
switch (direction) {
case NORTH:
case SOUTH:
return size;
case EAST:
case WEST:
return iconSize;
}
return size;
}
}
class BlankIcon implements Icon {
private Color fillColor;
private int size;
public BlankIcon() {
this(null, 11);
}
public BlankIcon(Color color, int size) {
//UIManager.getColor("control")
//UIManager.getColor("controlShadow")
fillColor = color;
this.size = size;
}
#Override
public void paintIcon(Component c, Graphics g, int x, int y) {
if (fillColor != null) {
g.setColor(fillColor);
g.drawRect(x, y, size - 1, size - 1);
}
}
#Override
public int getIconWidth() {
return size;
}
#Override
public int getIconHeight() {
return size;
}
}

Having multi-line text on a ListCell using JList and ListCellRenderer

This class instance is returned by my ListCellRenderer:
public class SessionQALine extends JPanel {
private JTextArea question;
private JLabel answer;
public SessionQALine() {
setLayout(new BoxLayout(this,BoxLayout.X_AXIS));
setOpaque(false);
question = new JTextArea();
question.setLineWrap(true);
question.setWrapStyleWord(true);
question.setFont(new Font(Font.SERIF, Font.ITALIC|Font.BOLD, 14));
question.setOpaque(false);
answer = new JLabel();
answer.setFont(new Font(Font.SERIF, Font.BOLD, 10));
answer.setOpaque(false);
add(question);
add(Box.createHorizontalGlue());
add(answer);
}
public void setQuestion(String q) {
question.setText(q);
}
public void setAnswer(String q) {
answer.setText(q);
}
#Override
public void paint(Graphics g) {
super.paint(g);
g.drawLine(0, getHeight()-1, getWidth(), getHeight()-1);
}
}
This is part of my ListCellRenderer:
private SessionQALine qaLine = new SessionQALine();
#Override
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
qaLine.setQuestion(questions.get(index));
qaLine.setAnswer(answers.get(index));
return qaLine;
}
everything works fine when the strings fit in one line, though if it needs more then one line, the second line and on seems not to be taken under consideration, the JList paints the cells and these line just never show, do you know how i can fix that?
for JList or JTable is easiest to use doLayout(), rather than getPreferredSize from java.swing.text.FieldView
most comfortable will be to put JTextArea to the JScrollPane, for quite nicer output to the GUI,
notice then have to redirect MouseScrollEvents from parent JScrollPane to the JScrollPane under Mouse Cursor, (five-six code_lines moreover a few times solved here)
use Borders or JSeparator instead of drawLine()
do you meaning
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.text.*;
//http://tips4java.wordpress.com/2008/10/26/text-utilities/
public class AutoWrapTest {
public JComponent makeUI() {
String[] columnNames = {" Text Area Cell Renderer "};
Object[][] data = {
{"123456789012345678901234567890"},
{"dddddddddddddddddddddddddddddddddddddddddddddddddddddddddx"},
{"----------------------------------------------0"},
{">>>>>>>>>>>>>dddddddddddddddddddddddddddddddddddddddddddddddddd"
+ "dddddddxdddddddddddddddddddddddddddddddddddddddddddddd"
+ "dddddddddddx>>>>>>>>>>>>>>>>>>>>>>>>>|"},
{">>>>>>>>>>>>ddddddddddddddddddddddddddddddddddddddddddddddddddd"
+ "ddddddx>>>>>>>>>>>>>>>>>>>>>>>>>>|"},
{"a|"},
{">>>>>>>>bbbb>>>>>>>>>>>>>>>>>>>|"},
{">>>>>>>>>>>>>>>>>>|"},
{">>>>>>>>>>>>>dddddddddddddddddddddddddddddddddddddddddddddddddd"
+ "dddddddxdddddddddddddd123456789012345678901234567890dddddd"
+ "dddddddddddddddddddddddddddddddddddddx>>>>>>>>>>>>>>>>>>>>"
+ ">>>>>|"},
{">>>>>>>>>>>>>dddddddddddddd123456789012345678901234567890dddddd"
+ "dddddddddddddddddddddddddddddddddddddxdddddddddddddd123456"
+ "789012345678901234567890dddddddddddddddddddddddddddddddddd"
+ "ddddd123456789012345678901234567890ddddx>>>>>>>>>>>>>>>>>>"
+ ">>>>>>>|"},};
TableModel model = new DefaultTableModel(data, columnNames) {
private static final long serialVersionUID = 1L;
#Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
JTable table = new JTable(model) {
private static final long serialVersionUID = 1L;
#Override
public void doLayout() {
TableColumn col = getColumnModel().getColumn(0);
for (int row = 0; row < getRowCount(); row++) {
Component c = prepareRenderer(col.getCellRenderer(), row, 0);
if (c instanceof JTextArea) {
JTextArea a = (JTextArea) c;
int h = getPreferredHeight(a) + getIntercellSpacing().height;
if (getRowHeight(row) != h) {
setRowHeight(row, h);
}
}
}
super.doLayout();
}
private int getPreferredHeight(JTextComponent c) {
Insets insets = c.getInsets();
View view = c.getUI().getRootView(c).getView(0);
int preferredHeight = (int) view.getPreferredSpan(View.Y_AXIS);
return preferredHeight + insets.top + insets.bottom;
}
};
table.setEnabled(false);
table.setShowGrid(false);
table.setTableHeader(null);
table.getColumnModel().getColumn(0).setCellRenderer(new TextAreaCellRenderer());
//table.setPreferredScrollableViewportSize(table.getPreferredSize());
JScrollPane sp = new JScrollPane(table);
sp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
sp.setPreferredSize(new Dimension(250, 533));
JPanel p = new JPanel(new BorderLayout());
p.add(sp);
return p;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new AutoWrapTest().makeUI());
f.setLocation(100, 100);
f.pack();
f.setVisible(true);
}
}
class TextAreaCellRenderer extends JTextArea implements TableCellRenderer {
private static final long serialVersionUID = 1L;
private final Color evenColor = new Color(230, 240, 255);
public TextAreaCellRenderer() {
super();
setLineWrap(true);
setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
}
#Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
if (isSelected) {
setForeground(table.getSelectionForeground());
setBackground(table.getSelectionBackground());
} else {
setForeground(table.getForeground());
setBackground(table.getBackground());
setBackground((row % 2 == 0) ? evenColor : getBackground());
}
setFont(table.getFont());
setText((value == null) ? "" : value.toString());
return this;
}
}

Animations when using Gridbag Layout.

I have recently started Java and wondered if it was possible to make Animations whilst using GridBag Layout.
Are these possible and how? Any tutorials, help and such would be greatly appreciated :)
In order to perform any kind of animation of this nature, you're going to need some kind of proxy layout manager.
It needs to determine the current position of all the components, the position that the layout manager would like them to have and then move them into position.
The following example demonstrates the basic idea. The animation engine use is VERY basic and does not include features like slow-in and slow-out fundamentals, but uses a linear approach.
public class TestAnimatedLayout {
public static void main(String[] args) {
new TestAnimatedLayout();
}
public TestAnimatedLayout() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestAnimatedLayoutPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestAnimatedLayoutPane extends JPanel {
public TestAnimatedLayoutPane() {
setLayout(new AnimatedLayout(new GridBagLayout()));
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
add(new JLabel("Value:"), gbc);
gbc.gridx++;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1;
add(new JComboBox(), gbc);
gbc.gridx = 0;
gbc.gridy++;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.gridwidth = 2;
add(new JScrollPane(new JTextArea()), gbc);
gbc.gridwidth = 0;
gbc.gridy++;
gbc.gridx++;
gbc.weightx = 0;
gbc.weighty = 0;
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.EAST;
add(new JButton("Click"), gbc);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
public class AnimatedLayout implements LayoutManager2 {
private LayoutManager2 proxy;
private Map<Component, Rectangle> mapStart;
private Map<Component, Rectangle> mapTarget;
private Map<Container, Timer> mapTrips;
private Map<Container, Animator> mapAnimators;
public AnimatedLayout(LayoutManager2 proxy) {
this.proxy = proxy;
mapTrips = new WeakHashMap<>(5);
mapAnimators = new WeakHashMap<>(5);
}
#Override
public void addLayoutComponent(String name, Component comp) {
proxy.addLayoutComponent(name, comp);
}
#Override
public void removeLayoutComponent(Component comp) {
proxy.removeLayoutComponent(comp);
}
#Override
public Dimension preferredLayoutSize(Container parent) {
return proxy.preferredLayoutSize(parent);
}
#Override
public Dimension minimumLayoutSize(Container parent) {
return proxy.minimumLayoutSize(parent);
}
#Override
public void layoutContainer(Container parent) {
Timer timer = mapTrips.get(parent);
if (timer == null) {
System.out.println("...create new trip");
timer = new Timer(125, new TripAction(parent));
timer.setRepeats(false);
timer.setCoalesce(false);
mapTrips.put(parent, timer);
}
System.out.println("trip...");
timer.restart();
}
protected void doLayout(Container parent) {
System.out.println("doLayout...");
mapStart = new HashMap<>(parent.getComponentCount());
for (Component comp : parent.getComponents()) {
mapStart.put(comp, (Rectangle) comp.getBounds().clone());
}
proxy.layoutContainer(parent);
LayoutConstraints constraints = new LayoutConstraints();
for (Component comp : parent.getComponents()) {
Rectangle bounds = comp.getBounds();
Rectangle startBounds = mapStart.get(comp);
if (!mapStart.get(comp).equals(bounds)) {
comp.setBounds(startBounds);
constraints.add(comp, startBounds, bounds);
}
}
System.out.println("Items to layout " + constraints.size());
if (constraints.size() > 0) {
Animator animator = mapAnimators.get(parent);
if (animator == null) {
animator = new Animator(parent, constraints);
mapAnimators.put(parent, animator);
} else {
animator.setConstraints(constraints);
}
animator.restart();
} else {
if (mapAnimators.containsKey(parent)) {
Animator animator = mapAnimators.get(parent);
animator.stop();
mapAnimators.remove(parent);
}
}
}
#Override
public void addLayoutComponent(Component comp, Object constraints) {
proxy.addLayoutComponent(comp, constraints);
}
#Override
public Dimension maximumLayoutSize(Container target) {
return proxy.maximumLayoutSize(target);
}
#Override
public float getLayoutAlignmentX(Container target) {
return proxy.getLayoutAlignmentX(target);
}
#Override
public float getLayoutAlignmentY(Container target) {
return proxy.getLayoutAlignmentY(target);
}
#Override
public void invalidateLayout(Container target) {
proxy.invalidateLayout(target);
}
protected class TripAction implements ActionListener {
private Container container;
public TripAction(Container container) {
this.container = container;
}
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("...trip");
mapTrips.remove(container);
doLayout(container);
}
}
}
public class LayoutConstraints {
private List<AnimationBounds> animationBounds;
public LayoutConstraints() {
animationBounds = new ArrayList<AnimationBounds>(25);
}
public void add(Component comp, Rectangle startBounds, Rectangle targetBounds) {
add(new AnimationBounds(comp, startBounds, targetBounds));
}
public void add(AnimationBounds bounds) {
animationBounds.add(bounds);
}
public int size() {
return animationBounds.size();
}
public AnimationBounds[] getAnimationBounds() {
return animationBounds.toArray(new AnimationBounds[animationBounds.size()]);
}
}
public class AnimationBounds {
private Component component;
private Rectangle startBounds;
private Rectangle targetBounds;
public AnimationBounds(Component component, Rectangle startBounds, Rectangle targetBounds) {
this.component = component;
this.startBounds = startBounds;
this.targetBounds = targetBounds;
}
public Rectangle getStartBounds() {
return startBounds;
}
public Rectangle getTargetBounds() {
return targetBounds;
}
public Component getComponent() {
return component;
}
public Rectangle getBounds(float progress) {
return calculateProgress(getStartBounds(), getTargetBounds(), progress);
}
}
public static Rectangle calculateProgress(Rectangle startBounds, Rectangle targetBounds, float progress) {
Rectangle bounds = new Rectangle();
if (startBounds != null && targetBounds != null) {
bounds.setLocation(calculateProgress(startBounds.getLocation(), targetBounds.getLocation(), progress));
bounds.setSize(calculateProgress(startBounds.getSize(), targetBounds.getSize(), progress));
}
return bounds;
}
public static Point calculateProgress(Point startPoint, Point targetPoint, float progress) {
Point point = new Point();
if (startPoint != null && targetPoint != null) {
point.x = calculateProgress(startPoint.x, targetPoint.x, progress);
point.y = calculateProgress(startPoint.y, targetPoint.y, progress);
}
return point;
}
public static Dimension calculateProgress(Dimension startSize, Dimension targetSize, float progress) {
Dimension size = new Dimension();
if (startSize != null && targetSize != null) {
size.width = calculateProgress(startSize.width, targetSize.width, progress);
size.height = calculateProgress(startSize.height, targetSize.height, progress);
}
return size;
}
public static int calculateProgress(int startValue, int endValue, float fraction) {
int value = 0;
int distance = endValue - startValue;
value = (int) ((float) distance * fraction);
value += startValue;
return value;
}
public class Animator implements ActionListener {
private Timer timer;
private LayoutConstraints constraints;
private int tick;
private Container parent;
public Animator(Container parent, LayoutConstraints constraints) {
setConstraints(constraints);
timer = new Timer(16, this);
timer.setRepeats(true);
timer.setCoalesce(true);
this.parent = parent;
}
private void setConstraints(LayoutConstraints constraints) {
this.constraints = constraints;
}
public void restart() {
tick = 0;
timer.restart();
}
protected void stop() {
timer.stop();
tick = 0;
}
#Override
public void actionPerformed(ActionEvent e) {
tick += 16;
float progress = (float)tick / (float)1000;
if (progress >= 1f) {
progress = 1f;
timer.stop();
}
for (AnimationBounds ab : constraints.getAnimationBounds()) {
Rectangle bounds = ab.getBounds(progress);
Component comp = ab.getComponent();
comp.setBounds(bounds);
comp.invalidate();
comp.repaint();
}
parent.repaint();
}
}
}
Update
You could also take a look at AurelianRibbon/Sliding-Layout
This is the program which i did long time back when i just started my Java classes.I did simple animations on different tabs on JTabbedPane (change the path of images/sound file as required),hope this helps:
UPDATE:
Sorry about not following concurrency in Swing.
Here is updated answer:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class ClassTestHello extends JApplet {
private static JPanel j1;
private JLabel jl;
private JPanel j2;
private Timer timer;
private int i = 0;
private int[] a = new int[10];
#Override
public void init() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
start();
paint();
}
});
}
public void paint() {
jl = new JLabel("hiii");
j1.add(jl);
a[0] = 1000;
a[1] = 800;
a[2] = 900;
a[3] = 2000;
a[4] = 500;
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
if(i % 2 == 0)
jl.setText("hiii");
else
jl.setText("byee");
i++;
if(i > 4)
i=0;
timer.setDelay(a[i]);
}
};
timer = new Timer(a[i], actionListener);
timer.setInitialDelay(0);
timer.start();
}
#Override
public void start() {
j1 = new JPanel();
j2 = new JPanel();
JTabbedPane jt1 = new JTabbedPane();
ImageIcon ic = new ImageIcon("e:/guitar.gif");
JLabel jLabel3 = new JLabel(ic);
j2.add(jLabel3);
jt1.add("one", j1);
jt1.addTab("hii", j2);
getContentPane().add(jt1);
}
}

How to make a JTable column to contain not JTextFields, but JTextAreas?

Please tell me, how to make a JTable column to contain JTextAreas,
so the cell's height will increase when user types a lot of text and
we can see more than one line (cell gets expanded; as result, row will expand too)
You need to write your own cell renderer and editor based on JTextArea:
public class Start
{
public static class JTextPaneCellEditor extends AbstractCellEditor implements TableCellEditor, KeyListener
{
private JViewport viewport;
private JTable table;
private int row;
private JTextPane pane;
public JTextPaneCellEditor(){
viewport = new JViewport();
pane = new JTextPane();
viewport.add(pane);
pane.addKeyListener(this);
}
#Override public Object getCellEditorValue(){
return pane.getText();
}
#Override public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column){
this.table = table;
this.row = row;
pane.setText(value.toString());
int newHeight = (int)pane.getPreferredSize().getHeight();
if(table.getRowHeight(row) < newHeight){
table.setRowHeight(row, newHeight);
}
return pane;
}
#Override public boolean isCellEditable(EventObject e){
if (e instanceof MouseEvent) {
return ((MouseEvent)e).getClickCount() >= 2;
}
return true;
}
#Override public void keyTyped(KeyEvent e){
table.setRowHeight(row, (int)pane.getPreferredSize().getHeight());
}
#Override public void keyPressed(KeyEvent e){
if(e.getKeyCode() == KeyEvent.VK_ENTER){
stopCellEditing();
}
}
#Override public void keyReleased(KeyEvent e){
}
}
private static class JTextPaneCellRenderer extends JViewport implements TableCellRenderer
{
JTextPane pane;
JTextPaneCellRenderer(){
pane = new JTextPane();
add(pane);
}
#Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column){
pane.setText(value.toString());
table.setRowHeight(row, (int)pane.getPreferredSize().getHeight());
return this;
}
}
public static void main(String[] args){
JTable table = new JTable(new String[][]{{"String String String String StringString String String String StringString String String String StringString String String String StringString String String String String"}, {"String 2"}}, new String[]{"Column"});
table.setDefaultRenderer(Object.class, new JTextPaneCellRenderer());
table.setDefaultEditor(Object.class, new JTextPaneCellEditor());
JFrame test = new JFrame();
test.add(new JScrollPane(table));
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test.setSize(300, 300);
test.setLocationRelativeTo(null);
test.setVisible(true);
}
}
EDIT: add viewports for better sizing. But the row is still not expanded on first edit. Does anyone have any ideas?
EDIT2: I agree with the comments. The thing you want is possible, but you need untested, hackish custom implementation. You will be better off if you redesigned your layout to exclude such JTable sorcery.
#Jakub Zaverka
I'll delete this answer later
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.text.*;
public class AutoWrapTest {
public JComponent makeUI() {
String[] columnNames = {"TextAreaCellRenderer"};
Object[][] data = {
{"123456789012345678901234567890"},
{"dddddddddddddddddddddddddddddddddddddddddddddddddddddddddx"},
{"----------------------------------------------0"},
{">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>|"},};
TableModel model = new DefaultTableModel(data, columnNames) {
private static final long serialVersionUID = 1L;
#Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
JTable table = new JTable(model) {
private static final long serialVersionUID = 1L;
#Override
public void doLayout() {
TableColumn col = getColumnModel().getColumn(0);
for (int row = 0; row < getRowCount(); row++) {
Component c = prepareRenderer(col.getCellRenderer(), row, 0);
if (c instanceof JTextArea) {
JTextArea a = (JTextArea) c;
int h = getPreferredHeight(a) + getIntercellSpacing().height;
if (getRowHeight(row) != h) {
setRowHeight(row, h);
}
}
}
super.doLayout();
} //http://tips4java.wordpress.com/2008/10/26/text-utilities/
private int getPreferredHeight(JTextComponent c) {
Insets insets = c.getInsets();
View view = c.getUI().getRootView(c).getView(0);
int preferredHeight = (int) view.getPreferredSpan(View.Y_AXIS);
return preferredHeight + insets.top + insets.bottom;
}
};
table.setEnabled(false);
table.setShowGrid(false);
table.setTableHeader(null);
table.getColumnModel().getColumn(0).setCellRenderer(new TextAreaCellRenderer());
JScrollPane sp = new JScrollPane(table);
sp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
JPanel p = new JPanel(new BorderLayout());
p.add(sp);
return p;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new AutoWrapTest().makeUI());
f.setSize(200, 200);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
class TextAreaCellRenderer extends JTextArea implements TableCellRenderer {
private static final long serialVersionUID = 1L;
private final Color evenColor = new Color(230, 240, 255);
public TextAreaCellRenderer() {
super();
setLineWrap(true);
setWrapStyleWord(true);
setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
}
#Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
if (isSelected) {
setForeground(table.getSelectionForeground());
setBackground(table.getSelectionBackground());
} else {
setForeground(table.getForeground());
setBackground(table.getBackground());
setBackground((row % 2 == 0) ? evenColor : getBackground());
}
setFont(table.getFont());
setText((value == null) ? "" : value.toString());
return this;
}
}

Categories

Resources