JTabbedPane within JScrollPane only showing one item at a time - java

When I launch my application, it launches the JFrame and loads up the JTabbedPane which contains the JScrollPane, yet it only shows one component inside it at a time. I have tried everything, and still I cannot solve the problem...
Here is my code:
package test;
import java.awt.*;
import javax.swing.*;
public class Main extends JFrame{
public Main()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400,500);
JPanel pane=new JPanel();
pane.setLayout(new BorderLayout());
UIManager.put("TabbedPane.contentOpaque", false);
JTabbedPane tabbedPane=new JTabbedPane();
JScrollPane scrollPane=new JScrollPane(pane);
tabbedPane.setPreferredSize(new Dimension(getWidth(),getHeight()));
for(int i = 0; i < 10; i++) pane.add(new JLabel("label22222222222222222222222222222222222222222222222222222222"+i));
//pane.add(scrollPane,BorderLayout.CENTER);
tabbedPane.add("Test",scrollPane);
add(tabbedPane);
}
public static void main(String[] args) {
Main main=new Main();
main.setVisible(true);
}
}
Please help me, I have no idea what I am doing wrong.

Your pane JPanel uses BorderLayout and you're adding components in a default fashion, or BorderLayout.CENTER. This is the expected behavior to show only the last component added.
You should consider using another layout such as GridLayout. Also, Google and read the "laying out components in a container" tutorial and understand the layouts that you're using.
Also, consider using a JList to display your data rather than a grid of JLabels.
As an aside, you should format your code for readability, not compactness. Don't put for loops on one line only. In fact all loops and blocks should go into curly braces to prevent your later editing your code, adding another line and thinking that it's in the loop when it's not.
Edit
For example, using a JList:
import javax.swing.*;
public class Main2 {
private static final int MAX_CELLS = 30;
private static void createAndShowGUI() {
final DefaultListModel<String> listModel = new DefaultListModel<>();
final JList<String> myList = new JList<>(listModel);
myList.setVisibleRowCount(8);
for (int i = 0; i < MAX_CELLS; i++) {
listModel.addElement("label22222222222222222222222222222222222222222222222222222222" + i);
}
JTabbedPane jTabbedPane = new JTabbedPane();
jTabbedPane.add("Test", new JScrollPane(myList));
JFrame frame = new JFrame("Main2");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(jTabbedPane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}

Related

Java: JTextField won't appear

public class HandleUI {
public static void setUpUI(){
JPanel jPan = new JPanel();
FlowLayout flow = new FlowLayout();
jPan.setLayout(flow);
txtFld = new JTextField();
txtFld.setSize(550,5);
jPan.add(txtFld);
jPan.setSize(10,200);
MainClass.mainFrame.add(jPan);
int gapX = MainClass.mainFrame.getX()-(txtFld.getX()/2);
}
//Instance variables.
public static JTextField txtFld;
public JButton [] buttons;
}
public class MainClass {
public static void main (String [] args){
int frameX = Constants.FRAME_WIDTH;
int frameY = Constants.FRAME_HEIGHT;
mainFrame = new JFrame();
mainFrame.setSize(frameX,frameY);
mainFrame.setResizable(false);
mainFrame.setVisible(true);
HandleUI.setUpUI();
}
//Instance variables
public static JFrame mainFrame;
}
It's supposed to show JTextField, but as you might have guessed - JFrame shows nothing. I didn't type in imports on purpose, but they are all there. I can't find the problem. Can anyone help?
1.) Simply write:
JTextField tField = new JTextField(10);
Here In the constructor you are passing the number of columns, which is sufficient for a layout like FlowLayout to set the size of the JTextField
2.) The line mainFrame.setVisible(true); must be the last line of the main method. You need to put the code at main() method, inside SwingUtilities.invokeLater(...) thingy.
3.) Instead of setting size on the JFrame use JFrame.pack(), to set the window to the preferred size.
4.) Creation of unnecessary static members is a design flaw. Try to keep yourself away from such thingies.
5.) Read a bit about Concurrency in Swing
One Example Program for help(Use the order of lines as specified in this answer):
import java.awt.*;
import javax.swing.*;
public class Example {
private void displayGUI() {
JFrame frame = new JFrame("Example Demo");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel contentPane = new JPanel();
JTextField tField = new JTextField(10);
contentPane.add(tField);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
Runnable runnable = new Runnable() {
#Override
public void run() {
new Example().displayGUI();
}
};
EventQueue.invokeLater(runnable);
}
}
You have to call setVisible(true) on your JFrame AFTER having initialised your UI.
Simply pulling the following line:
HandleUI.setUpUI();
... right before:
mainFrame.setVisible(true);
... will do the trick.
As a side note, I'd like to point out that setting the size of your text field won't really work like you did. You probably would use setPreferredSize(Dimension) instead. Or, even better, organise your UI only using Layouts and not manually setting any component's size.

