JTextPane/JScrollPane display issue - java

I 'm trying to wrap a JTextPane with a JScrollPane, and I need the background to be transparent.
Here's my code:
public CharPanel(){
super.setLayout(null);
JButton LButton = new JButton("<");
LButton.setBounds(0, 300, 50, 50);
super.add(LButton);
JButton RButton = new JButton(">");
RButton.setBounds(950, 300, 50, 50);
super.add(RButton);
JTextPane Bio = new JTextPane();
Bio.setBackground(new Color(0, 0, 0, 0));
JScrollPane BioScroll = new JScrollPane(Bio);
BioScroll.setBackground(new Color(0, 0, 0, 0));
BioScroll.setBounds(0, 500, 1000, 150);
super.add(BioScroll);
}
This is what it looks like normally: https://gyazo.com/db7e4d2a5668b36ffc617cefb1423fc4
This is what it looks like after I enter a character: https://gyazo.com/1509d952005195d6e14365f0ae2da69a
See the weird visual bug?

Bio.setBackground(new Color(0, 0, 0, 0));
Don't use Colors with an alpha value. This breaks Swing painting rules and Swing don't know how to paint the component properly and you get painting artifacts.
For full transparency just use:
component.setOpaque( false );
Check out Backgrounds With Transparency for more information on this and a solution if you need to use partial transparency.

Related

Is it possible to outline the text in a JButton?

The question is in the title.
Here's my code :
JButton button = new JButton("JOUER", new ImageIcon(getClass().getResource("template.png")));
button.setBounds(0, 0, 215, 84);
button.setBackground(new Color(0,0,0,0));
button.setFont(new Font("Bebas Neue Bold", Font.TRUETYPE_FONT, 50));
button.setForeground(Color.WHITE);
button.setVerticalTextPosition(SwingConstants.CENTER);
button.setHorizontalTextPosition(SwingConstants.CENTER);
Here's the result :
And I just want to know if is it possible to outline my text.
Here's the result I want to have:

how to set JtextArea focus in right position?

when i enable or request focus in text area the focus or caret anything u name it, it located at left. but when i start typing, it change to right.
please help me to make it on right when request focus(true);
this is the properties of textarea:
textManualIn = new JTextArea();
textManualIn.setLocale(new Locale("ar", "AE"));
textManualIn.setMinimumSize(new Dimension(5, 22));
textManualIn.setPreferredSize(new Dimension(5, 22));
textManualIn.setFocusCycleRoot(true);
textManualIn.setFocusTraversalPolicyProvider(true);
textManualIn.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
textManualIn.setRequestFocusEnabled(true);
textManualIn.setFont(new Font("Calibri", Font.PLAIN, 14));
textManualIn.setSelectionColor(new Color(153, 255, 255));
textManualIn.setMargin(new Insets(2, 5, 2, 5));
textManualIn.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
textManualIn.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
textManualIn.setCaretColor(Color.BLACK);
textManualIn.setBackground(new Color(255, 255, 255));
mainmanual.add(status);//add to Jpanel
This is not perfect, but you can try this code:
textManualIn.setText(" "); // one space
// And use one of the following
textManualIn.setCaretPosition(0);
// -or-
textManualIn.select(0, 1);

Why i cant see my buttons?

why if i have an image background i cant see all my buttons? only if my arrow go on button i see it..
this is my jlabel with background:
JLabel sfondo = new JLabel("");
sfondo.setBounds(0, 0, 832, 527);
contentPane.add(sfondo);
sfondo.setIcon(new ImageIcon(
"C:\\Users\\bocci\\workspace\\it's time of the series\\src\\immagini\\icone\\sfondo.jpg"));
sfondo.setIcon(icona3.newicona(patchsfondo, sfondo));
and i have 5 jbuttons
JButton btnOpzioni = new JButton("Opzioni");
btnOpzioni.setBounds(497, 5, 67, 23);
contentPane.add(btnOpzioni);
i see only first button.

Add 2 JTextPane to one scrollPane

