I created frame with button and when it is pressed all content is removed and replaced by new one. But I can not display label, here is my code:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
getContentPane().removeAll();
jLabel2 = new javax.swing.JLabel();
jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12));
jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel2.setText("Hello World!");
jLabel2.setLocation(80, 80);
jLabel2.setVisible(true);
getContentPane().add(jLabel2);
getContentPane().repaint();
pack();
}
What am I doing wrong? :(
instead of repaint() try validate().
Instead of trying to remove all and add new components, use a CardLayout, which will "layer" panels and let you navigate between them. See How to use CardLayout and you can see a simple example here
You can also see how to use CardLayout with Netbeans GUI Builder here
You should use validate() instead of repaint. The rest of your source looks fine.
The validate method is used to cause a container to lay out its subcomponents again. It should be invoked when this container's subcomponents are modified (added to or removed from the container, or layout-related information changed) after the container has been displayed.
Related
I am trying to make a JTabbedPane in Java 7 on OSX that has tabs positioned to the left with their text horizontal (instead of vertical). However, with the code:
import javax.swing.*;
import java.awt.Dimension;
class Probs extends JDialog {
JTabbedPane options = new JTabbedPane();
Probs(JFrame owner) {
//main constructor
super(owner, "User Preferences", true);
//set the tabs to be left aligned
options.setTabPlacement(JTabbedPane.LEFT);
//construct the authorization panel
JPanel authorization = new JPanel();
authorization.add(new JLabel("test"));
options.addTab("test", authorization);
add(options);
setSize(new Dimension(300,300)); //should use pack here
setResizable(false);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
JFrame test = new JFrame();
new Probs(test);
test.dispose();
}
}
I get a dialog box which looks like: this image
I would like the tab text to be horizontal (the 'test' title on the tab be oriented horizontally instead of vertically).
I searched around on Google for a while and have only run into occurrences wherein people wanted to achieve vertical text on their tabs, I could not manage to locate any in which people wanted to have horizontal text (what I am trying to achieve).
In particular, I am trying to achieve something which exactly looks like the image mentioned in the first post of this question. It is basically the exact opposite of that question because the person in that tab started with what I am trying to achieve (I believe). Basically, I am trying to determine how to create the image displayed in the first post of that question.
Can someone please tell me how to have left-oriented tabs while preserving horizontal tab titles (as opposed to vertical)?
Thank you for your time and assistance.
Again, since I can't replicate the problem, Try this suggestion:
JPanel authorization = new JPanel();
authorization.add(new JLabel("test"));
options.addTab("", authorization);
JLabel labTab2 = new JLabel("test"); // create a label
options.setTabComponentAt(0, labTab2); // set it to the component
The alignment is determined by your operating system. If you want to change the alignment of the tab text, you have to change the look and feel of your swing application. This worked for me. See here.
The system look and feel at MacOSX didn't support what you want in JTabbedPane. You must create a customized JComponent to do this or to set the look and feel of your application to cross platform (java metal) as stated before by #MonkeySupersonic.
I suggest the readings:
Apple Java Development Guide (section: User Interface Toolkits for Java) - https://developer.apple.com/library/content/documentation/Java/Conceptual/Java14Development/04-JavaUIToolkits/JavaUIToolkits.html
mac User Interface Guidelines - https://developer.apple.com/macos/human-interface-guidelines
I have an applet I have written that has a JLabel (containing an ImageIcon) and a custom ImagePanel inside a JPanel. For some reason the JLabel NEVER shows up in safari and firefox on mac os on first run/load but on other OSes (windows,linux) it appears fine. Now in the same applet there's a button that flips the image to another image. On safari/firefox on mac os, when the button is clicked, the second image shows, the when clicked again, the first image now appears!! Any idea what could be causing this issue? Even on safari for windows the applet works fine.. i.e. first image loads and appears.
UI code
public void createUI(){
mainpanel = new JPanel();
mainpanel.setMaximumSize(new Dimension(154, 212));
mainpanel.setMinimumSize(new Dimension(154, 212));
mainpanel.setName("mainPanel");
mainpanel.setLayout(new BorderLayout());
lcdpanel = new ImagePanel(bgLcdImage);
lcdpanel.setBounds(22, 22, 110, 28);
bgImage = Toolkit.getDefaultToolkit().createImage(bytes);//BufferedImage
label = new JLabel(new ImageIcon(bgImage));
mainpanel.add(lcdpanel);
mainpanel.add(label);
mainpanel.invalidate();
getContentPane().add(mainpanel);
repaint();
}
Button click code
private void flipImage()
{
label.setIcon(new ImageIcon(backImg));
label.repaint();
lcdpanel.setVisible(false);
lcdpanel.repaint();
mainpanel.repaint();
this.repaint();
}
Any help would be appreciated.Thanks
I even made the jlabel as a imagepanel, set the layout of jpanel to null
Setting the layout to null is the worst thing you can do. That will generally cause more problems than solve a problem.
The issue is not where i want the jlabel to be shown but WHY its not showing on Mac OS X firefox/safari browsers when it shows on windows/linux firefox/safari browsers.
How do we know when only a few lines of code are posted? Post a proper SSCCE when you have a problem.
On safari/firefox on mac os, when the button is clicked, the second image shows, the when clicked again, the first image now appears!!
The general format when adding/removing components on a visible GUI is to do:
panel.add(...);
panel.revalidate();
panel.repaint(); // sometimes needed
You never need to invoke repaint when you change the property of a component. Swing is smart enough to do the repaint for you.
label.setIcon(new ImageIcon(backImg));
//label.repaint();
In my swing application, I set a Vector as data to JList. JList is resizable (vertical and horizontal). Number of viewable items is 8. When data is set morethan 8 items to the JList, the Scroller is not visible and to see the rest of items, JList should be pull down. Is there any way to get this JScroller ?
I use Netbeans IDE 7.2 I checked the JScrollerPane's properties. It has default values for horizontal and vertical scroll bars.
Here is JScrollPane and JList codes which NetBeans has created.
JScrollPane
jScrollPane1 = new javax.swing.JScrollPane();
This is JList
jList1 = new javax.swing.JList();
jList1.setBackground(new java.awt.Color(##, ##, ##));
jList1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(##, ##, ##), 1, true));
jList1.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
jList1.setForeground(new java.awt.Color(##, ##, ##));
jList1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
jList1.setCellRenderer(new CellRendererManager());
jList1.setMaximumSize(new java.awt.Dimension(30, 80));
jList1.setMinimumSize(new java.awt.Dimension(30, 80));
jList1.setPreferredSize(new java.awt.Dimension(30, 80));
jList1.setValueIsAdjusting(true);
jScrollPane1.setViewportView(jList1);
`
You state:
JScrollPane lays on FreeDesign layout which shows in NetBeans IDE. JScrollPane has not been set any size.But the JList has set a dimension(30,80)
There's your problem -- you're setting the Dimension of the JList which prevents it's view from expanding, even if it is held by a JScrollPane.
Solution: don't do this. Read up on and use the various layout managers so that they and your component's innate preferred sizes do all the sizing and heavy lifting for you.
I have a fairly simple question. I have a JPanel on a JFrame. I have a JLabel on the JPanel. How, I wonder, do i FULLY REMOVE the JLabel from the JPanel during runtime?
ImageIcon image7= new ImageIcon("archmageanim.gif");
JLabel label7 = new JLabel("", image7, JLabel.CENTER);
p.add( label7, "0 , 6" ); //This coordinate has to do with a layout manager I'm using - it
//I'm using - it works fine.
I have looked for this solution...but everyone says "the easiest way" is to set setVisible(false)...but that doesn't truly remove the object -_-. How can I REMOVE it?
Can't you just use this to find the parent Container of the JLabel and then use the remove method?
Container parent = label7.getParent();
parent.remove(label7);
parent.validate();
parent.repaint();
That should remove the label altogether and then refresh the parent Container.
It's this.
jpanel.remove(label7);
jpanel.revalidate();
jpanel.repaint();
jpanel.remove(component);
This is all you need to call to remove a component.
I'm having trouble embedding Swing components inside SWT (such as eclipse plugin..)
Currently what I have:
public void createPartControl(Composite parent) {
java.awt.Frame f = SWT_AWT.new_Frame(parent);
JPanel panel = new JPanel(new BorderLayout());
JButton button = new JButton("Swing button");
JLabel label = new JLabel("Swing label");
panel.add(label,BorderLayout.NORTH);
panel.add(button,BorderLayout.CENTER);
f.add(panel);
}
This code snippet fails to load, the plugin crashes on the first line...
Any idea how to incorporate these components?
Thanks!
http://www.eclipse.org/articles/article.php?file=Article-Swing-SWT-Integration/index.html
Minimally, embedding an AWT frame inside an SWT composite is just two simple lines of code
Composite composite = new Composite(parent, SWT.EMBEDDED | SWT.NO_BACKGROUND);
Frame frame = SWT_AWT.new_Frame(composite);
Since your code is failing at the first line then please first make sure that the parent Composite is created using SWT.EMBEDDED. If it is not then create a child composite using the SWT.EMBEDDED and then call
java.awt.Frame f = SWT_AWT.new_Frame(newChildComposite);
An instance of
org.eclipse.swt.Composite is created
with the SWT.EMBEDDED style. This
style signals that an AWT frame is to
be embedded inside the Composite. The
call to the static new_Frame method
creates and returns such a frame. The
frame may then be populated with AWT
and/or Swing components.
Taken from Article-Swing-SWT-Integration