JTabbedPane component accessing - java

I use JTabbedPane in one of my Java GUI code. I use the following portion of code to instantiate and maintain the tabpane.
JTabbedPane tabpane = new JTabbedPane();
PageViewer pv = new PageViewer();
tabpane.addTab("tabttitle", new JScrollPane(pv));
PageViewer is an extended class of JEditorPane. I want to access and modify the currently selected tab's constituent PageViewer pv component. I tried the following lines of code with some values of ind.
JScrollPane jsp = (JScrollPane) tabpane.getComponentAt(tabpane.getSelectedIndex());
PageViewer pv2 = (PageViewer) jsp.getComponent(ind);
But for ind==0 compiler says "java.lang.ClassCastException: javax.swing.JViewport cannot be cast to menu_window.PageViewer".
For ind==1 it says "java.lang.ClassCastException: javax.swing.JScrollPane$ScrollBar cannot be cast to menu_window.PageViewer".
For ind==2 output is "java.lang.ClassCastException: javax.swing.JScrollPane$ScrollBar cannot be cast to menu_window.PageViewer".
And for ind>=3 error is "java.lang.ArrayIndexOutOfBoundsException: No such child: 3".
Now how do I do the desired job, if anyone knows please help.
Note: I use NetBeans 6.8 with Java 6 Standard Edition.

When you create a JScrollPane around a component, the scrollpane actually adds the component into an internal JViewPort. To get the original component from the scrollpane you can do this:
PageViewer pv2 = (PageViewer)jsp.getViewport().getView();

Related

convert form.showDialog from vb to Java

I need a similar code of form.showDialog from vb to Java to show a Frame up to its parent Frame. I've tried something like this :
private void button1ActionPerformed(java.awt.event.ActionEvent evt) {
Frame2 form = new Frame2();
form.setVisible(true);
}
and i got 2 problems,
the first : the new frame wont stay on the top alway, that mean i can select the old form
and the second problem : when i close the new form the parent form will close too !
Take a look at Swing's JDialog which has a modal property allowing the dialog to remain as the topmost window

Force Horizontal Tab Text on Left Aligned JTabbedPane

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

Add JInternalFrame to JDesktopPane using a button inside other JInternalFrame

This snippet code I got from https://stackoverflow.com/a/6868039/2240900
how to add the internal2 to desktoppane1 using a button placed somewhere in internal1.
In the ActionListener added to your button you can use code like the following to get a reference to the desktop pane:
Container container = SwingUtilities.getAncestorOfClass(JDesktopPane.class, (Component)event.getSource());
if (container != null)
{
JDesktopPane desktop = (JDesktopPane)container;
JInternalFrame frame = new JInternalFrame(...);
desktop.add( frame );
}
My question is how to add another JInternalFrame if the button reside in another JInternalFrame? ex: add internalX to desktoppane1 using a button placed somewhere in internal2/internal3/internalX, where each internal was created using a button inside internalX not using a menubar.
Any help will be appreciated. Thanks.
I accidentally find out that we can use a method of JInternalFrame that is getDesktopPane().
As mention in javadoc:
getDesktopPane
public JDesktopPane getDesktopPane()
Convenience method that searches the ancestor hierarchy for a JDesktop instance. If JInternalFrame finds none, the desktopIcon tree is searched.
Returns:
the JDesktopPane this internal frame belongs to, or null if none is found
So we can just use a command like:
JDesktopPane desktopPane = internalFrame.getDesktopPane();
desktopPane.add(internalX);
or if the class extends JInternalFrame simply use
JDesktopPane desktopPane = this.getDesktopPane();
desktoppane.add(internalX);
to get the JDesktopPane to add another JInternalFrame in a nested JInternalFrame.
Externalize the listener into it's own class, with proper parameters if needed. Then, you can instantiate this listener every time you create a new frame and apply it to its button.

How to modularize panels in GWT

