When I comment out frame.add(hidden) it only shows the text area. When I don't comment it out, it only shows a large gray box with a grayed out Scrollbar.
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Panlindrome{
public Panlindrome(){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Panlindrome?");
frame.setSize(240,320);
//frame.setLayout(new GridLayout(3,1));
JTextArea inputText = new JTextArea(30,1);
inputText.setLineWrap(true);
JScrollPane hidden = new JScrollPane(inputText);
hidden.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
frame.add(inputText);
//frame.add(hidden);
frame.setVisible(true);
}
public static void main(String[] args){
Panlindrome check = new Panlindrome();
}
}
Don't add inputText to the frame; only add hidden.
The content of the scroll pane is already a child of the scroll pane. If you also try to add it to the frame (actually the frame's content pane, but whatever) as well, it will be in two places at once, which doesn't work.
Related
I created a graphing utility using canvas. Currently, information is inputted through the console. However, I want to make the program more presentable by adding inputs via JTextField and JButtons. Is there any way I can do that?
something like this:
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class JFrameWithCanvas extends JFrame {
private Canvas canvas = new Canvas();
public JFrameWithCanvas() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel pnlToolbar = new JPanel();
pnlToolbar.add(new JTextField(10));
pnlToolbar.add(new JButton("foo"));
getContentPane().add(pnlToolbar, BorderLayout.PAGE_START);
canvas.setBackground(Color.MAGENTA);
getContentPane().add(canvas, BorderLayout.CENTER);
canvas.setPreferredSize(new Dimension(300, 300));
pack();
setLocationRelativeTo(null); // center it on the screen
}
public static void main(String[] args) {
new JFrameWithCanvas().setVisible(true);
}
}
Below is a Java program which does not work properly. I wanted the JTextfield to go to the bottom, and be 50x50, but it is not working. I was wondering if I could get some help.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class Main {
public static JTextField intext = new JTextField();
public static JPanel panel = new JPanel();
public static boolean running = true;
public static String runtext;
public static String word = "HELLO";
public static String guesses = "";
public static void main(String[] args) {
intext.setBounds(5,667,50,50);
panel.add(intext);
JFrame frame = new JFrame("Hangman");
frame.setBounds(10,10,600,750);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.setResizable(false);
frame.setVisible(true);
I inferred that when using the .setBounds() method the JTextfield would go where it was needed, though it remains at the top, and extremely thin.
Try editing your code this way:-
panel.setLayout(null); // setting layout for panel
panel.setBounds(0,0,600,650); // setting bounds for panel with respect to frame
intext.setBounds(5,557,50,50); //setting bounds for textfield with respect to panel
panel.add(intext);
JFrame frame= new JFrame("Hangman");
frame.setBounds(10,10,600,750);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.setResizable(false);
frame.setLayout(null); // setting layout for frame
frame.setVisible(true);
Make sure to include layouts for your components and set the bounds for your JPanel's and JTextField's properly.
IN MY PROGRAM,
I have a button when pressed, adds a new JPanel to a JPanel with BOXLAYOUT. Since when I add the JPanel it adds it to the bottom of the previous one. BUT THERE IS NO SCROLL BAR.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
public class tes{
public static void main(String[] args) {
JFrame newL = new JFrame();
newL.setTitle("New Level Files");
//newL.setLayout(new BoxLayout());
//t.setSize(500,600);
//newL.pack();
newL.setVisible(true);
newL.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel listPane = new JPanel();
listPane.setLayout(new BoxLayout(listPane, BoxLayout.PAGE_AXIS));
JScrollPane scrollPane = new JScrollPane(listPane, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setPreferredSize(new Dimension(600, 100));
newL.add(scrollPane, BorderLayout.CENTER);
JPanel levelP = new JPanel();
levelP.setBorder(BorderFactory.createLineBorder(Color.black));
levelP.setLayout(new GridBagLayout());
GridBagConstraints l = new GridBagConstraints();
l.insets = new Insets(10,10,10,5);
JButton okForFileEdit = new JButton("Edit this File");
l.gridx = 1;
l.gridy = 6;
levelP.add(okForFileEdit, l);
okForFileEdit.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent n){
JPanel createInPanel = new JPanel();
createInPanel.setSize(200,200);
createInPanel.setBorder(BorderFactory.createLineBorder(Color.black));
createInPanel.setLayout(new GridBagLayout());
listPane.add(createInPanel);
JLabel yout = new JLabel("YEah this is a long sentence to see the placement");
createInPanel.add(yout);
listPane.revalidate();
listPane.repaint();
newL.revalidate();
}});
listPane.add(levelP);
listPane.revalidate();
listPane.repaint();
newL.add(listPane);
newL.pack();
}
** I added a SIMPLIFIED VERSION OF THE PROGRAM THAT DOESN'T MAKE THE SCROLLBAR
Hope this makes more sense. Thanks for the help in advance :);
I have a button when pressed, adds a new JPanel to a JPanel with BOXLAYOUT.
When you dynamically add components to a panel the basic logic need in your ActionListener is:
panel.add(...);
panel.revalidate();
panel.repaint();
You need to invoke the revalidate method to invoke the layout manager of the panel so the preferred size can be recalculated.
Edit:
newL.add(listPane);
You don't need the above statement. A Swing component can only have a single parent. That statement removed the panel from the scroll pane so you don't see the scroll bars.
newL.revalidate();
You don't need that statement. As I said in my answer you only need to revalidate() the panel that you changed.
Also you don't need all the statements below:
listPane.add(levelP);
listPane.revalidate();
listPane.repaint();
newL.add(listPane);
That is the layout manager is invoked when you pack the frame of make the frame visible. Your code should be:
newl.pack();
newl.setVisible(true);
That is you should only make the frame visible AFTER adding all the components to the frame.
I have a jsplitPane and 2 component. I want to not let the user click the divider to maximize the top component.
Here is my code.
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JSplitPane;
public class MovingJSplitPaneDivider {
public static void main(String[] a) {
JFrame horizontalFrame = new JFrame();
horizontalFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComponent topButton = new JButton("Left");
JComponent bottomButton = new JButton("Right");
final JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
splitPane.setTopComponent(topButton);
splitPane.setBottomComponent(bottomButton);
horizontalFrame.add(splitPane, BorderLayout.CENTER);
horizontalFrame.setSize(150, 150);
horizontalFrame.setVisible(true);
splitPane.setDividerLocation(0.5);
}
}
What I want exactly is that to block the user when he wants to move down the divider.
I have a JMenu and when a person clicks on a JMenuItem I want the middle panel to refresh and display some new stuff. So I tried the .removeAll which works fine but when I try to add something it wont show.
Note: I'm using the WindowBuilder PRO, so I still trying to get use to it and whatnot
Here is my code:
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenu;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
import java.awt.List;
import javax.swing.JLabel;
import javax.swing.JTextPane;
import javax.swing.JSeparator;
import org.eclipse.wb.swing.FocusTraversalOnArray;
import java.awt.Component;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Panel;
import java.awt.GridBagConstraints;
public class HomeScreen {
JFrame frame;
private final Panel panel = new Panel();
public HomeScreen(String name) {
initialize(name);
}
/**
* Initialize the contents of the frame.
*/
private void initialize(String name) {
frame = new JFrame("Timzys CMS / Monitor / Account( " + name + " )");
frame.setResizable(false);
frame.setSize(694,525);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);
JMenu mnMonitor = new JMenu("Monitor");
menuBar.add(mnMonitor);
JMenuItem mntmUsers = new JMenuItem("Users");
mnMonitor.add(mntmUsers);
JMenuItem mntmContentPosts = new JMenuItem("Content Posts");
mnMonitor.add(mntmContentPosts);
JMenuItem mntmLogs = new JMenuItem("Logs");
mnMonitor.add(mntmLogs);
JMenu mnExtras = new JMenu("Extras");
menuBar.add(mnExtras);
mntmUsers.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
frame.getContentPane().removeAll();
frame.getContentPane().add(new JLabel("Test"));
frame.getContentPane().revalidate();
}
});
JMenuItem mntmFeedbacksuggestions = new JMenuItem("Feedback/Suggestions");
mnExtras.add(mntmFeedbacksuggestions);
frame.getContentPane().setLayout(null);
panel.setBounds(0, 0, 688, 476);
frame.getContentPane().add(panel);
panel.setLayout(null);
JTextPane txtpnWelcomeTimzysCms = new JTextPane();
txtpnWelcomeTimzysCms.setFont(new Font("Arial", Font.PLAIN, 18));
txtpnWelcomeTimzysCms.setEditable(false);
txtpnWelcomeTimzysCms.setText("Welcome Timzys CMS Monitor. If you are here then you are a admin! So please do not tamper with any important things. If you have questions or suggestions goto the extras tab and submit a feedback idea. Enjoy!");
txtpnWelcomeTimzysCms.setBounds(10, 11, 668, 72);
panel.add(txtpnWelcomeTimzysCms);
}
}
Probably you're facing similar problem as someone else in the question:
Java Swing revalidate() vs repaint()
As suggested: try to call repaint instead of revalidate (the one you're calling right now in your implementation of method actionPerformed)
The problem is that because you have set the frame's layout to null, the new JLabel that you add will need to have its size set. e.g.:
JLabel label = new JLabel("Test");
label.setSize(100, 100);
frame.getContentPane().add(label);
As an aside:
Actually I wonder why you are mixing AWT & Swing components here. You have a heavyweight AWT panel added to the frame which blocks out the menus. Switching to JPanel would fix this.