I'm a java newbie and I'm currently working on a simple application with a menu, scrollpane and textarea.
So far I've gotten everything I wanted on the form but when I fire up my application the scrollpane/textarea won't show up until I rezise the window.
I've tried using the repaint method as suggested on other forums for similar problems but it didn't work, perhaps I'm not using it correctly :S
Here's my class:
public class FenetreEditeur {
public static void main(String[] args){
FenetreEditeur f = new FenetreEditeur();
}
public FenetreEditeur(){
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setSize(400,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
initMenuBar(frame);
JTextArea areaMain = new JTextArea();
JScrollPane scrollPane = new JScrollPane(areaMain);
frame.add(scrollPane);
}
private void initMenuBar(JFrame frame){
JMenuBar menu = new JMenuBar();
JMenu revision = new JMenu("Revision");
JMenuItem statistiques = new JMenu("Statistiques");
JMenuItem grammaire = new JMenu("Grammaire et orthographe");
JMenuItem analyse = new JMenu("Analyse Automatique");
menu.add(revision);
revision.add(statistiques);
revision.add(grammaire);
revision.add(analyse);
frame.setJMenuBar(menu);
}}
Any help/tip would be greatly appreciated.
Thanks!
Call scrollPanel.revalidate() after adding it, or better, move frame.setVisible(true) to the end:
public FenetreEditeur(){
JFrame frame = new JFrame();
frame.setSize(400,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
initMenuBar(frame);
JTextArea areaMain = new JTextArea();
JScrollPane scrollPane = new JScrollPane(areaMain);
frame.add(scrollPane);
frame.setVisible(true);
}
Related
I am trying to write a Title for the main menu of my program, by using a JLabel, but it doesn't seem to appear on my screen.
import javax.swing.*;
public class GUI {
public GUI() {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createEmptyBorder(30,30,10,30));
panel.setLayout(new GridLayout());
frame.add(panel, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Title");
frame.pack();
frame.setSize(854,560);
frame.setVisible(true);
JLabel title = new JLabel();
title.setText("Title");
//title.setSize();
title.setVisible(true);
}
public static void main(String[] args) {
new GUI();
}
}
What am I doing wrong and how could I change the position of the Text if I manage to make it visible?
And I also want to add a button to go to the next page so if you could tell me how to do that too that would be great.
I would quickly and untested say that you are adding the label after you set the frame visible.
Do it before. Else you would have to revalidate and repaint the frame
As I can see in your code you are not adding title in panel. As a quick solution put panel.add(title); after title.setVisible(true); line in your code, it will display the label.
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createEmptyBorder(30,30,10,30));
panel.setLayout(new GridLayout());
frame.add(panel, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Title");
frame.pack();
frame.setSize(854,560);
frame.setVisible(true);
JLabel title = new JLabel();
title.setText("Title");
//title.setSize();
title.setVisible(true);
panel.add(title); //<---- this one line will diaplay label in GUI
I'm using inteliJ to write a file transfer program and I'm working on the GUI right now using Swing. I've made plenty of programs in swing, but for some reason I can't figure out why my GUI isn't showing up when I run the program. Everything compiles just fine.
public class stage {
JPanel mainContainer = new JPanel();
JPanel window = new JPanel();
JButton loadButton = new JButton();
JButton saveButton = new JButton();
JTextPane cmdOut = new JTextPane();
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu();
JMenuItem exitButton = new JMenuItem("exit");
public void display(){
mainContainer.setVisible(true);
mainContainer.add(window);
mainContainer.add(menuBar);
menuBar.add(menu);
menu.add(exitButton);
window.setLayout(new GridBagLayout());
window.add(loadButton);
window.add(saveButton);
window.add(cmdOut);
cmdOut.setText("TEST");
window.setVisible(true);
}
}
Here is my main method in another class.
public static void main(String[] args) throws IOException {
stage stage = new stage();
stage.display();
}
why don't you put your main panel into a JFrame, so that your JFrame will contain your main panel, and your main panel hold everything else?
I am trying to add a JList to my JPanel as it is stated by the following source code but it's not working :
public class Game {
public static void main(String[] args) throws Exception {
UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );
DefaultListModel<String> defListPlayers = new DefaultListModel<String>();
defListPlayers.addElement("Player 1");
defListPlayers.addElement("Player 2");
JList<String> listPlayers = new JList<String>(defListPlayers);
listPlayers.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
listPlayers.setLayoutOrientation(JList.VERTICAL);
listPlayers.setVisibleRowCount(10);
JScrollPane scroller = new JScrollPane(listPlayers);
scroller.setViewportBorder(new LineBorder(Color.RED));
scroller.revalidate();
JPanel panel = new JPanel();
panel.add(scroller);
JFrame frame = new JFrame;
frame.setContentPane(panel);
frame.pack();
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setVisible(true);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().repaint();
}
}
Any idea please why and what to do?
For some reason I can't get my scrollpane to be displayed within an applet.
public void init() {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JScrollPane scrPane = new JScrollPane(panel);
scrPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrPane.setLayout(new ScrollPaneLayout());
frame.getContentPane().add(scrPane);
this.setVisible(true);
}
You never display the JFrame that you create!
This:
frame.getContentPane().add(scrPane):
this.setVisible(true); // this != frame
is not working because you create a JFrame and then ignore it.
You shouldn't have an applet display a JFrame anyway. If you need to show a separate window, consider showing a JDialog. Better still, why not simply put the JScrollPane in the applet itself?
e.g.,
public void init() {
//JFrame frame = new JFrame();
JPanel panel = new JPanel();
JScrollPane scrPane = new JScrollPane(panel);
scrPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
// scrPane.setLayout(new ScrollPaneLayout());
// frame.getContentPane().add(scrPane);
getContentPane().add(scrPane);
// this.setVisible(true);
}
Im not sure how to reference to JPanel when it was declared like this.
This is the coding of the entire program: Everything works but the layout is not how I want it. adding BorderLayouts to it doesnt seem to work.
class FrameDemo
{
public static void main(String[] args)
{
final JFrame frame = new JFrame("CIT Test Program");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(350, 250));
frame.add(new JPanel()
{{
String[] tests = {"A+ Certification", "Network+ Certification", "Security+ Certification", "CIT Full Test Package"};
JComboBox comboBox = new JComboBox(tests);
TextArea text = new TextArea(5,10);
add(new JLabel("Welcome to the CIT Test Program "));
add(new JLabel("Please select which Test Package from the list below."));
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenu editMenu = new JMenu("Edit");
JMenu helpMenu = new JMenu("Help");
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(helpMenu);
JMenuItem newMenu = new JMenuItem("New (Ctrl+N)");
JMenuItem openMenu = new JMenuItem("Open (Ctrl+O)");
JMenuItem saveMenu = new JMenuItem("Save (Ctrl+S)");
JMenuItem exitMenu = new JMenuItem("Exit (Ctrl+W)");
JMenuItem cutMenu = new JMenuItem("Cut (Ctrl+X)");
JMenuItem copyMenu = new JMenuItem("Copy (Ctrl+C)");
JMenuItem pasteMenu = new JMenuItem("Paste (Ctrl+V)");
JMenuItem infoMenu = new JMenuItem("Help (Ctrl+H)");
fileMenu.add(newMenu);
fileMenu.add(openMenu);
fileMenu.add(saveMenu);
fileMenu.add(exitMenu);
editMenu.add(cutMenu);
editMenu.add(copyMenu);
editMenu.add(pasteMenu);
helpMenu.add(infoMenu);
this.add(comboBox, BorderLayout.NORTH);
this.add(text, BorderLayout.SOUTH);
frame.setJMenuBar(menuBar);
add(new JButton("Select")
{{
addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e )
{
frame.dispose();
JOptionPane.showMessageDialog(frame,"IT WORKS!");
}
});
}});
}});
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible( true );
}
}
Not sure how to reference to JPanel to use BorderLayout.
How would I go about doing this?
If you add a panel to a JFrame (using add() as you are doing here) , you can assume that it is being added as the contentPanel. However, it is much better to be explicit. Instead of this:
frame.add(new JPanel()
{}
use this:
JPanel panel = new JPanel(new BorderLayout());
// add your stuff to the panel;
frame.add(panel);
EDIT:
after looking at your edit, what is clear is that you are initializing an anonymous class. This is not generally bad practice, but here you are putting a lot of initialization code. The code you are putting in is in a double-braced block, which in essence puts it into a static initializer. It seems like a normal anonymous class, but it really isn't. With that much code, it deserves its own class. If you went so far as to code a new class, you should be fine. I would suggest that you then define it as an extension of JPanel and in your own constructor pass a new BorderLayout() to the super.
EDIT 2:
if you create a brand new file/class named Bar and you coded it like this:
public class Bar extends JPanel {
public Bar(final JFrame frame) {
String[] tests = { "A+ Certification", "Network+ Certification",
"Security+ Certification", "CIT Full Test Package" };
JComboBox comboBox = new JComboBox(tests);
TextArea text = new TextArea(5, 10);
add(new JLabel("Welcome to the CIT Test Program "));
add(new JLabel("Please select which Test Package from the list below."));
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenu editMenu = new JMenu("Edit");
JMenu helpMenu = new JMenu("Help");
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(helpMenu);
JMenuItem newMenu = new JMenuItem("New (Ctrl+N)");
JMenuItem openMenu = new JMenuItem("Open (Ctrl+O)");
JMenuItem saveMenu = new JMenuItem("Save (Ctrl+S)");
JMenuItem exitMenu = new JMenuItem("Exit (Ctrl+W)");
JMenuItem cutMenu = new JMenuItem("Cut (Ctrl+X)");
JMenuItem copyMenu = new JMenuItem("Copy (Ctrl+C)");
JMenuItem pasteMenu = new JMenuItem("Paste (Ctrl+V)");
JMenuItem infoMenu = new JMenuItem("Help (Ctrl+H)");
fileMenu.add(newMenu);
fileMenu.add(openMenu);
fileMenu.add(saveMenu);
fileMenu.add(exitMenu);
editMenu.add(cutMenu);
editMenu.add(copyMenu);
editMenu.add(pasteMenu);
helpMenu.add(infoMenu);
this.add(comboBox, BorderLayout.NORTH);
this.add(text, BorderLayout.SOUTH);
frame.setJMenuBar(menuBar);
add(new JButton("Select") {
{
addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.dispose();
JOptionPane.showMessageDialog(frame, "IT WORKS!");
}
});
}
});
}
All you would need to do to use it would be to call
JPanel panel = new Bar(frame);
however, the goal here is to use a BorderLayout, so I would suggest that you put this call in to start:
public Bar(final JFrame frame) {
super(new BorderLayout());
.... everything else
}
On top of all answers already given... Your program has fundamental flaw. All manipulation with Swing components has to be done on EDT thread. So your code should be slightly different
class FrameDemo
{
public static void main(String[] args)
{
SwingUtilities.invokeLater( new Runnable() {
void run() {
/// your code here
}
});
}
}
Otherwise what happens is unpredictable. You can read more about it at http://www.javaworld.com/javaworld/jw-08-2007/jw-08-swingthreading.html
Adding BorderLayout to it doesn't seem to work.
The default layout of a new JPanel is FlowLayout, as can be seen by resizing the frame. To see the difference, replace
frame.add(new JPanel()
with
frame.add(new JPanel(new BorderLayout())
As #akf suggests, lengthy static initialization using double braces can be obscure.