Custom component creation in intellij fails

I just want to set custom list model for ComboBox. The commented code also didn't worked. I completely don't know why!
I'm working in Intellij Community Edition under JDK 1.8
import jssc.SerialPortList;
import javax.swing.*;
import java.awt.*;
public class sampleForm extends JFrame {
private JComboBox comboBox1;
private JPanel panel1;
/*public sampleForm() {
super("title");
String[] portNames = SerialPortList.getPortNames();
setLayout(new FlowLayout());
comboBox1 = new JComboBox(portNames);
add(comboBox1);
}*/
public static void main(String[] args) {
JFrame frame = new JFrame("sampleForm");
frame.setContentPane(new sampleForm().panel1);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
private void createUIComponents() {
// TODO: place custom component creation code here
String[] portNames = SerialPortList.getPortNames();
comboBox1 = new JComboBox(port);
}
}
Your class is already a JFrame, but you create another JFrame instance in the main method. So whatever you added in the sampleForm constructor has no affect visually because the sampleForm instance isn't the frame your showing. You're showing the new JFrame instance. So instead, do
public sampleForm() {
super("title");
String[] portNames = SerialPortList.getPortNames();
comboBox1 = new JComboBox(portNames);
panel1 = new JPanel(); // default FlowLayout
panel.add(comboBox1);
setContentPane(panel1);
}
public static void main(String[] args) {
sampleForm frame = new sampleForm("sampleForm");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
Also, see Initial Threads. Swing apps should be run on the Event Dispatch Thread.

JFrame inside another JFrame

I have a game of chess.
I have written 3 classes.
1st if for game. (chessboard, pieces, and so on)
And another one is for menu. (buttons like new, open, set time)
Both of them use JFrame.
I would like to put both classes mentioned above into the 3rd class. For example the Game window would be on the left, and the menu on the right.
The third class would also show the whole app by JFrame.
How to do that?
You can't put one JFrame inside another. You have a couple of design choices here. You can change your JFrames to JPanels. This is probably the easiest change. On the other hand, you can look at using Internal Frames instead.
In your case i suggest you to use JInternalFrame which can be added inside Jframe try out this code i hope it will work:
package demo;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
public class Demo {
public static void main(String[] args) {
JFrame jf=new JFrame();
jf.setLayout(null);
jf.setSize(1280, 720);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JInternalFrame jInternalFrame=new JInternalFrame();
jInternalFrame.setLocation(100, 100);
jInternalFrame.setSize(500, 300);
jInternalFrame.setTitle("Internal frame");
jInternalFrame.setVisible(true);
jInternalFrame.setClosable(true);
jInternalFrame.setResizable(true);
jf.add(jInternalFrame);
jf.repaint();
}
}
You can use JPanels for that. It's easier that way... use a JFrame for the main window and for menu items use a JPanel inside it. Search for tutorials on JPanel usage.
The best thing to do would be to leave the outer frame as it is and change the inner contents to JPanels. When I wrote Chess, I had an outer frame which extended JFrame, and inner panel that extended JPanel on which I placed the board. The board itself was comprised of 64 JButtons.
Given your description, I think this would be a good starting point:
package data_structures;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
#SuppressWarnings("serial")
public class Chess extends JFrame implements ActionListener {
private JButton[][] tiles;
public Chess() {
setTitle("Chess");
setSize(500, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setLayout(new BorderLayout());
JPanel board = new JPanel();
board.setLayout(new GridLayout(8, 8));
tiles = new JButton[8][8];
for(int y = 0; y < tiles.length; y++) {
for(int x = 0; x < tiles[y].length; x++) {
tiles[x][y] = new JButton();
tiles[x][y].setActionCommand(x + " " + y);
tiles[x][y].addActionListener(this);
board.add(tiles[x][y]);
}
}
add(board, BorderLayout.CENTER);
JPanel options = new JPanel();
options.setLayout(new GridLayout(1, 3));
JButton newGame = new JButton("New");
newGame.addActionListener(this);
options.add(newGame);
JButton openGame = new JButton("Open");
openGame.addActionListener(this);
options.add(openGame);
JButton setTime = new JButton("Set Time");
setTime.addActionListener(this);
options.add(setTime);
add(options, BorderLayout.SOUTH);
revalidate();
}
public void actionPerformed(ActionEvent event) {
String command = event.getActionCommand();
System.out.println(command);
revalidate();
}
public static void main(String[] args) {
new Chess();
}
}
Also, a word of warning:
Fully implementing the logic of Chess is very difficult, no matter what you do for the graphics.
Hope this helps!
I guess that's what you want to do.
public class OuterFrame extends JFrame {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
OuterFrame outerFrame = new OuterFrame();
outerFrame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public OuterFrame() {
JFrame innerFrame = new JFrame();
innerFrame.setVisible(true);
}
}
You have a MainFrame (OuterFrame), and you create it. But, you create a JFrame inside this MainFrame. That's not a beautiful thing to do, but it's certainly a way of opening a "JFrame inside the other". This will give you two "windows" on the screen. You could create countless JFrames inside the MainFrame.

JLabel tooltiptext not showing

I expected this to be very simple and straightforward, but tooltiptext is not showing upon hovering the mouse over. I tried printing the text and it prints correctly. Any comments what I'm doing wrong?
public class gui2 extends JFrame {
private JLabel item1;
public gui2() {
super("The title bar");
setLayout(new FlowLayout());
item1 = new JLabel("label 1");
item1.setToolTipText("This is a message");
String str = item1.getToolTipText();
System.out.println(str);
add(item1);
}
class gui {
public static void main(String[] args) {
gui2 g2 = new gui2();
g2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
g2.setSize(400, 200);
g2.setVisible(true);
}
}
}
Your code does not compile even if you add the imports. Here is your code corrected and working :
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Gui {
public static void main(String[] args) {
Window window = new Window();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(400, 200);
window.setVisible(true);
}
}
class Window extends JFrame {
private static final long serialVersionUID = 1L;
private JLabel jlabel;
public Window() {
super("The title bar");
setLayout(new FlowLayout());
jlabel = new JLabel("label 1");
jlabel.setToolTipText("This is a message");
String str = jlabel.getToolTipText();
System.out.println(str);
add(jlabel);
}
}
As mentioned by #restricteur your code does not compile.
This is due to the fact that your class gui which holds the main(..) is nested within another class, hence no static declaration of method is allowed unless the nested class is marked static. ( I simply moved/un-nested Gui out from Gui2)
Besides that your code does work, I think you are being hasty - hold the mouse over JLabel for like 3-4 seconds and you should see the ToolTip appear:
(using your code with no compilation error of course):
Suggestions on code:
1) Please watch java naming conventions i.e class names should begin with a capital letter and each new word thereafter should also i.e gui becomes Gui or GUI but I prefer the former.
2) Dont call setSize on JFrame use and= appropriate LayoutManager and call pack() on JFrame before setting it visible (but after components have been added).
3) Dont extend JFrame unnecessarily simply create an instance and use that.
4) Always create and manipulate Swing Components on Event Dispatch Thread via SwingUtilities.invokeLater(Runnable r) block.
5) Opt for setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); unless using Timers as this will allow main(..) to continue regardless if GUI is exited.
Here is code with above fixes:
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
class Gui2 {
private JLabel item1;
private JFrame frame;
public Gui2() {
frame = new JFrame("The title bar");
frame.setLayout(new FlowLayout());
item1 = new JLabel("label 1");
item1.setToolTipText("This is a message");
String str = item1.getToolTipText();
System.out.println(str);
frame.add(item1);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
class Gui {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Gui2();
}
});
}
}