Suppose I have the following layout for my page:
vertical panel mainbody = new VerticalPanel;
//beginning of 1st sub-vertical panel
VerticalPanel first_panel = new verticalPanel();
first_panel.add(...);
//other such stuff, end of first_panel
mainbody.add(first_panel);
//beginning of 2nd sub-vertical panel
VerticalPanel second_panel = new verticalPanel();
second_panel.add(...);
//other such stuff, end of first_panel
mainbody.add(second_panel);
//beginning of 3rd sub-vertical panel
VerticalPanel third_panel = new verticalPanel();
third_panel.add(...);
//other such stuff, end of first_panel
mainbody.add(third_panel);
//end of mainbody
RootPanel.get().add(mainbody);
I want to modularize it, such that each sub panel (first_panel, second_panel and third_panel) belong to individual files, so that after some import, I can just code this in the main page to do the trick:
vertical panel mainbody = new VerticalPanel;
mainbody.add(first_panel);
mainbody.add(second_panel);
mainbody.add(third_panel);
//end of mainbody
RootPanel.get().add(mainbody);
The sub-panels themselves may use other sub-panels, for which the modularization should cascade. Now what do I need to do to enable this? The Google documentation is not quite clear to me. Specifically,
How do I define those panels in individual files (packages or folders) so that they can be imported into the main file?
How do I import them (somewhat like include in PHP)?
What changes do I need to make to the xml file (if any is needed)?
How to define the same set of rules for the imported files also, so that the imports cascade? That is, if a imports b, and b imports c and d to construct itself, both c and d are imported into the main file dutring import?
Will the CSS rules in the main CSS file be enough for all imported panels?
If someone can explain this from the viewpoint of the default StockWatcher app which comes packaged with GWT (so that we can have some common ground for understanding the directory structure), it will be easy for me to understand.
You need to make a Widget out of the panels.
package com.example.widgets // Make this package whatever you want
import com.google.gwt.user.client.ui.Composite
public class FirstPanel extends Composite{
private VerticalPanel verticalPanel;
public FirstPanel(){
verticalPanel = new VerticalPanel();
// All composites must call initWidget() in their constructors.
initWidget(verticalPanel);
// For your CSS
setStyleName("example-SomeStyle");
// Continue constructing object ...
}
}
The initWidget() needs to be called exactly once every time the constructor is used. It sets the widget to be wrapped by the composite. Check the documentation here.
Then you'll be able to use it like so
VerticalPanel mainPanel = new VerticalPanel();
FirstPanel firstPanel = new FirstPanel();
mainPanel.add(firstPanel);
The above should answer point 1
If your Widget isn't in the same package as the code above you'll need to import it. For the example above: import com.example.widget.FirstPanel
No changes need to be made in the XML file
Yes, the imports will cascade as you described.
The CSS Rule in the main CSS should be enough as long as you declare the style names in the widgets. You can do that by calling setStyleName("example-SomeStyle"); as you can see in the example above.
What you are asking about is called custom widgets. You can create a custom widget to represent first_panel, another one to represent second_panel, etc. Then you can make widgets that consist of other widgets, if necessary. Each custom widget is a separate class - it will be represented by a separate file (or two files if you use Ui:Binder for your custom widget).
You can read more about custom widgets here:
https://developers.google.com/web-toolkit/doc/latest/DevGuideUiCustomWidgets

Stretching out JScrollPane - Java

I have this structure:
<JFrame>
<JPanel backgroundcolor = "pink">
<JScrollPane>
<JTable>!!!Data here !!!</JTable>
</JScrollPane>
</JPanel>
</JFrame>
How do i stretch the ScrollPane it to cover the full window without using setSize?
This is how it looks like now:
alt text http://img22.imageshack.us/img22/8491/17747996.png
Thanks!
Mmmph! Nobody offered a simple solution such as using BorderLayout as layout manager for my JScrollpane container!
I am not familiar with the XML file format.
If it is coded, you may need to code something like this:
JScrollPane1 = new JScrollPane();
JPanel1.add(JscrollPane1);
JScrollPane1.setBounds(5,29,636,122);
JTable1 = new JTable();
JPanel1.add(JTable1);
JScrollPane1.setBounds(5,434,553,3097);
JScrollPane1.setViewportView(JTable1);
Use setPreferredScrollableViewportSize() and a suitable layout.
Edit: You'll also need setFillsViewportHeight(), as discussed in Adding a Table to a Container.

Categories