I'm coding in Java Swing and for some reason, when I add two elements to a gridlayout, they both assume the same position. I have tried simplifying it into something that would not fail and then building up from there, but alas, it's still not working.
The misbehaving code within the program is:
bodyPanelMain.setLayout(new GridLayout(4, 1, 10, 10));
JTextArea one = new JTextArea("Hi");
one.setLineWrap(true);
one.setSize(100, 100);
JTextArea two = new JTextArea("Goodbye");
two.setLineWrap(true);
two.setSize(100, 100);
bodyPanelMain.add(one);
bodyPanelMain.add(two);
bodyPanelMain.repaint();
If I make JTextArea's width 200 and background a different color, it's clear that it's visible behind it, so it's most certainly adding all the proper elements, their positions are just wrong.
EDIT: Here's a very very short version of what I am trying to do.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class minimessageboard extends Applet implements ActionListener {
JPanel mainPanel;
JPanel buttonPanel;
JButton announcements, websites;
JPanel bodyPanel, bodyPanelMain;
public minimessageboard() {
this.setSize(600, 400);
mainPanel = new JPanel(new BorderLayout());
mainPanel.setPreferredSize(new Dimension(this.getWidth(), this.getHeight()));
this.add(mainPanel);
buttonPanel = new JPanel(new GridLayout(6, 1, 10, 10));
mainPanel.add(buttonPanel, BorderLayout.WEST);
announcements = new JButton("Announcements");
this.formatButton(announcements);
announcements.setActionCommand("announcements");
buttonPanel.add(announcements);
websites = new JButton("Websites");
this.formatButton(websites);
websites.setActionCommand("websites");
buttonPanel.add(websites);
bodyPanel = new JPanel(new BorderLayout());
bodyPanel.setSize(200, 500);
bodyPanel.setPreferredSize(new Dimension(200, 500));
mainPanel.add(bodyPanel, BorderLayout.CENTER);
bodyPanelMain = new JPanel(new BorderLayout());
bodyPanel.add(bodyPanelMain, BorderLayout.CENTER);
bodyPanelMain.setLayout(new GridLayout(4, 1, 10, 10));
JButton one = new JButton("Roar");
bodyPanelMain.add(one);
bodyPanelMain.revalidate();
bodyPanelMain.repaint();
}
public static void main(String args[]) {
JFrame overall = new JFrame();
overall.pack();
overall.setVisible(true);
overall.add(new minimessageboard());
}
public void formatButton(JButton b){
b.setPreferredSize(new Dimension(150, 33));
b.addActionListener(this);
}
public void actionPerformed(ActionEvent arg0) {
String action = arg0.getActionCommand();
bodyPanelMain.removeAll();
if (action.equals("websites")){
System.out.println("Fires!");
bodyPanelMain.setLayout(new GridLayout(4, 1, 10, 10));
JButton one = new JButton("Hi");
JButton two = new JButton("Goodbye");
bodyPanelMain.add(one);
bodyPanelMain.add(two);
bodyPanelMain.revalidate();
}
bodyPanelMain.repaint();
}
}
Basically, when you click on websites, "Hi" and "Bye" should show up. If I move the code within the block in the websites if statement (if (action.equals("websites")) up to the original constructor, it appears perfectly fine. The code outputs "Fires!", so I am 100% certain it gets to that part. For note, I changed it from JTextArea to JButton because I will be using JButtons, not JTextArea.
Don't set the size of a JTextArea as it won't work well when your text extends beyond the text area size and you find that it just won't scroll. Instead set the preferred row and column values, and then let the JTextArea size itself. On to your problem: are you adding these components after the GUI has rendered itself? If so, do you call revalidate() on the bodyPanelMain after it receives the JTextAreas? If this doesn't help, consider creating and posting an sscce.
For example, this works fine for me:
import java.awt.GridLayout;
import javax.swing.*;
public class SwingFoo {
private static final int ROWS = 10;
private static final int COLS = 16;
private static void createAndShowGui() {
JPanel bodyPanelMain = new JPanel();
bodyPanelMain.setLayout(new GridLayout(4, 1, 10, 10));
JTextArea one = new JTextArea("Hi", ROWS, COLS);
one.setLineWrap(true);
// one.setSize(100, 100);
JTextArea two = new JTextArea("Goodbye", ROWS, COLS);
two.setLineWrap(true);
// two.setSize(100, 100);
bodyPanelMain.add(new JScrollPane(one));
bodyPanelMain.add(new JScrollPane(two));
// bodyPanelMain.repaint();
JFrame frame = new JFrame("SwingFoo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(bodyPanelMain);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
Related
I have a JTextArea where I want to allow the user to input any number of strings up to 100 but it could be less. When I set the JTextArea as I have in my code below where it is commented out (i.e. //tfResult= new JTextArea(10, 0);) and the user inputs ten lines of strings then my code runs exactly as expected and prints out what I need it to.
But if I try to input more of less lines I get
java.lang.ArrayIndexOutOfBoundsException
followed by the number of lines of user input, whether I have it declared with no bounds or as I have it commented out.
I am new to graphics in java and I can't figure out why this is happening and I have searched everywhere for answers. Do I have the bounds set wrong or have I declared the JTextArea wrong?
I also am trying to include a JScrollPane but I am having issues with that also as its not showing up.
I would really appreciate any help as I am struggling to solve this issue.
class Window {
JFrame windowFrame;
Panel bottomPanel;
JScrollPane scroll;
JTextArea tfResult;
Button btnPlayAgain;
Font font;
Window(int width, int height, String title)
{
windowFrame = new JFrame();
windowFrame.setTitle(title);
windowFrame.setBounds(0,0,width,height);
windowFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
windowFrame.setResizable(true);
windowFrame.setCursor(new Cursor(Cursor.HAND_CURSOR)); // setting cursor to hand
windowFrame.setLayout(null);
createBottomPanel();
windowFrame.add(bottomPanel);
//windowFrame.add(field.getCanvas());
windowFrame.setVisible(true);
}
private void createBottomPanel()
{
JButton b = new JButton("Compute");
bottomPanel = new Panel();
bottomPanel.setBackground(Color.PINK);
bottomPanel.setBounds(0,400,800,140);
bottomPanel.setLayout(null);
//*********
//tfResult= new JTextArea(10, 0);
tfResult= new JTextArea();
tfResult.setBounds(10,10,600,100);
tfResult.setFont(new Font("SansSerif", Font.BOLD, 16));
tfResult.setFocusable(true);
scroll = new JScrollPane(tfResult);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
btnPlayAgain = new Button("Compute");
btnPlayAgain.setBounds(620,10,150,100);
btnPlayAgain.setBackground(Color.RED);
btnPlayAgain.setFont(new Font("SansSerif", Font.BOLD, 24));
btnPlayAgain.setFocusable(true);
bottomPanel.add(tfResult);
bottomPanel.add(btnPlayAgain);
bottomPanel.add(b);
bottomPanel.add(scroll);
tfResult.setVisible(true);
scroll.setVisible(true);
btnPlayAgain.setVisible(true);
bottomPanel.setVisible(true);
btnPlayAgain.addActionListener(new ActionListener()
{
//#Override
public void actionPerformed(ActionEvent e)
{
//should include the code to genrate the output inside here
String input = tfResult.getText();
Mat xy;
xy = new Mat();
//String output = xy.getOutput(input).toString();
String output = xy.getOutput(input);
//String output = Output(input);
tfTarget.setText(output);
}
});
}
}
I went ahead and created the following GUI that allows you to enter data with a JTextArea.
The first thing I did was start my Swing application with a call to the SwingUtilities invokeLater method. This method ensures that the Swing components are created and executed on the Event Dispatch Thread.
Next, I created a JFrame. Then I created a JPanel with a BorderLayout. The JTextArea is inside of a JScrollPane, which is then placed inside of the center of a JPanel.
The JButton is placed after the last line of the JPanel.
Here's the complete runnable code, otherwise known as a minimal runnable example.
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class JTextAreaInputGUI implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new JTextAreaInputGUI());
}
private JTextArea textArea;
#Override
public void run() {
JFrame frame = new JFrame("JTextArea Input GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createMainPanel(), BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createMainPanel() {
JPanel panel = new JPanel(new BorderLayout());
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
textArea = new JTextArea(10, 40);
JScrollPane scrollPane = new JScrollPane(textArea);
panel.add(scrollPane, BorderLayout.CENTER);
JButton button = new JButton("Submit");
button.addActionListener(new ButtonListener());
panel.add(button, BorderLayout.AFTER_LAST_LINE);
return panel;
}
public class ButtonListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent event) {
System.out.println(textArea.getText().trim());
}
}
}
Why does my Inner JPanel not display my JLabel text?
I have an outer JPanel, and an inner JPanel to display some text with the right dimensions. However I don't understand why it won't show?
JPanel jp = new JPanel();
jp.setBackground(Color.decode("#ffffff"));
jp.setBounds(0, 35, 400, 315);
JPanel mostInner = new JPanel();
mostInner.setForeground(Color.black);
mostInner.setBounds(207, 5, 190, 240);
jp.add(mostInner);
JLabel jltxt = new JLabel();
jltxt.setText("Test");
mostInner.add(jltxt);
Again, much better to avoid using null layouts and setBounds(...). While null layouts and setBounds() might seem to Swing newbies like the easiest and best way to create complex GUI's, the more Swing GUI'S you create the more serious difficulties you will run into when using them. They won't re-size your components when the GUI resizes, they are a royal witch to enhance or maintain, they fail completely when placed in scrollpanes, they look gawd-awful when viewed on all platforms or screen resolutions that are different from the original one.
For example, the following code creates this GUI:
This uses a GridLayout to place a JPanel in the right hand side of another JPanel. If I wanted to add more components in different relative locations, it would be easy to do by simply nesting JPanels, each using its own layout.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.*;
public class WorkWithLayouts extends JPanel {
private static final long serialVersionUID = 1L;
private static final int PREF_W = 400;
private static final int PREF_H = 315;
private static final String BG = "#ffffff";
public WorkWithLayouts() {
JPanel mostInner = new JPanel();
mostInner.setForeground(Color.black);
mostInner.setOpaque(false); // if you want the backing jpanel's background to show through
// add title temporarily just to show where mostInner panel is
mostInner.setBorder(BorderFactory.createTitledBorder("most inner")); // TODO: delete this
JLabel jltxt = new JLabel();
jltxt.setText("Test");
mostInner.add(jltxt);
setBackground(Color.decode(BG));
setLayout(new GridLayout(1, 2));
add(new JLabel()); // empty label
add(mostInner);
}
#Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
} else {
return new Dimension(PREF_W, PREF_H);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
createAndShowGui();
});
}
private static void createAndShowGui() {
WorkWithLayouts mainPanel = new WorkWithLayouts();
JFrame frame = new JFrame("Work With Layouts");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
}
As already mentioned by Hovercraft Full Of Eels, you can help us to help you by adding a small program to your question that shows your problem (see https://stackoverflow.com/help/mcve for more information). Then people can try to reproduce and solve the issue.
When I created a small program myself, it seemed to work fine:
import java.awt.*;
import javax.swing.*;
public class LabelNotVisible {
public static void main(String[] arguments) {
SwingUtilities.invokeLater(() -> new LabelNotVisible().createAndShowGui());
}
private void createAndShowGui() {
JFrame frame = new JFrame("Stack Overflow");
frame.setBounds(100, 100, 800, 200);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JPanel jp = new JPanel();
jp.setBackground(Color.decode("#ffffff"));
//jp.setBounds(0, 35, 400, 315);
JPanel mostInner = new JPanel();
mostInner.setForeground(Color.black);
//mostInner.setBounds(207, 5, 190, 240);
jp.add(mostInner);
JLabel jltxt = new JLabel();
jltxt.setText("Test");
mostInner.add(jltxt);
frame.getContentPane().add(jp);
frame.setVisible(true);
}
}
I am working on a semester project that I have and I was wondering if it was possible to store 3-4 JPanels instead one single "main" JPanel. The reason for me asking this is because I a trying to make a GUI checkbook program and my checkbook has 7 buttons that should open a new window once I click on it. To switch between each window I'm going to have to use the CardLayout, but my understand of the CardLayout is that I can only assign one single JPanel to that card so I can't assign multiple JPanels to a single Card layout so when the user clicks on a different card 3-4 different JPanels appear.
The reason that I am asking this is because I asked for help earlier and received help for creating my first window in this project, it produces the output I want PERFECTLY, but uses more than 1 JPanel in doing so. Since this prevents me from continuing on to the other steps of my 7 GUI Windows, I am stuck.
Here is the code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class checkbook extends JPanel implements ActionListener {
private static final String title = "Use The Buttons Below To Manage Transactions";
private static final String[] bottomButtons = { "Create a New Account",
"Load a Trans from a File", "Add New Transactions",
"Search Transactions", "Sort Transactions",
"View/Delete Transactions", "Backup Transaction", "Exit" };
static JButton Button[] = new JButton[8];
static ActionListener AL = new checkbook();
public checkbook() {
JLabel titleLabel = new JLabel(title, SwingConstants.CENTER);
titleLabel.setFont(titleLabel.getFont().deriveFont(Font.BOLD, 18));
JPanel titlePanel = new JPanel();
titlePanel.add(titleLabel); // put it in a JPanel so it will expand to fill BoxLayout
JTextField textfield = new JTextField();
JPanel accountBalancePanel = new JPanel();
accountBalancePanel.add(new JLabel("Account Name:"));
accountBalancePanel.add(new JTextField(10));
accountBalancePanel.add(Box.createHorizontalStrut(4));
accountBalancePanel.add(new JLabel("Balance:"));
textfield = new JTextField("0.0", 10);
textfield.setHorizontalAlignment(JTextField.RIGHT);
accountBalancePanel.add(textfield);
JPanel northPanel = new JPanel();
northPanel.setLayout(new BoxLayout(northPanel, BoxLayout.PAGE_AXIS));
northPanel.add(titlePanel);
northPanel.add(accountBalancePanel);
JPanel southBtnPanel = new JPanel(new GridLayout(2, 4, 1, 1));
for(int i = 0; i < 8; i++){
Button[i] = new JButton(bottomButtons[i]);
southBtnPanel.add(Button[i]);
Button[i].addActionListener(AL);
}
setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
setLayout(new BorderLayout());
add(northPanel, BorderLayout.NORTH);
add(Box.createRigidArea(new Dimension(100, 100))); // just an empty placeholder
add(southBtnPanel, BorderLayout.SOUTH);
}
private static void createAndShowGui() {
checkbook mainPanel = new checkbook();
JFrame frame = new JFrame("Checkbook");
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();
}
});
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == Button[7]) {
System.exit(0);
}
}
}
Credit goes to Hovercraft Full Of Eels for showing me the above example
If there is anything that is unclear about my question, please ask and I will do the best I can to fix it.
Here is what the code produces:
http://i.stack.imgur.com/WY0c3.png
I am working on the editor. I am using Java swing for it. I have embedded a JTextArea with JScrollPane. i want to position the jtextarea of particular size at the middle of JScrollPane. To do this I used setLocation function. But this is not working?
public class ScrollPaneTest extends JFrame {
private Container myCP;
private JTextArea resultsTA;
private JScrollPane scrollPane;
private JPanel jpanel;
public ScrollPaneTest() {
resultsTA = new JTextArea(50,50);
resultsTA.setLocation(100,100);
jpanel=new JPanel(new BorderLayout());
jpanel.add(resultsTA,BorderLayout.CENTER);
scrollPane = new JScrollPane(jpanel,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setPreferredSize(new Dimension(800, 800));
scrollPane.setBounds(0, 0, 800, 800);
setSize(800, 800);
setLocation(0, 0);
myCP = this.getContentPane();
myCP.setLayout(new BorderLayout());
myCP.add(scrollPane);
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public static void main(String[] args) {
new ScrollPaneTest();
}
}
You simply have to add the JTextArea to the JScrollPane, and add it to the CENTER of the JPanel having BorderLayout.
Don't use AbsolutePositioning. Add a proper LayoutManager, and let LayoutManager do the rest for positioning and sizing your components on the screen.
In order to use the setBounds(...) method you have to use a null Layout for your component, which is not worth using, provided the perspective, as mentioned in the first paragraph of the AbsolutePositioning. Though in the code example provided by you, you are doing both the thingies together i.e. using Layout and using AbsolutePositioning, which is wrong in every way. My advice STOP DOING IT :-)
In the example provided the ROWS and COLUMNS provided by you are sufficient to size the JTextArea by the Layout concern.
Code Example :
import java.awt.*;
import javax.swing.*;
public class Example
{
private JTextArea tarea;
private void displayGUI()
{
JFrame frame = new JFrame("JScrollPane Example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel contentPane = new JPanel();
contentPane.setLayout(new BorderLayout(5, 5));
JScrollPane textScroller = new JScrollPane();
tarea = new JTextArea(30, 30);
textScroller.setViewportView(tarea);
contentPane.add(textScroller);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String... args)
{
EventQueue.invokeLater(new Runnable()
{
#Override
public void run()
{
new Example().displayGUI();
}
});
}
}
For some reason i am having problems centering my panel vertically that is located inside another panel. I do exactly as the examples i studied but still no luck.
Down there is my code. Despite using setAlignmentY(0.5f) on my container panel, it still wont center when i resize the window.
Also the components inside container panel wont center either, despite setAligenmentX(0.5f).
I wonder if there is a solution for this, I pretty much tried everything out there but couldnt find a solution.
JLabel idLabel;
JLabel passLabel;
JTextField id;
JTextField pass;
JButton enter;
JPanel container;
public JournalLogin()
{
//setLayout(new FlowLayout());
//setPreferredSize(new Dimension(500, 500));
//setBorder(BorderFactory.createEmptyBorder(100, 100, 100, 100));
container = new JPanel();
container.setLayout(new MigLayout());
container.setAlignmentX(0.5f);
container.setAlignmentY(0.5f);
container.setPreferredSize(new Dimension(300, 300));
container.setBorder(BorderFactory.createTitledBorder("Login"));
add(container);
idLabel = new JLabel("ID:");
idLabel.setAlignmentX(0.5f);
container.add(idLabel);
id = new JTextField();
id.setText("id");
id.setAlignmentX(0.5f);
id.setPreferredSize(new Dimension(80, 20));
container.add(id, "wrap");
setAlignmentX and Y are not the way to go about doing this. One way to center a component in a container is to have the container use GridBagLayout and to add the component without using any GridBagConstraints, a so-called default addition. There are other ways as well.
For example to alter Nick Rippe's example (1+ to him):
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import javax.swing.*;
public class UpdatePane2 extends JPanel {
private static final int PREF_W = 300;
private static final int PREF_H = 200;
public UpdatePane2() {
JPanel innerPanel = new JPanel();
innerPanel.setLayout(new BorderLayout());
innerPanel.add(new JLabel("Hi Mom", SwingConstants.CENTER),
BorderLayout.NORTH);
innerPanel.add(new JButton("Click Me"), BorderLayout.CENTER);
setLayout(new GridBagLayout());
add(innerPanel);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}
private static void createAndShowGui() {
JFrame frame = new JFrame("UpdatePane2");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new UpdatePane2());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
Alignments tend to be pretty picky in Swing - they do [usually] work... but if all you're looking for is a panel that's centered, I'd recommend using Boxes in the BoxLayout (My personal favorite LayoutManager). Here's an example to get you started:
import java.awt.Dimension;
import javax.swing.*;
public class UpdatePane extends JPanel{
public static void main(String... args) {
SwingUtilities.invokeLater(new Runnable(){
#Override
public void run() {
//Create Buffers
Box verticalBuffer = Box.createVerticalBox();
Box horizontalBuffer = Box.createHorizontalBox();
verticalBuffer.add(Box.createVerticalGlue()); //Top vertical buffer
verticalBuffer.add(horizontalBuffer);
horizontalBuffer.add(Box.createHorizontalGlue()); //Left horizontal buffer
//Add all your content here
Box mainContent = Box.createVerticalBox();
mainContent.add(new JLabel("Hi Mom!"));
mainContent.add(new JButton("Click me"));
horizontalBuffer.add(mainContent);
horizontalBuffer.add(Box.createHorizontalGlue()); //Right horizontal buffer
verticalBuffer.add(Box.createVerticalGlue()); //Bottom vertical buffer
// Other stuff for making the GUI
verticalBuffer.setPreferredSize(new Dimension(300,200));
JFrame frame = new JFrame();
frame.add(verticalBuffer);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
});
}
}
You will need to get the LayoutManager to center the layout for you. Currently it looks like the implementation of "MigLayout" does not honor the Alignment. Try changing it or creating a subclass.