I'm trying to create a window with a scrollable JTextArea and a JTextField below it. I want the frame to look like a chat window; one, large scrollable text area and a single lined text frame. I've tried variations but I can't get the text area scrollable without making the entire window scrollable. It's incredibly annoying. My current iteration only draws one panel to the screen:
private void buildGUI() {
Container chatClientContainer = getContentPane();
chatClientContainer.setLayout(new BorderLayout());
JPanel messagesReceivedPanel = new JPanel();
messagesReceivedPanel.setLayout(new GridLayout(1, 1, 5, 5));
JTextArea messagesReceived = new JTextArea("area");
messagesReceivedPanel.add(messagesReceived);
JPanel draftPanel = new JPanel();
draftPanel.setLayout(new GridLayout(1, 1, 5, 5));
JTextField draftMessage = new JTextField("field");
draftPanel.add(draftMessage);
chatClientContainer.add(new JScrollPane(messagesReceivedPanel));
chatClientContainer.add(draftPanel);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int windowWidth = 400;
int windowHeight = 600;
int posX = ((int) screenSize.getWidth())/2 - windowWidth/2;
int posY = (int) screenSize.getHeight()/2 - windowHeight/2;
setBounds(posX, posY, windowWidth, windowHeight);
setResizable(true);
setVisible(true);
}
How can I position this the way I want?
Why not just use BorderLayout? Place the JTextArea's JScrollPane BorderLayout.CENTER and the JTextField (not JTextFrame) BorderLayout.PAGE_END.
For example:
import java.awt.BorderLayout;
import javax.swing.*;
public class ChatPanel extends JPanel {
private static final long serialVersionUID = 1L;
private static final int ROWS = 15;
private static final int COLS = 30;
private JTextArea textArea = new JTextArea(ROWS, COLS);
private JTextField textField = new JTextField(COLS);
public ChatPanel() {
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
setLayout(new BorderLayout());
add(scrollPane, BorderLayout.CENTER);
add(textField, BorderLayout.PAGE_END);
}
private static void createAndShowGUI() {
ChatPanel paintEg = new ChatPanel();
JFrame frame = new JFrame("ChatPanel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(paintEg);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
Related
Trying to read and write a text document, everything is working but the horizontal scrollbar isn't visible.
I tried to activate the JScrollPane horizontal scrollbar manually but that wasnt a result.
public class JScrollPaneÜbung extends JFrame
{
private static final long serialVersionUID = 1L;
private JTextArea area;
private JTextField field;
private JScrollPane scroll;
private JButton dateisuche;
private JButton dateispeichern;
private Panel panel;
private Panel sPanel;
public JScrollPaneÜbung()
{
area = new JTextArea(32, 41);
field = new JTextField(30);
scroll = new JScrollPane(area);
dateispeichern = new JButton("Speichern");
dateisuche = new JButton("Durchsuchen");
panel = new Panel();
sPanel = new Panel();
createGUI();
}
public void createGUI() {
setBounds(200, 200, 600, 600);
BorderLayout b = new BorderLayout();
setLayout(b);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
dateisuche.addActionListener(new JScrollPaneListener(this));
dateispeichern.addActionListener(new JScrollPaneListener(this));
panel.add(dateisuche);
panel.add(field);
panel.add(scroll);
sPanel.add(dateispeichern);
add(panel, BorderLayout.NORTH);
add(sPanel, BorderLayout.SOUTH);
setVisible(true);
}
public static void main(String[] args) {
new JScrollPaneÜbung();
}
No horizontal scrollbar visible, but my text document is longer than the JTextArea
You're using a regular JPane. Use a JScrollPane instead. You can find documentation here that will guide you.
Resource
I got a project where I always want to center the focused JPanel. So I thought I can just change the Viewport position. But I can't use the viewport. I created an example project to show how I use the viewport. I just want that the user only see one of the orange boxes. But it should be also possible to view all boxes at once. So the view has to zoom in or something like this. How can I fix this problem?
My example:
import javax.swing.*;
import java.awt.*;
public class main {
public static void main(String [] args){
//create JFrame
JFrame _frame = new JFrame();
//create Viewport
JViewport _view = new JViewport();
//create Mainpanel
JPanel _mainPanel = new JPanel();
//tell the view to handle mainpanel
_view.setView(_mainPanel);
//create Layout
GridLayout _layout = new GridLayout(3,3,3,3);
//set gridlayout to mainpanel
_mainPanel.setLayout(_layout);
for(int i = 0;i<12;i++){
JPanel _tempPanel = new JPanel();
_tempPanel.setBackground(Color.ORANGE);
_tempPanel.setBorder(BorderFactory.createLineBorder(Color.black));
_mainPanel.add(_tempPanel);
}
_view.setExtentSize(new Dimension(300,300));
//add mainpanel to frame
_frame.add(_mainPanel);
_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
_frame.pack();
//set size of Jframe
_frame.setSize(1000,1000);
_frame.setVisible(true);
}
}
JViewPort can not help you with your requirement.
Here is an ugly but running code. You can improve it yourself.
public static void main(String[] args) {
// create JFrame
JFrame _frame = new JFrame();
JPanel conPanel = new JPanel(new BorderLayout());
// create Mainpanel
JPanel _mainPanel = new JPanel() {
#Override
public String toString() {
return "All";
}
};
// create Layout
GridLayout _layout = new GridLayout(3, 3, 3, 3);
// set gridlayout to mainpanel
_mainPanel.setLayout(_layout);
JComboBox<JPanel> combo = new JComboBox<>();
combo.addItem(_mainPanel);
for (int i = 0; i < 12; i++) {
final int fi = i;
JPanel _tempPanel = new JPanel() {
#Override
public String toString() {
return "Panel" + fi;
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawString(toString(), 5, 15);
}
};
_tempPanel.setBackground(Color.ORANGE);
_tempPanel.setBorder(BorderFactory.createLineBorder(Color.black));
_mainPanel.add(_tempPanel);
combo.addItem(_tempPanel);
}
combo.addActionListener( e -> {
JPanel panel = (JPanel)combo.getSelectedItem();
conPanel.remove(_mainPanel);
_mainPanel.removeAll();
for(int i = 1; i < combo.getItemCount(); i++)
_mainPanel.add(combo.getItemAt(i));
conPanel.add(panel, BorderLayout.CENTER);
conPanel.revalidate();
conPanel.repaint();
} );
conPanel.add(_mainPanel, BorderLayout.CENTER);
JPanel buttonsPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
buttonsPanel.add(combo);
conPanel.add(buttonsPanel, BorderLayout.SOUTH);
// add mainpanel to frame
_frame.setContentPane(conPanel);
_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// set size of Jframe
_frame.setSize(1000, 1000);
_frame.setVisible(true);
}
class CipherGUIFrame extends JFrame {
public CipherGUIFrame() {
super("Caesar Cipher GUI");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 600);
JTextArea area1 = new JTextArea();
JTextArea area2 = new JTextArea();
JSpinner myspinner=new JSpinner();
JPanel mainframe = new JPanel();
mainframe.setLayout(new BoxLayout(mainframe, BoxLayout.Y_AXIS));
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
p1.setLayout(new BoxLayout(p1, BoxLayout.Y_AXIS));
p2.setLayout(new BoxLayout(p2, BoxLayout.Y_AXIS));
p1.setBorder(BorderFactory.createTitledBorder("Cleartext"));
p2.setBorder(BorderFactory.createTitledBorder("Spinner"));
p3.setLayout(new BoxLayout(p3, BoxLayout.Y_AXIS));
p3.setBorder(BorderFactory.createTitledBorder("Ciphertext"));
p1.add(area1);
p2.add(myspinner);
p3.add(area2);
mainframe.add(p1);
mainframe.add(p2);
mainframe.add(p3);
this.add(mainframe);
}
}
It seems that this code produces something which looks similar to this:
I am trying to tidy this up so it looks cleaner; is there a way to shrink the middle panel or to make the others bigger to make it look nicer?
Don't set the sizes of anything, but instead set the columns and rows of your JTextAreas. Don't use BoxLayout when you don't want its behaviors. Put your JTextAreas in JScrollPanes instead. And don't forget to pack() your JFrame.
import java.awt.BorderLayout;
import javax.swing.*;
public class Cipher2 extends JPanel {
public static final int ROWS = 12;
public static final int COLS = 30;
private JTextArea textArea1 = new JTextArea(ROWS, COLS);
private JTextArea textArea2 = new JTextArea(ROWS, COLS);
public Cipher2() {
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); // Box OK here
JScrollPane scroll1 = new JScrollPane(textArea1);
add(wrapComponentWithTitle(scroll1, "Fubar"), BorderLayout.PAGE_START);
add(wrapComponentWithTitle(new JSpinner(), "Spinner"), BorderLayout.CENTER);
scroll1 = new JScrollPane(textArea2);
add(wrapComponentWithTitle(scroll1, "Snafu"), BorderLayout.PAGE_END);
}
private JPanel wrapComponentWithTitle(JComponent component, String title) {
// BoxLayout NOT OK here. Use BorderLayout instead
JPanel wrapPanel = new JPanel(new BorderLayout());
wrapPanel.add(component);
wrapPanel.setBorder(BorderFactory.createTitledBorder(title));
return wrapPanel;
}
private static void createAndShowGui() {
Cipher2 mainPanel = new Cipher2();
JFrame frame = new JFrame("Foo");
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();
}
});
}
}
I figured out the answer: change Y_AXIS to X_AXIS.
<3
Please look into the small code below. The scroll pane appears, but the sliders do not.
Even if I resize the frame the sliders do not. Please help.
import javax.swing.*;
public class sample {
static JFrame frame;
public static void main(String[] args) {
String Msg = "Sample Message To Test Scrolling";
frame = new JFrame("Sample Program");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 600);
JPanel panel = new JPanel();
panel.setLayout(null);
for (int ypos = 0, i = 0; i < 40; i++) {
JLabel label = new JLabel("" + i + " " + Msg);
label.setFont(new Font("Courier", Font.BOLD, 12));
panel.add(label);
label.setBounds(10, ypos + 5,
label.getPreferredSize().width,
label.getPreferredSize().height);
ypos += label.getPreferredSize().height;
}
JScrollPane scroll = new JScrollPane(panel,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
frame.setLayout(new BorderLayput());
frame.add(scroll);
frame.setVisible(true);
}
}
The sliders will only appear if and when the component contained by the JScrollPane's viewport is larger than the viewport. Based on your posted code, I don't see why your component would be larger than the viewport as the panel's size will be based on its preferredSize, something that will never change since for one, you're adding components to it with it using a null layout.
As an aside you should almost never use null layout.
For example:
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.*;
public class Sample2 {
private static final int PREF_W = 600;
private static final int PREF_H = PREF_W;
private static final int MAX_ROWS = 400;
private static final String TEXT_BODY = "Sample Message To Test Scrolling";;
private static void createAndShowGui() {
JPanel panel = new JPanel(new GridLayout(0, 1));
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
for (int i = 0; i < MAX_ROWS; i++) {
String text = String.format("%03d %s", i, TEXT_BODY);
JLabel label = new JLabel(text);
panel.add(label);
}
JScrollPane scrollPane = new JScrollPane(panel);
scrollPane.setPreferredSize(new Dimension(PREF_W, PREF_H));
JFrame frame = new JFrame("Sample2");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(scrollPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
I am using Swing and AWT (for the listeners) to make a small program. I have a problem concerning getting the size of my JPanel (the class named Chess).
My Layout:
public class Main extends JFrame implements MouseListener, ActionListener{
Chess chessPanel = new Chess ();
JButton newGameButton = new JButton ("New Game");
JButton loadGameButton = new JButton ("Load Game");
JButton saveGameButton = new JButton ("Save Game");
JButton exitButton = new JButton ("Exit");
public static void main (String [] args) {
new Main();
}
Main () {
super ("Chess");
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
setSize(dim);
setLocation(0,0);
setUndecorated(true);
chessPanel.addMouseListener(this);
add(chessPanel, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
newGameButton.addActionListener(this);
loadGameButton.addActionListener(this);
saveGameButton.addActionListener(this);
exitButton.addActionListener(this);
buttonPanel.add(newGameButton);
buttonPanel.add(loadGameButton);
buttonPanel.add(saveGameButton);
buttonPanel.add(exitButton);
add(buttonPanel, BorderLayout.SOUTH);
setVisible(true);
}
// ... Code ...
}
As you can see by the code, I have one JPanel in the CENTER, which takes nearly all the screen. In the bottom I have another JPanel (SOUTH), which has a row of buttons.
What I need is the size that the JPanel in the CENTER takes. When I call the getWidth(), getHeight() or getBounds() methods inherited from JPanel, they all return 0, because of the BorderLayout.
Any idea how to get the real values?
PS: The screen always takes up the entire screen, and will never be resized, if that helps.
You're likely calling getWidth before the JPanel has been rendered, and so it will be 0. The solution is to get the size after rendering, for instance after pack() or setVisible(true) has been called on the root container that holds this JPanel.
Also, I recommend against calling setSize() on anything since most of the standard layout managers observe the preferred size of a component, not the size, and when you call pack() telling the layout managers to do their thing, the set sizes are usually ignored. You may want to make your JPanel that is in the center set its own size by overriding its setPreferredSize method if it needs to be a certain size. Then let the JFrame and its held containers set the bet fit size based on the their layout managers when you call pack.
e.g.,
import java.awt.*;
import javax.swing.*;
public class Main extends JFrame {
Chess chessPanel = new Chess();
JButton newGameButton = new JButton("New Game");
JButton loadGameButton = new JButton("Load Game");
JButton saveGameButton = new JButton("Save Game");
JButton exitButton = new JButton("Exit");
public static void main(String[] args) {
new Main();
}
Main() {
super("Chess");
add(chessPanel, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
buttonPanel.add(newGameButton);
buttonPanel.add(loadGameButton);
buttonPanel.add(saveGameButton);
buttonPanel.add(exitButton);
System.out.printf("chessPanel Size before rendering: %s%n", chessPanel.getSize());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(buttonPanel, BorderLayout.SOUTH);
pack();
System.out.printf("chessPanel Size after rendering: %s%n", chessPanel.getSize());
setLocationRelativeTo(null);
setVisible(true);
}
// ... Code ...
}
#SuppressWarnings("serial")
class Chess extends JPanel {
private static final int CHESS_WIDTH = 600;
private static final int CHESS_HEIGHT = CHESS_WIDTH;
private static final int MAX_ROW = 8;
private static final int MAX_COL = 8;
private static final Color LIGHT_COLOR = new Color(240, 190, 40);
private static final Color DARK_COLOR = new Color(180, 50, 0);
#Override
public Dimension getPreferredSize() {
return new Dimension(CHESS_WIDTH, CHESS_HEIGHT);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int panelWidth = getWidth();
int panelHeight = getHeight();
int sqrWidth = panelWidth / MAX_ROW;
int sqrHeight = panelHeight / MAX_COL;
for (int row = 0; row < MAX_ROW; row++) {
for (int col = 0; col < MAX_COL; col++) {
Color c = (row % 2 == col % 2) ? LIGHT_COLOR : DARK_COLOR;
g.setColor(c);
int x = (row * panelWidth) / MAX_ROW;
int y = (col * panelHeight) / MAX_COL;
g.fillRect(x, y, sqrWidth, sqrHeight);
}
}
}
}