I am trying to add 2 JTextPane to one scrollPane. But it not scrolling. What am I doing wrong?
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(402, 211, 178, 193);
frame.getContentPane().add(scrollPane);
JPanel panel = new JPanel();
scrollPane.setViewportView(panel);
panel.setLayout(null);
JTextPane textPane_branding = new JTextPane();
textPane_branding.setBounds(98, 0, 78, 191);
panel.add(textPane_branding);
JTextPane textPane_trunk = new JTextPane();
textPane_trunk.setBounds(0, 0, 88, 191);
panel.add(textPane_trunk);
I'm not quite sure what you are trying to achieve here. If you want both of your JTextPane to be scrollable you need to put each one into its own JScrollPane. That would look like this:
JTextPane textPane_branding = new JTextPane();
JScrollPane scroll_branding = new JScrollPane(textPane_branding);
scroll_branding.setBounds(98, 0, 78, 191);
panel.add(scroll_branding);
JTextPane textPane_trunk = new JTextPane();
JScrollPane scroll_trunk = new JScrollPane(textPane_trunk);
scroll_trunk.setBounds(0, 0, 88, 191);
panel.add(scroll_trunk);
If you want both of your JTextPane being into one JPanel that is scrollable I'm wondering why you set fixed Bounds to your JScrollPane and to the JTextPanes. That makes Scrolling absurd here. And thats why JScrollPanes don't work with Panes that don't have Layouts and use fixed Bounds. Also it is very bad practise.
So I would suggest here to stay with a Layout Manager in your JPanel and use setPreferredSize with your JTextPanes to define your desired Dimensions. And then your JScrollPane will start working.
JScrollPane scrollPane = new JScrollPane();
//scrollPane.setBounds(402, 211, 178, 193); // Don't do this!
frame.getContentPane().add(scrollPane);
JPanel panel = new JPanel();
scrollPane.setViewportView(panel);
//panel.setLayout(null); // Use a Layout Manager
JTextPane textPane_branding = new JTextPane();
textPane_branding.setPreferredSize(new Dimension(78,191));
//textPane_branding.setBounds(98, 0, 78, 191);
panel.add(textPane_branding);
JTextPane textPane_trunk = new JTextPane();
textPane_trunk.setPreferredSize(new Dimension(88,191));
//textPane_trunk.setBounds(0, 0, 88, 191);
panel.add(textPane_trunk);

how to make JTable display all records form database

My problem is when i run the program the data displayed at the JTable but not all records are displayed although i have a scroll Pane and aJtable inside it,when i run it provides a scroll but for the firs few records ,the jtable have a textfield to provide searching in table using rowSorter ,although jtable doesn't show all records when i search for a record that is not viewed i gets it.this is an image describing the problem.please how can i fix that.
this is the link i have no permission to attache an image here
http://www.megafileupload.com/en/file/561971/Untitled-jpg.html
this is piece of code where i initialize these component
table2 = new JTable(tableModel);
table2.setFont(new Font("Tahoma", Font.PLAIN, 16));
table2.getTableHeader().setFont(new Font("Tahoma", Font.BOLD, 14));
scrollPane_1.setColumnHeaderView(table2.getTableHeader());
table2.setBackground(new Color(210, 180, 140));
table2.setBorder(new LineBorder(new Color(105, 105, 105)));
table2.setForeground(new Color(0, 0, 0));
sorter2 = new TableRowSorter<TableModel>(table2.getModel());
table2.setRowSorter(sorter2);
JViewport vp2 = new JViewport();
vp2.setBackground(new Color(210, 105, 30));
vp2.setForeground(new Color(0, 0, 0));
scrollPane_1.setViewportView(vp2);
vp2.add(table2);
this is the method that fill the table
table2Data = new Vector<>();
Iterator<SellBean> buyIt = boughtedList.iterator();
while (buyIt.hasNext()) {
SellBean buyBean = buyIt.next();
Vector rec = new Vector<>();
rec.add(buyBean.getPRODUCT_ID());
rec.add(buyBean.getPRODUCT_NAME());
// System.out.println(buyBean.getPRODUCT_NAME());
rec.add(buyBean.getQUANTITY());
rec.add(buyBean.getBUY_PRICE());
rec.add(buyBean.getSELL_PRICE());
rec.add(buyBean.getPROFIT());
rec.add(buyBean.getBUY_DATE());
rec.add(buyBean.getRETURNED());
table2Data.add(rec);
}
tm2.setDataVector(table2Data, table2ColNames);
for (int i = 0; i < table2.getColumnCount(); i++) {
DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
centerRenderer.setHorizontalAlignment(JLabel.CENTER);
table2.getColumnModel().getColumn(i)
.setCellRenderer(centerRenderer);
}
for (int i = 0; i < table2.getRowCount(); i++) {
table2.setRowHeight(i, 28);
}
table2.getColumnModel().getColumn(0).setPreferredWidth(25);
}
i hope it is clear now
Maybe you have problems with the scrollpane (like: no scrollbars visible).
Are your sure you are using it correct?
this looks strange to me:
You create a ViewPort and put this as View?!
JViewport vp2 = new JViewport();
scrollPane_1.setViewportView(vp2);
vp2.add(table2);
why not just set the Table as ViewportView (as the name implies?)
just:
scrollPane_1.setViewportView(table2);
Edit: Info about the ViewPort:
The "viewport" or "porthole" through which you see the underlying information. When you scroll, what moves is the viewport. It is like peering through a camera's viewfinder. Moving the viewfinder upwards brings new things into view at the top of the picture and loses things that were at the bottom.
http://docs.oracle.com/javase/7/docs/api/javax/swing/JViewport.html

Categories

Resources