Java (Swing): JScrollPane.setBounds() does not apply?

I'm trying to create a simple JList with a scrollbar, and therefore i need to have the JList within a JScrollPane. So far, so good. However, for some reason i can't resize/position the JScrollPane!? It sounds logic that everything inside it should stretch to 100%, so if i set the JScrollPane to be 300px wide, the elements inside will be as well. Is that correct?
While you're at it, please critisize and give me hints if i should change something or optimize it.
Anyhow, here's the code:
package train;
import java.awt.*;
import javax.swing.*;
public class GUI {
private DefaultListModel loggerContent = new DefaultListModel();
private JList logger = new JList(loggerContent);
GUI() {
JFrame mainFrame = new JFrame("title");
this.addToLog("testing testing");
this.addToLog("another test");
// Create all elements
logger = new JList(loggerContent);
JScrollPane logWrapper = new JScrollPane(logger);
logWrapper.setBounds(10, 10, 20, 50);
// Add all elements
mainFrame.add(logWrapper);
// Show everything
mainFrame.setSize(new Dimension(600, 500));
mainFrame.setVisible(true);
}
public void addToLog(String inputString) {
int size = logger.getModel().getSize();
loggerContent.add(size, inputString);
}
}
Thanks in advance,
qwerty
EDIT: Here's a screenshot of it running: http://i.stack.imgur.com/sLGgQ.png
The setVisibleRowCount() method of JList is particularly convenient for this, as suggested in the relevant tutorial. ListDemo is a good example.
Addendum:
please critisize and give me hints…
Well, since you ask: Don't invoke public methods in the constructor; make them private or invoke them after the constructor finishes. There's no need to find the last index for add(), when addElement() is available. Also, be sure to construct your GUI on the event dispatch thread .
import java.awt.*;
import javax.swing.*;
/** #see http://stackoverflow.com/questions/5422160 */
public class ListPanel extends JPanel {
private DefaultListModel model = new DefaultListModel();
private JList list = new JList(model);
ListPanel() {
list.setVisibleRowCount(5);
}
public void append(String inputString) {
model.addElement(inputString);
}
private void init() {
for (int i = 0; i < 10; i++) {
this.append("String " + String.valueOf(i));
}
JFrame mainFrame = new JFrame("GUI");
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JScrollPane jsp = new JScrollPane(list);
mainFrame.add(jsp);
mainFrame.pack();
mainFrame.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new ListPanel().init();
}
});
}
}
The bounds & size of a component are generally ignored over that of it's preferred size and the constraints of the layout being used by the container.
To solve this problem, learn how to use layouts & apply them appropriately.
Try to put your JScrollPane inside a JPanel and add the panel to the frame.
JPanel panel = new JPanel();
panel.add (logWrapper);
mainFrame.add(panel);
Then set the bounds of the panel instead of the JScrollpane
panel.setBounds(10, 10, 20, 50);
The probles is that Swing uses layout managers to control child bounds property. Adding a JScrollpane directly to the main frame, doesn't allow you to choose right bounds properly.

Categories

Resources