How to add ScrollBar on Textarea or JFrame in Java Swing? - java

Hi everyone ,i`m beginner in programmation and i wanted to make a text editor interface by using java Eclipse and i get problem each time i add scrollbar , the text area disappear ... How can i fix my code to continue on it ?... i saw many tutorials but nothing work>
import java.awt.*;
import java.awt.color.*;
import javax.swing.*;
public class Fenetre extends JFrame {
JMenuBar Menubar = new JMenuBar();
JMenu MenuFichier = new JMenu("Fichier");
JMenuItem MenuFichier1 = new JMenuItem("Nouveau");
JMenuItem MenuFichier2 = new JMenuItem("Ouvrir");
JMenuItem MenuFichier3 = new JMenuItem("Enregister");
JMenuItem MenuFichier4 = new JMenuItem("Enregister sous");
JMenuItem MenuFichier5 = new JMenuItem("Quitter");
JMenu MenuEdition = new JMenu("Edition");
JMenuItem MenuEdition1 = new JMenuItem("Couper");
JMenuItem MenuEdition2 = new JMenuItem("Copier");
JMenuItem MenuEdition3 = new JMenuItem("Coller");
JMenu MenuAide = new JMenu("Aide");
JMenuItem MenuAide1 = new JMenuItem("A propos");
JPanel p=new JPanel();
JScrollPane sp = new JScrollPane ();
JTextArea jt = new JTextArea (90,90);
public Fenetre() {
add(p);
setTitle("Text Editor");
setBounds(100,100, 500, 500);
//setExtendedState(JFrame.MAXIMIZED_BOTH);
sp.add(jt);
jt.setText(null);
jt.setLineWrap(true);
jt.setWrapStyleWord(true);
jt.setVisible(true);
sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
sp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
p.add(sp);
sp.setVisible(true);
Menubar.add(MenuFichier);
Menubar.add(MenuEdition);
Menubar.add(MenuAide);
MenuFichier.add(MenuFichier1);
MenuFichier.add(MenuFichier2);
MenuFichier.addSeparator();
MenuFichier.add(MenuFichier3);
MenuFichier.add(MenuFichier4);
MenuFichier.addSeparator();
MenuFichier.add(MenuFichier5);
MenuEdition.add(MenuEdition1);
MenuEdition.add(MenuEdition2);
MenuAide.add(MenuAide1);
Menubar.setBackground(Color.PINK);
getContentPane().add(sp);
setJMenuBar(Menubar);
setVisible(true);
setAlwaysOnTop(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}}
and this is the main of the code
import java.awt.*;
import javax.swing.*;
import java.awt.Color;
public class CreationEditor {
public static void main(String[] args) {
JFrame f = new Fenetre();
}
}

Related

How do I add my menubar class to my JFrame which is in my Main Class

I am unsure on the code to connect my MenuDriver class to my JFrame which is in Main class. I understand that this could all be done in the main class, but I am being told to have a separate class for the menu.
Main-
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.MenuBar;
import javax.swing.JFrame;
public class GraphicMain {
public static void main(String[] args) {
JFrame frame = new JFrame("Graphics Program");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 600);
MenuDriver menu = new MenuDriver();
frame.pack();
frame.setVisible(true);
}
}
Menu Class-
import java.awt.*;
import javax.swing.*;
public class MenuDriver {
public MenuDriver(){
JMenuBar menubar = new JMenuBar();
JMenu file = new JMenu("File");
menubar.add(file);
JMenuItem load = new JMenuItem("Load");
file.add(load);
JMenuItem save = new JMenuItem("Save");
file.add(save);
JMenuItem exit = new JMenuItem("Exit");
file.add(exit);
JMenu help = new JMenu("Help");
menubar.add(help);
JMenuItem info = new JMenuItem("Info");
file.add(info);
}
}
In your MenuDriver class you need to create a method getMenuBar() which will return the JMenuBar that you created in the constructor of your class.
Then you need to make the JMenuBar an instance variable of the class:
public class MenuDriver
{
private JMenubar menuBar; // added
public MenuDriver()
{
//JMenuBar menubar = new JMenuBar();
menubar = new JMenuBar();
...
I'll let you write the getMenuBar() method.
Now in you main class you can reference the menu bar by using:
MenuDriver menu = new MenuDriver();
frame.setJMenuBar( menu.getMenuBar() );

Why my Jtextfield and jmenubar doesnt work

Q1: MY JTextFeild and JMenuBar does not show up, and I don't know why
there is an picture of the program.
Q2: I have seen a lot of ways to write a program of interface, I don't know which way is better. Is this way or this way: https://www.youtube.com/watch?v=706Ye4ubtEY
import javax.swing.*;
import java.awt.*;
import javax.swing.JTextField;
import javax.swing.JMenuBar;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Real_Interface extends JFrame implements ActionListener {
public Real_Interface(){
JFrame window = new JFrame();
window.add(new JLabel("Rocket Data Visualization Tool"));
window.setTitle("Rocket Data Visualization Tool");
window.setSize(640, 480);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLocationRelativeTo(null);
window.setVisible(true);
window.setResizable(false);
JTextField tf = new JTextField();
tf.setText("New Text");
String str = tf.getText();
tf.setVisible(true);
JMenuBar bar = new JMenuBar();
JMenu file = new JMenu("File");
JMenuItem menuItem = new JMenuItem("haha");
JMenuItem menuItem2 = new JMenuItem("haha2");
file.add(menuItem);
file.addSeparator();
file.add(menuItem2);
bar.add(file);
JButton button = new JButton("OK");
window.add(button);
button.setBounds(250, 400, 150, 40);
button.addActionListener(this);
}
public static void main(String[] args){
new Real_Interface();
}
public void actionPerformed(ActionEvent e) {
System.out.println("The Button Works!");
Plot.main(null);
}
}
A few reasons
The menubar has not been assigned to the frame
window.setJMenuBar(bar);
The textfield has not been added
window.add(textfield, BorderLayout.PAGE_START);
This component is visible by default so calling setVisible is unnecessary.
The frame needs to be made visible after all components have been added to the frame rather than beforehand
window.setVisible(true);
Your JTextField tf and JMenuBar bar aren't added to their parent (the window ?).

Swing hangs when being resized on a second display

Main thread calls GUI() function that adds frame, menus, menuitems, and a scrollpane with jtable (DefaultTableModel).
If i place the running application to a second display/screen it hangs as soon i switch to a different application or when i try to resize it.
public static void main(String[] args) {
GUI();
}
public static void GUI(){
JButton button;
JMenuBar menuBar;
JMenu menu, submenu;
JMenuItem menuItem;
window = new JFrame();
window.setResizable(false);
//menu
menuBar = new JMenuBar();
menu = new JMenu("Start");
submenu = new JMenu("New");
menu.add(submenu);
menuItem = new JMenuItem("menu1");
menuItem.addActionListener(new buttonActionListener("m1"));
submenu.add(menuItem);
menuItem = new JMenuItem("menu2");
menuItem.addActionListener(new buttonActionListener("m2"));
submenu.add(menuItem);
menuItem = new JMenuItem("menu3");
menuItem.addActionListener(new buttonActionListener("m3"));
submenu.add(menuItem);
menuItem = new JMenuItem("menu4");
menuItem.addActionListener(new buttonActionListener("m4"));
submenu.add(menuItem);
menuBar.add(menu); //start
menu = new JMenu("Options");
menuBar.add(menu); //options
//top panel
JPanel top = new JPanel();
button = new JButton("Paste");
button.addActionListener(new buttonActionListener("paste"));
top.add(button);
button = new JButton("Copy");
button.addActionListener(new buttonActionListener("copy"));
top.add(button);
JButton start = new JButton("Start");
start.addActionListener(new buttonActionListener("start"));
top.add(start);
//main panel with table
main = new JScrollPane(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
DefaultTableModel model = new DefaultTableModel();
table = new JTable(model);
//bottom panel
JPanel bottom = new JPanel();
Creq = new JCheckBox();
Creq.setSelected(true);
item= new JTextField("item");
comment = new JTextField("comment");
target= new JTextField("target");
req = new JTextField("requirement");
bottom.add(Creq);
bottom.add(req);
bottom.add(item);
depToUpdate.setVisible(true);
bottom.add(target);
targetDep.setVisible(true);
bottom.add(comment);
comment.setVisible(true);
window.setJMenuBar(menuBar);
window.add(top, BorderLayout.NORTH);
window.add(main, BorderLayout.CENTER);
window.add(bottom, BorderLayout.SOUTH);
window.pack();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLocationRelativeTo(null);
window.setVisible(true);
}
After spending way too much time fixing the code so that it would actually run in Java, I tested the GUI on my second monitor in Windows 8. The GUI didn't do anything, but it didn't freeze either.
Here are the problems I fixed:
I moved the Swing GUI code into a regular class method (not static).
I put the Swing components on the Event Dispatch thread. You must always create and execute your Swing components on the Event Dispatch thread.
Here's the version of the code I ran.
package snippet;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
public class Snippet implements Runnable{
private JCheckBox creq;
private JFrame window;
private JScrollPane main;
private JTable table;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Snippet());
}
#Override
public void run() {
JButton button;
JMenuBar menuBar;
JMenu menu, submenu;
JMenuItem menuItem;
window = new JFrame();
window.setResizable(false);
// menu
menuBar = new JMenuBar();
menu = new JMenu("Start");
submenu = new JMenu("New");
menu.add(submenu);
menuItem = new JMenuItem("menu1");
// menuItem.addActionListener(new buttonActionListener("m1"));
submenu.add(menuItem);
menuItem = new JMenuItem("menu2");
// menuItem.addActionListener(new buttonActionListener("m2"));
submenu.add(menuItem);
menuItem = new JMenuItem("menu3");
// menuItem.addActionListener(new buttonActionListener("m3"));
submenu.add(menuItem);
menuItem = new JMenuItem("menu4");
// menuItem.addActionListener(new buttonActionListener("m4"));
submenu.add(menuItem);
menuBar.add(menu); // start
menu = new JMenu("Options");
menuBar.add(menu); // options
// top panel
JPanel top = new JPanel();
button = new JButton("Paste");
// button.addActionListener(new buttonActionListener("paste"));
top.add(button);
button = new JButton("Copy");
// button.addActionListener(new buttonActionListener("copy"));
top.add(button);
JButton start = new JButton("Start");
// start.addActionListener(new buttonActionListener("start"));
top.add(start);
// main panel with table
// main = new JScrollPane(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
// ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
DefaultTableModel model = new DefaultTableModel();
table = new JTable(model);
main = new JScrollPane(table);
// bottom panel
JPanel bottom = new JPanel();
creq = new JCheckBox();
creq.setSelected(true);
JTextField item = new JTextField("item");
JTextField comment = new JTextField("comment");
JTextField target = new JTextField("target");
JTextField req = new JTextField("requirement");
bottom.add(creq);
bottom.add(req);
bottom.add(item);
// depToUpdate.setVisible(true);
bottom.add(target);
// targetDep.setVisible(true);
bottom.add(comment);
comment.setVisible(true);
window.setJMenuBar(menuBar);
window.add(top, BorderLayout.NORTH);
window.add(main, BorderLayout.CENTER);
window.add(bottom, BorderLayout.SOUTH);
window.pack();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLocationRelativeTo(null);
window.setVisible(true);
}
}

Swing; How would I position my components in this format?

So, I would like my items to be positioned in the format below; I'm really not confident with positioning but would like to learn it a bit more. Here is the code as of yet:
package com.bleh.harry;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Main
{
private JMenuBar menuBar;
private JMenu fileMenu, windowMenu, helpMenu;
private JMenuItem fileNew, fileOpen, fileSave, windowTheme, windowLayout, windowProperties, helpWelcome, helpHelp, helpAbout;
private JTextArea mainTextArea;
public
Main()
{
JPanel mainCard = new JPanel(new BorderLayout(8,8));
JPanel mainTop = new JPanel(new FlowLayout(FlowLayout.CENTER));
menuBar = new JMenuBar();
fileMenu = new JMenu("File");
windowMenu = new JMenu("Window");
helpMenu = new JMenu("Help");
menuBar.add(fileMenu);
menuBar.add(windowMenu);
menuBar.add(helpMenu);
final CardLayout layout = new CardLayout(); //ADDS CARDS TO CONTAINER
final JPanel cards = new JPanel(layout);
cards.add(mainCard, "2");
mainCard.add(mainTop, BorderLayout.NORTH);
JFrame window = new JFrame("Pseudo code text editor");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().add(cards);
window.setSize(1280, 720);
window.setLocationRelativeTo(null);
window.setVisible(true);
}
public static void main(String args[])
{
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Main();
}
});
}
}
You could use a BorderLayout...
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class LayoutTest {
private JMenuBar menuBar;
private JMenu fileMenu, windowMenu, helpMenu;
private JMenuItem fileNew, fileOpen, fileSave, windowTheme, windowLayout, windowProperties, helpWelcome, helpHelp, helpAbout;
private JTextArea mainTextArea;
public LayoutTest() {
JPanel mainCard = new JPanel(new BorderLayout(8, 8));
menuBar = new JMenuBar();
fileMenu = new JMenu("File");
windowMenu = new JMenu("Window");
helpMenu = new JMenu("Help");
menuBar.add(fileMenu);
menuBar.add(windowMenu);
menuBar.add(helpMenu);
final CardLayout layout = new CardLayout();
final JPanel cards = new JPanel(layout);
cards.add(mainCard, "2");
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.add("One", createPane());
tabbedPane.add("Two", createPane());
tabbedPane.add("Three", createPane());
tabbedPane.add("Four", createPane());
mainTextArea = new JTextArea(20, 40);
mainCard.add(tabbedPane, BorderLayout.WEST);
mainCard.add(new JScrollPane(mainTextArea));
JFrame window = new JFrame("Pseudo code text editor");
window.setJMenuBar(menuBar);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().add(cards);
window.pack();
window.setLocationRelativeTo(null);
window.setVisible(true);
}
protected JPanel createPane() {
return new JPanel() {
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
};
}
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new LayoutTest();
}
});
}
}
The problem here, is the amount of space that the JTabbedPane wants is depended on it's content...
You could even try using a GridBagLayout which might give you a little more control...
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class LayoutTest {
private JMenuBar menuBar;
private JMenu fileMenu, windowMenu, helpMenu;
private JMenuItem fileNew, fileOpen, fileSave, windowTheme, windowLayout, windowProperties, helpWelcome, helpHelp, helpAbout;
private JTextArea mainTextArea;
public LayoutTest() {
JPanel mainCard = new JPanel(new GridBagLayout());
menuBar = new JMenuBar();
fileMenu = new JMenu("File");
windowMenu = new JMenu("Window");
helpMenu = new JMenu("Help");
menuBar.add(fileMenu);
menuBar.add(windowMenu);
menuBar.add(helpMenu);
final CardLayout layout = new CardLayout(); //ADDS CARDS TO CONTAINER
final JPanel cards = new JPanel(layout);
cards.add(mainCard, "2");
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.add("One", createPane());
tabbedPane.add("Two", createPane());
tabbedPane.add("Three", createPane());
tabbedPane.add("Four", createPane());
mainTextArea = new JTextArea(20, 40);
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 0.25;
gbc.weighty = 1;
mainCard.add(tabbedPane, gbc);
gbc.weightx = 0.75;
mainCard.add(new JScrollPane(mainTextArea), gbc);
JFrame window = new JFrame("Pseudo code text editor");
window.setJMenuBar(menuBar);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().add(cards);
window.pack();
window.setLocationRelativeTo(null);
window.setVisible(true);
}
protected JPanel createPane() {
return new JPanel() {
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
};
}
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new LayoutTest();
}
});
}
}
Just remember, the JMenuBar belongs to the window ;)
To lay components out next to each other, I'd recommend using a BoxLayout. A BoxLayout takes an orientation parameter and lays components out accordingly. The two most used options are X_AXIS and Y_AXIS. X_AXIS lays things out left to right while Y_AXIS lays things out top to bottom. You want X_AXIS.
To set the layout with a BoxLayout using an instance of JFrame named window, do:
window.setLayout(new BoxLayout(window, BoxLayout.X_AXIS));

hidden labels/panels in java

Basically, i am trying to make it so there are 3 buttons on the bottom of the screen and then have a label which has words in it in the middle of the screen. However, i cant seem to have both buttons and the label in the GUI at the same time. I am a beginner and dont know much about layouts (even though i have read into them) so any help/guidance would be helpful on why i cannot see both the label and the buttons.enter code here
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
public class AWorldPanel implements ActionListener {
/** Declaring all the menu items within the GUI **/
private JMenuItem Fileitem1 = new JMenuItem("New configuration");
private JMenuItem Fileitem2 = new JMenuItem("Open configuration file ");
private JMenuItem Fileitem3 = new JMenuItem("Save");
private JMenuItem Fileitem4 = new JMenuItem("Save As");
private JMenuItem Fileitem5 = new JMenuItem("Exit");
private JMenuItem Viewitem1 = new JMenuItem("Display configuration");
private JMenuItem Viewitem2 = new JMenuItem("Edit configuration");
private JMenuItem Viewitem3 = new JMenuItem("Info about Bugs");
private JMenuItem Viewitem4 = new JMenuItem("Info about Map");
private JMenuItem Edititem1 = new JMenuItem("Remove");
private JMenuItem Edititem2 = new JMenuItem("Add");
private JMenuItem Simulationitem1 = new JMenuItem("Simulation");
private JMenuItem Helpitem1 = new JMenuItem("Info about application");
private JMenuItem Helpitem2 = new JMenuItem("Info about author");
private JLabel theLabel;
private JPanel thePanel;
JButton Run, Pause, Reset;
JFrame GUI = new JFrame("Graphical User Interface");
private static AWorld guiworld;
public AWorldPanel() {
/** Creating the menu **/
JMenuBar menubar = new JMenuBar();
JMenu File = new JMenu("File");
JMenu View = new JMenu("View");
JMenu Edit = new JMenu("Edit");
JMenu Help = new JMenu("Help");
/** welcome label **/
theLabel = new JLabel("Hello ", JLabel.CENTER);
theLabel.setVisible(true);
theLabel.setVerticalTextPosition(JLabel.TOP);
theLabel.setHorizontalTextPosition(JLabel.CENTER);
/** file sub menus **/
menubar.add(File);
File.add(Fileitem1);
File.add(Fileitem2);
File.add(Fileitem3);
File.add(Fileitem4);
File.add(Fileitem5);
menubar.add(View);
View.add(Viewitem1);
View.add(Viewitem2);
View.add(Viewitem3);
View.add(Viewitem4);
menubar.add(Edit);
Edit.add(Edititem1);
Edit.add(Edititem2);
menubar.add(Help);
Help.add(Helpitem1);
Help.add(Helpitem2);
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
panel.setBorder(new EmptyBorder(new Insets(300, 125, 100, 100)));
Run = new JButton("Run");
Pause = new JButton("Pause");
Reset = new JButton("Reset");
panel.add(Run);
panel.add(Box.createRigidArea(new Dimension(0, 5)));
panel.add(Pause);
panel.add(Box.createRigidArea(new Dimension(0, 5)));
panel.add(Reset);
GUI.add(panel);
GUI.add(theLabel);
GUI.setJMenuBar(menubar);
}
private static void createAndShowGUI() {
AWorldPanel newworld = new AWorldPanel();
// Create the container
JFrame frame = new JFrame("Graphical User Interface");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// changing the menu settings
newworld.GUI.setLocation(300, 100);
newworld.GUI.setSize(500, 500);
newworld.GUI.setVisible(true);// Now the frame will appear on screen
}
public static void main(String args[]) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
The default layout manager for a JFrame is a BorderLayout. If you don't specify a constraint then the component will be added to the BorderLayout.CENTER. You can't add multiple components to the same location in the layout. Try something like:
GUI.add(panel, BorderLayout.SOUTH);
GUI.add(theLabel, BorderLayout.CENTER);
Also, learn standard Java naming conventions. Every book, tutorial or example you will read uses these standards so don't make up your own conventions. Variable names do not start with an upper case character.

Categories

Resources