How do I get a textbox to appear on this JFrame? Also is it good practice to build everything on top of the JFrame itself? Or is it better to overlay a JPanel and build everything on top of that?
Thanks in advance!
public class GUI {
private static JFrame frame = new JFrame("FrameDemo");
public GUI() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BufferedImage myImage = null;
try {
myImage = ImageIO.read(new File("C:/Users/Desktop/background.jpg"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
frame.setContentPane(new ImageFrame(myImage));
JTextField field = new JTextField(10);
frame.add(field, BorderLayout.SOUTH);
Dimension dimension = new Dimension();
dimension.setSize(950, 800);
frame.setSize(dimension);
frame.setVisible(true);
}
}
You have replaced the default content pane with you own content pane. I would guess your content pane does not use a layout manager so the text field is never displayed.
Try something like:
//frame.setContentPane(new ImageFrame(myImage));
ImageFrame content = new ImageFrame(myImage));
content.setLayout( new BorderLayout() );
frame.setContentPane(content);
Now you text field should be added to the south of your image panel.
Also is it good practice to build everything on top of the JFrame itself? Or is it better to overlay a JPanel and build everything on top of that?
The content pane of a JFrame is a JPanel, so it doesn't really matter what you do since your will be using a panel either way. The key is to manage the layout manager of your content pane.
Related
I am trying to get a JInternalFrame to appear on my screen when a button is pressed, a pop up effect basically. However when the button is pressed the JInternalFrame does not appear on the screen. Also when I resize the screen all the elements expand with it, I am wondering if there is a way to get a pop up window to appear on the screen and keep the layout manager I have now still in place so that when the window is resized the elements are also resized with it
public class testing2 implements ActionListener {
JButton buttonAppear = new JButton();
JLayeredPane LayeredPane = new JLayeredPane();
public static void main(String[] args) {
new testing2();
}
public testing2() {
LayeredPane = new JLayeredPane();
BorderLayout borderlayoutpane = new BorderLayout();
LayeredPane.setLayout(borderlayoutpane);
JPanel mainPanel = new JPanel();
BorderLayout borderlayout = new BorderLayout();
mainPanel.setLayout(borderlayout);
JButton button = new JButton("Button");
mainPanel.add(button, "Center");
buttonAppear = new JButton("Panel Appear");
buttonAppear.addActionListener(this);
mainPanel.add(buttonAppear, "South");
LayeredPane.add(mainPanel, 2);
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(LayeredPane);
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource() == buttonAppear)
{
JInternalFrame inFrame = new JInternalFrame("Internal Frame", true, true, true, true);
inFrame.setBounds(10, 10, 200, 200);
inFrame.setVisible(true);
LayeredPane.add(inFrame, 1);
}
}
}
a pop up effect basically.
Then use a JDialog. A JInternalFrame was designed to work with a JDesktopPane.
mainPanel.add(button, "Center");
Don't use hardcode strings for the constraint. Use the field provided by the API:
mainPanel.add(button, BorderLayout.CENTER);
Also, follow Java naming conventions. Variable names should NOT start with an upper case character. Be consistent.
Don't know if it will make a difference but components with a higher layer number are painted on top of components with a lower index. So I would guess the panel (which is opaque) would just paint over top of the internal frame. Read the section from the Swing tutorial on How to Use Layered Panes. Also read the section on How to Use Root Panes to find the special variable for "popups" on a layered pane.
I'm using IntelliJ and I have a form full of components placed inside several JPanel containers. I would like to change only a single component of a single panel when the button is pressed. I tried to remove all the components of that panel and add what I need but no results.
To be precise I would like a JLabel to become a JTextField.
How can I do this without drawing the GUI again and only changing that component?
You should use the jframe.remove(Component comp) method and than use the jframe.add(Component comp).finally, don't forget the jframe.revalidate() method to update your JFrame.
for example:
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel l = new JLabel("hello");
f.add(l);
f.pack();
f.setVisible(true);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JTextField tf = new JTextField();
f.remove(l);
f.add(tf);
f.revalidate();
After 3 seconds, the JLabel in the JFrame will replaced by a JTextField.
I have a class that returns a JPanel:
public static JPanel program(String csvName) {
JPanel f = new JPanel();
try {
String path = System.getProperty("user.dir");
String datafile = path+"/files/logic/"+csvName+".csv";
FileReader fin = new FileReader(datafile);
DefaultTableModel m = createTableModel(fin, null);
JTable table = new JTable(m);
JScrollPane stable = new JScrollPane (table);
stable.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
stable.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
f.add(stable);
f.setMinimumSize(new Dimension(900,500));
JFrame desktopFrame = new JFrame();
desktopFrame.add(f);
desktopFrame.setSize(900, 500);
desktopFrame.setVisible(true);
toExcel(m, new File(path+"/files/logic/"+csvName+".csv"));
} catch (Exception e) {
e.printStackTrace();
}
return f;
}
And this is what is used to display the JPanel Modally.
String csv = "war";
JPanel f = T1Data.program(csv);
JDialog desktopFrame = new JDialog();
desktopFrame.add(f);
desktopFrame.setModal(true);
desktopFrame.setSize(900, 500);
desktopFrame.setVisible(true);
However the result I am getting has the JPanel centered and not fitting the JDialog.
It looks like this:
http://gyazo.com/4bc360e7d2c7cf7117a95d748d520838.png
How can I fix this?
The JPanel is using a FlowLayout, if you change it to a BorderLayout, the scroll panel will be laid out so it fills the full container.
You should also consider using JDialog#pack over setSize as well
To make the panel fit the size of the dialog, you can either change the LayoutManager of the dialog, or ,since this should obviously be the only panel added to the dialog, simply set the panel as contentpane (desktopFrame.setContentPane(f) instead of desktopFrame.add(f)).
I am trying to get a JInternalFrame to appear on my screen when a button is pressed, a pop up effect basically. However when the button is pressed the JInternalFrame does not appear on the screen. Also when I resize the screen all the elements expand with it, I am wondering if there is a way to get a pop up window to appear on the screen and keep the layout manager I have now still in place so that when the window is resized the elements are also resized with it
public class testing2 implements ActionListener {
JButton buttonAppear = new JButton();
JLayeredPane LayeredPane = new JLayeredPane();
public static void main(String[] args) {
new testing2();
}
public testing2() {
LayeredPane = new JLayeredPane();
BorderLayout borderlayoutpane = new BorderLayout();
LayeredPane.setLayout(borderlayoutpane);
JPanel mainPanel = new JPanel();
BorderLayout borderlayout = new BorderLayout();
mainPanel.setLayout(borderlayout);
JButton button = new JButton("Button");
mainPanel.add(button, "Center");
buttonAppear = new JButton("Panel Appear");
buttonAppear.addActionListener(this);
mainPanel.add(buttonAppear, "South");
LayeredPane.add(mainPanel, 2);
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(LayeredPane);
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource() == buttonAppear)
{
JInternalFrame inFrame = new JInternalFrame("Internal Frame", true, true, true, true);
inFrame.setBounds(10, 10, 200, 200);
inFrame.setVisible(true);
LayeredPane.add(inFrame, 1);
}
}
}
a pop up effect basically.
Then use a JDialog. A JInternalFrame was designed to work with a JDesktopPane.
mainPanel.add(button, "Center");
Don't use hardcode strings for the constraint. Use the field provided by the API:
mainPanel.add(button, BorderLayout.CENTER);
Also, follow Java naming conventions. Variable names should NOT start with an upper case character. Be consistent.
Don't know if it will make a difference but components with a higher layer number are painted on top of components with a lower index. So I would guess the panel (which is opaque) would just paint over top of the internal frame. Read the section from the Swing tutorial on How to Use Layered Panes. Also read the section on How to Use Root Panes to find the special variable for "popups" on a layered pane.
I want to add a scroll bar into my text area and I know the simple code for adding scroll bar but when I put the code for scroll bar the whole text area disappears!
What is the problem?
Here is my code:
private JFrame frame;
private JTextArea textarea;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
SmsForm window = new SmsForm();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public SmsForm() {
initialize();
}
private void initialize() {
frame = new JFrame("???");
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JPanel groupBoxEncryption = new JPanel();
final JTextArea textarea=new JTextArea();
textarea.setBounds(50, 100, 300, 100);
frame.getContentPane().add(textarea);
textarea.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
JScrollPane scrollPanePlain = new JScrollPane(textarea);
groupBoxEncryption.add(scrollPanePlain);
scrollPanePlain.setBounds(100, 30, 250, 100);
scrollPanePlain.setVisible(true);
There are a number of issues
You need to add the JPanel groupBoxEncryption to the application JFrame
Don't add the textarea to the frame - components can only have one parent component
As already mentioned, you're using null layout which doesnt size components - forget about not a layout manager.
As JPanel uses FlowLayout by default, you need to override getPreferredSize for the panel groupBoxEncryption. Better yet use a layout manager such as GridLayout that automatically sizes the component
Example
JPanel groupBoxEncryption = new JPanel(new GridLayout());
Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or combinations of them, along with layout padding & borders for white space.
Suggest a preferred size for the text area in the number of rows and columns.
Add the text area to a scroll pane before then adding the scroll pane to the GUI.